Automatically Persisting a Complex Java Object

Posted by VeeArr on Stack Overflow See other posts from Stack Overflow or by VeeArr
Published on 2010-06-09T18:23:32Z Indexed on 2010/06/09 18:52 UTC
Read the original article Hit count: 146

Filed under:
|
|
|

For a project I am working on, I need to persist a number of POJOs to a database. The POJOs class definitions are sometimes highly nested, but they should flatten okay, as the nesting is tree-like and contains no cycles (and the base elements are eventually primitives/Strings). It is preferred that the solution used create one table per data type and that the tables will have one field per primitive member in the POJO. Subclassing and similar problems are not issues for this particular project.

Does anybody know of any existing solutions that can:

  1. Automatically generate a CREATE TABLE definition from the class definition
  2. Automatically generate a query to persist an object to the database, given an instance of the object
  3. Automatically generate a query to retrieve an object from the database and return it as a POJO, given a key.

Solutions that can do this with minimum modifications/annotions to the class files and minimum external configuration are preferred.


Example:

Java classes

//Class to be persisted
class TypeA {
  String guid;
  long timestamp;
  TypeB data1;
  TypeC data2;
}

class TypeB {
  int id;
  int someData;
}

class TypeC {
  int id;
  int otherData;
}

Could map to

CREATE TABLE TypeA (
  guid CHAR(255),
  timestamp BIGINT,
  data1_id INT,
  data1_someData INT,
  data2_id INt,
  data2_otherData INT
);

Or something similar.

© Stack Overflow or respective owner

Related posts about java

Related posts about sql