-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: New junit test for Hibernate 3 isInitialized()
PostPosted: Tue Feb 01, 2005 8:14 pm 
Beginner
Beginner

Joined: Sat Jan 22, 2005 9:11 am
Posts: 35
Location: London
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.java

Code:
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.java

Code:
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.xml

Code:
<?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>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 02, 2005 6:20 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Are you sure there is not a similar test somewhere?
If not, put this to JIRA please

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.