I would like to create a few classes which are in essense wrapper of a map of key value pair. What is the best appoarch to retrieve or store these classes into 开发者_开发百科a database using hiberate? What are the right hibernate annotations to get the job done?
On the database, I would expect this formation of data
class1, field1, value1
class2, field2, value2
class1, field2, somevalue1
...
On the java side, I may have
@Table("some_db_table")
class KeyValuePairs {
private HashMap<String, String> map;
...
}
class Class1 extends KeyValuePairs {
private Stirng id = "class1";
...
}
class Class2 extends KeyValuePairs {
private Stirng id = "class2";
...
}
I guess you have a ready-made implementation here http://www.java2s.com/Code/Java/Hibernate/HibernateCollectionMappingMap.htm
Check if this is helpful (You can create your subclasses just the way you mentioned)? That generates two tables which is a good practice since you want to keep your primary key that is not being used for business logic. IF you dont want it then you can always tweak it.
精彩评论