I've built a test case using your mapping docs and don't see the same behaviour. In fact it all works fine.
Your bi-directional page mapping is slightly wrong because you don't specify which end is the controlling end (inverse="true") so you end up with 2 columns in TextInstance containing the page id: page_id and page. However, I ran the test with your mapping as-is and it doesn't seem to affect things.
Also, the page mapping refers to "beans.ImageInstance" which I presume is just a typo and should be beans.TextInstance ??
I'm using session.load to retrieve the Page but I see different results in eclipse. Immediately after the load, Page is cglib enhanced but
page > textAttributes, null
page > textInstances, null
Perhaps its the way/order your objects are constructed or something in your java code?
hibernate version: 3.2.5ga
Here's my test code:
Code:
package test.onetoone;
import java.util.List;
public class Page {
private long id;
private List<TextInstance> textInstances;
private TextAttributes textAttributes;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public List<TextInstance> getTextInstances() {
return textInstances;
}
public void setTextInstances(List<TextInstance> textInstances) {
this.textInstances = textInstances;
}
public TextAttributes getTextAttributes() {
return textAttributes;
}
public void setTextAttributes(TextAttributes textAttributes) {
this.textAttributes = textAttributes;
}
}
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="test.onetoone">
<class name="Page" table="onetoone_page">
<id name="id">
<meta attribute="scope-set">protected</meta>
<generator class="foreign">
<param name="property">textAttributes</param>
</generator>
</id>
<list name="textInstances" cascade="all-delete-orphan">
<key column="page_id"/>
<list-index column="index_col"/>
<one-to-many class="TextInstance"/>
</list>
<one-to-one name="textAttributes" class="TextAttributes" constrained="true" lazy="false" cascade="all"/>
</class>
</hibernate-mapping>
package test.onetoone;
public class TextInstance {
private long id;
private String body;
private TextAttributes textAttributes;
private Page page;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public TextAttributes getTextAttributes() {
return textAttributes;
}
public void setTextAttributes(TextAttributes textAttributes) {
this.textAttributes = textAttributes;
}
public Page getPage() {
return page;
}
public void setPage(Page page) {
this.page = page;
}
}
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="test.onetoone">
<class name="TextInstance" table="onetoone_text_instance">
<id name="id">
<meta attribute="scope-set">protected</meta>
<generator class="foreign">
<param name="property">textAttributes</param>
</generator>
</id>
<property name="body" type="text" not-null="true"/>
<one-to-one name="textAttributes" class="TextAttributes" constrained="true" lazy="false" cascade="all"/>
<many-to-one name="page" class="Page" not-null="true"/>
</class>
</hibernate-mapping>
package test.onetoone;
public class TextAttributes {
private long id;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
}
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="test.onetoone">
<class name="TextAttributes" table="onetoone_text_attributes">
<id name="id">
<meta attribute="scope-set">protected</meta>
<generator class="native" />
</id>
</class>
</hibernate-mapping>
package test.onetoone;
import java.util.ArrayList;
import java.util.List;
import junit.framework.TestCase;
import org.hibernate.Session;
public class Test extends TestCase {
public void testIt() {
createObjects();
Session s = HibernateUtil.getSession();
s.beginTransaction();
Page page = (Page)s.load(Page.class, new Long(1));
TextAttributes pageTa = page.getTextAttributes();
assertEquals(1, pageTa.getId());
TextInstance textInstance = (TextInstance)page.getTextInstances().get(0);
assertEquals(2, textInstance.getId());
TextAttributes textInstanceTa = textInstance.getTextAttributes();
assertEquals(2, textInstanceTa.getId());
s.getTransaction().commit();
s.close();
}
private void createObjects() {
Session s = HibernateUtil.getSession();
s.beginTransaction();
TextAttributes ta1 = new TextAttributes();
TextAttributes ta2 = new TextAttributes();
TextInstance ti = new TextInstance();
Page page = new Page();
page.setTextAttributes(ta1);
ti.setBody("body");
ti.setPage(page);
ti.setTextAttributes(ta2);
List<TextInstance> textInstances = new ArrayList<TextInstance>();
textInstances.add(ti);
page.setTextInstances(textInstances);
s.save(page);
s.getTransaction().commit();
s.close();
}
}
package test.onetoone;
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(Page.class)
.addClass(TextInstance.class)
.addClass(TextAttributes.class)
.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;
}
}
generated sql
Code:
Hibernate: insert into onetoone_text_attributes values ( )
Hibernate: insert into onetoone_page (id) values (?)
Hibernate: insert into onetoone_text_attributes values ( )
Hibernate: insert into onetoone_text_instance (body, page, id) values (?, ?, ?)
Hibernate: update onetoone_text_instance set page_id=?, index_col=? where id=?
Hibernate: select page0_.id as id0_0_ from onetoone_page page0_ where page0_.id=?
Hibernate: select textattrib0_.id as id2_0_ from onetoone_text_attributes textattrib0_ where textattrib0_.id=?
Hibernate: select textinstan0_.page_id as page4_1_, textinstan0_.id as id1_, textinstan0_.index_col as index5_1_, textinstan0_.id as id1_0_, textinstan0_.body as body1_0_, textinstan0_.page as page1_0_ from onetoone_text_instance textinstan0_ where textinstan0_.page_id=?
Hibernate: select textattrib0_.id as id2_0_ from onetoone_text_attributes textattrib0_ where textattrib0_.id=?