Hi all, I am facing a complex requirement, puzzling me a lot. Help...
Hibernate:4.3.6 MySql 5.6.21
For support dynamic extension on standard product, such as: Standard POJO:
package com.inspur.gsp;
import java.util.HashMap; import java.util.Map;
public class Person { private String id; private String age;
private Map<CustomPK, Person_T> i18nProperty = new HashMap<CustomPK, Person_T>();
private Map<String, Map<String, Object>> extProperty = new HashMap<String, Map<String, Object>>();
public Map<CustomPK, Person_T> getI18nProperty() { return i18nProperty; }
public void setI18nProperity(Map<CustomPK, Person_T> i18nProperty) { this.i18nProperty = i18nProperty; }
public String getId() { return id; }
public void setId(String id) { this.id = id; }
public String getAge() { return age; }
public void setAge(String age) { this.age = age; }
public Map<String, Map<String, Object>> getExtProperity() { return extProperty; }
public void setExtProperity(Map<String, Map<String, Object>> extProperity) { this.extProperty = extProperity; } }
So, ExtProperity's type is Map<String, Map<String, Object>>, it will be used for storage extension property. Then we need to persist it to database.
Here is my hbm.xml: <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated Nov 3, 2014 11:48:31 AM by Hibernate Tools 3.4.0.CR1 --> <hibernate-mapping> <class name="com.inspur.gsp.Person" table="GSPPerson"> <id name="id" type="java.lang.String"> <column name="ID" length="36" /> <generator class="assigned" /> </id> <property name="age" type="java.lang.String"> <column name="age" length="128" /> </property> <map name="i18nProperty" table="GSPPerson_T"> <key column="ID"></key> <composite-map-key class="com.inspur.gsp.CustomerPK"> <key-property name="culture"></key-property> </composite-map-key> <composite-element class="com.inspur.gsp.Person_T"> <property name="description" column="description"></property> <property name="comments" column="comments"></property> </composite-element> </map> <map name="extProperty" table="GSPPerson_Ext"> <key column="ID"></key> <composite-map-key class="com.inspur.gsp.CustomerPK"> <key-property name="culture"></key-property> </composite-map-key> <composite-element class="java.util.HashMap"> <property name="ext1" column="ext1"></property> <property name="ext2" column="ext2"></property> </composite-element> </map> </class> </hibernate-mapping>
Test code: SessionFactory sf = createSessionFactory(); Person person = new Person(); person.setId("1"); person.setAge("30"); HashMap<CustomPK, Person_T> i18nProperity = new HashMap<CustomPK, Person_T>(); Person_T t1 = new Person_T(); t1.setComments("abc"); t1.setDescription("description"); Person_T t2 = new Person_T(); t2.setComments("efg"); t2.setDescription("ooooooo"); i18nProperity.put(new CustomPK("1", "cn"), t1); i18nProperity.put(new CustomPK("1", "en"), t2); person.setI18nProperity(i18nProperity); HashMap<String, HashMap<String,Object>> extProperty = new HashMap<String, HashMap<String,Object>>(); HashMap<String,Object> e1 = new HashMap<String,Object>(); e1.put("ext1", 1); e1.put("ext2", "c001"); extProperty.put("0001", e1); person.setExtProperity(extProperty);
Session session = sf.getCurrentSession(); Transaction ts = session.beginTransaction(); session.save(person); Person person2 = (Person) session.get(Person.class, "1");
ts.commit();
The configuraion in hbm.xml cannot work. Please help me. Thanks.
Eric
|