How about this... create a separate java class for each of your table types and use "table per class hierarchy" to persist them. Type 2 would have a collection of Table2Data objects, type 3 would have a collection of Table3Data objects, type 0 would have no collections.
e.g.
Table.java
Code:
package test.tableperhierarchy;
public abstract class Table {
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
TableWithNoTypeData.java
Code:
package test.tableperhierarchy;
public class TableWithNoTypeData extends Table {
}
TableWithType2Data.java
Code:
package test.tableperhierarchy;
import java.util.Set;
public class TableWithType2Data extends Table {
private Set<Type2Data> type2Data;
public Set<Type2Data> getType2Data() {
return type2Data;
}
public void setType2Data(Set<Type2Data> type2Data) {
this.type2Data = type2Data;
}
}
TableWithType3Data.java
Code:
package test.tableperhierarchy;
import java.util.Set;
public class TableWithType3Data extends Table {
private Set<Type3Data> type3Data;
public Set<Type3Data> getType3Data() {
return type3Data;
}
public void setType3Data(Set<Type3Data> type3Data) {
this.type3Data = type3Data;
}
}
Table.hbm.xml
Code:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="test.tableperhierarchy">
<class name="Table" table="tableperhierarchy" discriminator-value="0">
<id name="id">
<generator class="native"/>
</id>
<discriminator column="table_type" type="integer"/>
<subclass name="TableWithType2Data" discriminator-value="2">
<set name="type2Data">
<key column="table_id"/>
<one-to-many class="Type2Data"/>
</set>
</subclass>
<subclass name="TableWithType3Data" discriminator-value="3">
<set name="type3Data">
<key column="table_id"/>
<one-to-many class="Type3Data"/>
</set>
</subclass>
<subclass name="TableWithNoTypeData" discriminator-value="0">
</subclass>
</class>
</hibernate-mapping>
Type2Data.java
Code:
package test.tableperhierarchy;
public class Type2Data {
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
Type2Data.hbm.xml
Code:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="test.tableperhierarchy">
<class name="Type2Data" table="type2data">
<id name="id">
<generator class="native"/>
</id>
</class>
</hibernate-mapping>
Type3Data.java
Code:
package test.tableperhierarchy;
public class Type3Data {
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
Type3Data.hbm.xml
Code:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="test.tableperhierarchy">
<class name="Type3Data" table="type3data">
<id name="id">
<generator class="native"/>
</id>
</class>
</hibernate-mapping>
TestIt.java
Code:
package test.tableperhierarchy;
import java.util.HashSet;
import java.util.Set;
import junit.framework.TestCase;
import org.hibernate.Session;
public class TestIt extends TestCase {
public void testIt() {
createObjects();
Session s = HibernateUtil.getSession();
s.beginTransaction();
TableWithType2Data tableWithType2Data = (TableWithType2Data)s.load(TableWithType2Data.class, new Long(2));
assertEquals(3, tableWithType2Data.getType2Data().size());
s.getTransaction().commit();
s.close();
}
private void createObjects() {
Session s = HibernateUtil.getSession();
s.beginTransaction();
TableWithNoTypeData table1 = new TableWithNoTypeData();
s.save(table1);
Type2Data type2Data1 = new Type2Data();
s.save(type2Data1);
Type2Data type2Data2 = new Type2Data();
s.save(type2Data2);
Type2Data type2Data3 = new Type2Data();
s.save(type2Data3);
Set<Type2Data> type2DataSet = new HashSet<Type2Data>();
type2DataSet.add(type2Data1);
type2DataSet.add(type2Data2);
type2DataSet.add(type2Data3);
TableWithType2Data table2 = new TableWithType2Data();
table2.setType2Data(type2DataSet);
s.save(table2);
Type3Data type3Data1 = new Type3Data();
s.save(type3Data1);
Type3Data type3Data2 = new Type3Data();
s.save(type3Data2);
Type3Data type3Data3 = new Type3Data();
s.save(type3Data3);
Set<Type3Data> type3DataSet = new HashSet<Type3Data>();
type3DataSet.add(type3Data1);
type3DataSet.add(type3Data2);
type3DataSet.add(type3Data3);
TableWithType3Data table3 = new TableWithType3Data();
table3.setType3Data(type3DataSet);
s.save(table3);
s.getTransaction().commit();
s.close();
}
}
HibernateUtil.java
Code:
package test.tableperhierarchy;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static SessionFactory factory;
static {
Configuration config = new Configuration()
.addClass(Table.class)
.addClass(Type2Data.class)
.addClass(Type3Data.class)
// MYSQL config
.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect")
.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver")
.setProperty("hibernate.connection.url", "jdbc:mysql://localhost/test")
.setProperty("hibernate.connection.username", "root")
.setProperty("hibernate.connection.password", "password")
.setProperty("hibernate.hbm2ddl.auto", "create-drop")
.setProperty("hibernate.cache.provider_class", "org.hibernate.cache.NoCacheProvider")
.setProperty("hibernate.show_sql", "true");
HibernateUtil.setSessionFactory(config.buildSessionFactory());
}
public static synchronized Session getSession() {
if (factory == null) {
factory = new Configuration().configure().buildSessionFactory();
}
return factory.openSession();
}
public static void setSessionFactory(SessionFactory factory) {
HibernateUtil.factory = factory;
}
}