Search code examples
javajpajdbcpojopersist

Is it possible to persist POJOs with JDBC?


silly qeustion: I wonder if it is possible to persist POJOs with JDBC like it is possible with JPA?

Lets say I have the class "A" that contains one field

public class A implements Serializable{

    public String someString = "asd";
}

Now let's say I have an Instance of A (A myA = new A(); ).

My question is: Is it possible to persist "myA" with JDBC or do I have to persist the String. Or when I want to load "myA" can I load the instance "myA" or do I have to build up "myA" by loading the String and create a new instance form A with that loaded String.


Solution

  • You can store serialized Java objects to a database; e.g. by using a Blob type.

    But it is not generally a good idea:

    • It is is impractical to perform an SQL query that tests the state of a serialized object in any non-trivial way.

    • Serialized objects tend to be sensitive to changes in the object's code. Dealing with this can be awkward, and not dealing with it can leave you with serialized objects in your database that you can't deserialize any more.

    If you want to persist POJOs to relational database, you would be better off using an object-relational mapping, such as JPA or Hibernate.