I am contributing a new jUnit test
org.hibernate.test,manytoone.ManyToOneLazyTest
(Might be of use - I scanned through all the jUnit tests included with Hibernate 3 CVS source and noticed there was no explicit test for the precise many-to-one lazy loading functionality I was looking at - InstrumentTest came close but concentrates on primitive properties rather than entities)
5 files
File 1: org/hibernate/test/manytoone/ManyToOneLazyTest.java
Code:
package org.hibernate.test.manytoone;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.test.TestCase;
/**
*/
public class ManyToOneLazyTest extends TestCase {
public ManyToOneTest(String str) {
super(str);
}
public void testManyToOne() {
Session s = openSession();
Transaction t = s.beginTransaction();
// set up a child with a lazy and a non-lazy parent
Child c = new Child();
c.setName("Child One");
NonLazyParent nlp = new NonLazyParent();
nlp.setName("NonLazy");
LazyParent lp = new LazyParent();
lp.setName("Lazy");
c.setNonLazyParent(nlp);
c.setLazyParent(lp);
s.save(nlp);
s.save(lp);
s.save(c);
t.commit();
s.close();
s = openSession();
s.load( c, c.getId() );
assertTrue( Hibernate.isInitialized(c) );
assertFalse( Hibernate.isInitialized(c.getLazyParent()) );
s.close();
s = openSession();
c = (Child) s.createQuery("from Child").uniqueResult();
assertTrue( Hibernate.isInitialized(c) );
assertFalse( Hibernate.isInitialized(c.getLazyParent()) );
s.close();
s = openSession();
c = (Child) s.load( Child.class, c.getId() );
assertFalse( Hibernate.isInitialized(c) );
lp = c.getLazyParent();
assertTrue( Hibernate.isInitialized(c) );
assertFalse( Hibernate.isInitialized(lp) );
nlp = c.getNonLazyParent();
assertFalse( Hibernate.isInitialized(lp) );
assertTrue( Hibernate.isInitialized(nlp) );
assertEquals( "NonLazy", nlp.getName() );
assertFalse( Hibernate.isInitialized(lp) );
assertEquals( "Lazy", lp.getName() );
assertTrue( Hibernate.isInitialized(lp) );
s.close();
}
protected String[] getMappings() {
return new String[] { "manytoone/Parent.hbm.xml" };
}
public static Test suite() {
return new TestSuite(ManyToOneTest.class);
}
public static void main(String[] args) {
junit.textui.TestRunner.run ( suite() );
}
}
File 2: org/hibernate/test/manytoone/LazyParent.java
Code:
package org.hibernate.test.manytoone;
import java.util.Collection;
import java.util.HashSet;
/**
*/
public class LazyParent {
private Long id;
private String name;
private Collection children = new HashSet();
/**
* @return Returns the children.
*/
public Collection getChildren() {
return children;
}
/**
* @param children The children to set.
*/
public void setChildren(Collection children) {
this.children = children;
}
/**
* @return Returns the id.
*/
public Long getId() {
return id;
}
/**
* @param id The id to set.
*/
public void setId(Long id) {
this.id = id;
}
/**
* @return Returns the name.
*/
public String getName() {
return name;
}
/**
* @param name The name to set.
*/
public void setName(String name) {
this.name = name;
}
}
File 3: org/hibernate/test/manytoone/Child.javaCode:
package org.hibernate.test.manytoone;
public class Child {
private Long id;
private String name;
private LazyParent lazyParent;
private NonLazyParent nonLazyParent;
/**
* @return Returns the id.
*/
public Long getId() {
return id;
}
/**
* @param id The id to set.
*/
public void setId(Long id) {
this.id = id;
}
/**
* @return Returns the name.
*/
public String getName() {
return name;
}
/**
* @param name The name to set.
*/
public void setName(String name) {
this.name = name;
}
/**
* @return Returns the parent.
*/
public LazyParent getLazyParent() {
return this.lazyParent;
}
/**
* @param parent The parent to set.
*/
public void setLazyParent(LazyParent parent) {
this.lazyParent = parent;
}
/**
* @return Returns the parent.
*/
public NonLazyParent getNonLazyParent() {
return this.nonLazyParent;
}
/**
* @param parent The parent to set.
*/
public void setNonLazyParent(NonLazyParent parent) {
this.nonLazyParent = parent;
}
}
File 4: org/hibernate/test/manytoone/NonLazyParent.javaCode:
package org.hibernate.test.manytoone;
import java.util.Collection;
import java.util.HashSet;
/**
*/
public class NonLazyParent {
private Long id;
private String name;
private Collection children = new HashSet();
/**
* @return Returns the children.
*/
public Collection getChildren() {
return children;
}
/**
* @param children The children to set.
*/
public void setChildren(Collection children) {
this.children = children;
}
/**
* @return Returns the id.
*/
public Long getId() {
return id;
}
/**
* @param id The id to set.
*/
public void setId(Long id) {
this.id = id;
}
/**
* @return Returns the name.
*/
public String getName() {
return name;
}
/**
* @param name The name to set.
*/
public void setName(String name) {
this.name = name;
}
}
File 5: org/hibernate/test/manytoone/Parent.hbm.xmlCode:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
package="org.hibernate.test.manytoone">
<class name="LazyParent" lazy="true">
<id name="id"
column="lazy_parent_id">
<generator class="increment"/>
</id>
<property name="name"/>
<set name="children"
cascade="all"
inverse="true">
<key column="parent_id"/>
<one-to-many class="Child"/>
</set>
</class>
<class name="NonLazyParent" lazy="false">
<id name="id"
column="non_lazy_parent_id">
<generator class="increment"/>
</id>
<property name="name"/>
<set name="children"
cascade="all"
inverse="true">
<key column="parent_id"/>
<one-to-many class="Child"/>
</set>
</class>
<class name="Child" lazy="true">
<id name="id"
column="child_id">
<generator class="increment"/>
</id>
<property name="name"/>
<many-to-one name="lazyParent"
column="lazy_parent_id"
lazy="true"
not-null="true"/>
<many-to-one name="nonLazyParent"
column="non_lazy_parent_id"
lazy="false"
not-null="true"/>
</class>
</hibernate-mapping>