I’m using Hibernate 3.6 but would prefer to stick to JPA annotations if possible.
I have a parent entity which has an id, and possibly other simple properties (name, description,etc) but will have a 2-D collection of children (in the simple case, Strings).
Ideally I’d like the ParentEntity to look like this:
class ParentEntity {
@Id
private Long id;
....
@????
// first Long should be column_index, second Long should be row_index
private Map<Long,Map<Long,String>> gridOfStrings;
}
In my database, my gridOfString is represented like this:
GridItems
------------
grid_id (int,FK to grid)
column_index (int)
row_index (int)
value (varchar)
I have some amount of flexibility to change the schema or the way that the entity works.
What’s the clearest way to have Hibernate do the magic for me? I want to avoid having to write the logic for the mapping/transformation of a list of entities into columns of rows of strings.
Alternative approach
I can assume that I’ll only have 2 or 3 columns and make them separate Maps. I’m not sure how to have hibernate filter each join though.
The Entity could look like this:
@???
//rows where column_index=1
private Map<Long,String> column1;
...
private Map<Long,String> column2;
...
private Map<Long,String> column3;
The easiest way for Hibernate to do this for you would be to break your “grid” into an object that has a list of strings, and then your
ParentEntitywould have a list of those objects. Hibernate can then easily map that out and can eagerly fetch the structure if you like so that you have it all in memory and don’t incur additional selects.