So a Task has both DoThis and DoThat items associated with it. Are they as similar in real life as in your example? It would affect how you mapped them. Lets assume that they are different enough to warrant 2 unrelated classes and 2 separate tables.
For now I'll also assume the relationship between Task and DoThis/DoThat is one to many (you don't specify). Here's some example code and mappings... DoThisCollection and DoThatCollection will be lazy loaded by default.
DoThat.java
Code:
package test.thisthat;
public class DoThat {
private Long id;
private String name;
public Long getId() {
return id;
}
protected DoThat() {}
public DoThat(String name) {
super();
this.name = name;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
DoThat.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.thisthat">
<class name="DoThat" table="thisthat_do_that">
<id name="id">
<generator class="native" />
</id>
<property name="name"/>
</class>
</hibernate-mapping>
DoThis.java
Code:
package test.thisthat;
public class DoThis {
private Long id;
private String name;
protected DoThis() {}
public DoThis(String name) {
super();
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
DoThis.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.thisthat">
<class name="DoThis" table="thisthat_do_this">
<id name="id">
<generator class="native" />
</id>
<property name="name"/>
</class>
</hibernate-mapping>
Task.java
Code:
package test.thisthat;
import java.util.HashSet;
import java.util.Set;
public class Task {
private Long id;
private String name;
private Set<DoThat> doThatCollection;
private Set<DoThis> doThisCollection;
protected Task() {}
public Task(String name) {
super();
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
protected Set<DoThat> getDoThatCollection() {
return doThatCollection;
}
protected void setDoThatCollection(Set<DoThat> doThatCollection) {
this.doThatCollection = doThatCollection;
}
protected Set<DoThis> getDoThisCollection() {
return doThisCollection;
}
protected void setDoThisCollection(Set<DoThis> doThisCollection) {
this.doThisCollection = doThisCollection;
}
public void addDoThis(DoThis doThis) {
if (doThisCollection == null) {
doThisCollection = new HashSet<DoThis>();
}
doThisCollection.add(doThis);
}
public void addDoThat(DoThat doThat) {
if (doThatCollection == null) {
doThatCollection = new HashSet<DoThat>();
}
doThatCollection.add(doThat);
}
}
Task.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.thisthat">
<class name="Task" table="thisthat_task">
<id name="id">
<generator class="native" />
</id>
<property name="name"/>
<set name="doThatCollection" cascade="all">
<key column="task_id"/>
<one-to-many class="DoThat"/>
</set>
<set name="doThisCollection" cascade="all">
<key column="task_id"/>
<one-to-many class="DoThis"/>
</set>
</class>
</hibernate-mapping>
Test.java
Code:
package test.thisthat;
import org.hibernate.Session;
import junit.framework.TestCase;
public class Test extends TestCase {
public void testCreate() {
Long taskId = createObjects();
Session s = HibernateUtil.getSession();
s.beginTransaction();
Task task = (Task)s.load(Task.class, taskId);
assertEquals("task1", task.getName());
assertEquals(2, task.getDoThisCollection().size());
assertEquals(2, task.getDoThatCollection().size());
assertTrue(task.getDoThisCollection().iterator().next().getName().startsWith("DoThis"));
assertTrue(task.getDoThatCollection().iterator().next().getName().startsWith("DoThat"));
s.getTransaction().commit();
s.close();
}
private Long createObjects() {
Session s = HibernateUtil.getSession();
s.beginTransaction();
Task task = new Task("task1");
task.addDoThis(new DoThis("DoThis1"));
task.addDoThis(new DoThis("DoThis2"));
task.addDoThat(new DoThat("DoThat1"));
task.addDoThat(new DoThat("DoThat2"));
Long taskId = (Long)s.save(task);
s.getTransaction().commit();
s.close();
return taskId;
}
}
HibernateUtil.java
Code:
package test.thisthat;
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(Task.class)
.addClass(DoThis.class)
.addClass(DoThat.class)
.setProperty("hibernate.dialect", "org.hibernate.dialect.HSQLDialect")
.setProperty("hibernate.connection.driver_class", "org.hsqldb.jdbcDriver")
.setProperty("hibernate.connection.url", "jdbc:hsqldb:mem:test")
.setProperty("hibernate.connection.username", "sa")
.setProperty("hibernate.connection.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;
}
}