|
From: <hib...@li...> - 2006-05-06 03:33:51
|
Author: ste...@jb...
Date: 2006-05-05 23:33:33 -0400 (Fri, 05 May 2006)
New Revision: 9901
Added:
trunk/Hibernate3/test/org/hibernate/test/ejb3/fetch/
trunk/Hibernate3/test/org/hibernate/test/ejb3/fetch/FetchingTest.java
trunk/Hibernate3/test/org/hibernate/test/ejb3/fetch/Person.hbm.xml
trunk/Hibernate3/test/org/hibernate/test/ejb3/fetch/Person.java
trunk/Hibernate3/test/org/hibernate/test/ejb3/fetch/Stay.java
Modified:
trunk/Hibernate3/test/org/hibernate/test/ejb3/EJB3Suite.java
Log:
copied over a test from annotations
Modified: trunk/Hibernate3/test/org/hibernate/test/ejb3/EJB3Suite.java
===================================================================
--- trunk/Hibernate3/test/org/hibernate/test/ejb3/EJB3Suite.java 2006-05-06 00:13:44 UTC (rev 9900)
+++ trunk/Hibernate3/test/org/hibernate/test/ejb3/EJB3Suite.java 2006-05-06 03:33:33 UTC (rev 9901)
@@ -5,6 +5,7 @@
import org.hibernate.test.ejb3.lock.EJB3LockTest;
import org.hibernate.test.ejb3.lock.RepeatableReadTest;
import org.hibernate.test.ejb3.proxy.Ejb3ProxyTest;
+import org.hibernate.test.ejb3.fetch.FetchingTest;
/**
* @author Steve Ebersole
@@ -15,6 +16,7 @@
suite.addTest( EJB3LockTest.suite() );
suite.addTest( RepeatableReadTest.suite() );
suite.addTest( Ejb3ProxyTest.suite() );
+ suite.addTest( FetchingTest.suite() );
return suite;
}
}
Added: trunk/Hibernate3/test/org/hibernate/test/ejb3/fetch/FetchingTest.java
===================================================================
--- trunk/Hibernate3/test/org/hibernate/test/ejb3/fetch/FetchingTest.java 2006-05-06 00:13:44 UTC (rev 9900)
+++ trunk/Hibernate3/test/org/hibernate/test/ejb3/fetch/FetchingTest.java 2006-05-06 03:33:33 UTC (rev 9901)
@@ -0,0 +1,83 @@
+package org.hibernate.test.ejb3.fetch;
+
+import java.util.Date;
+
+import org.hibernate.Hibernate;
+import org.hibernate.Session;
+import org.hibernate.Transaction;
+import org.hibernate.test.TestCase;
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+/**
+ * @author Emmanuel Bernard
+ */
+public class FetchingTest extends TestCase {
+
+ public void testLazy() throws Exception {
+ Session s;
+ Transaction tx;
+ s = openSession();
+ tx = s.beginTransaction();
+ Person p = new Person( "Gavin", "King", "JBoss Inc" );
+ Stay stay = new Stay( p, new Date(), new Date(), "A380", "Blah", "Blah" );
+ p.addStay( stay );
+ s.persist( p );
+ tx.commit();
+ s.clear();
+ tx = s.beginTransaction();
+ p = (Person) s.createQuery( "from Person p where p.firstName = :name" )
+ .setParameter( "name", "Gavin" ).uniqueResult();
+ assertFalse( Hibernate.isInitialized( p.getStays() ) );
+ s.delete( p );
+ tx.commit();
+ s.close();
+ }
+
+ public void testHibernateFetchingLazy() throws Exception {
+ Session s;
+ Transaction tx;
+ s = openSession();
+ tx = s.beginTransaction();
+ Person p = new Person( "Gavin", "King", "JBoss Inc" );
+ Stay stay = new Stay( null, new Date(), new Date(), "A380", "Blah", "Blah" );
+ Stay stay2 = new Stay( null, new Date(), new Date(), "A320", "Blah", "Blah" );
+ Stay stay3 = new Stay( null, new Date(), new Date(), "A340", "Blah", "Blah" );
+ stay.setOldPerson( p );
+ stay2.setVeryOldPerson( p );
+ stay3.setVeryOldPerson( p );
+ p.addOldStay( stay );
+ p.addVeryOldStay( stay2 );
+ p.addVeryOldStay( stay3 );
+ s.persist( p );
+ tx.commit();
+ s.clear();
+ tx = s.beginTransaction();
+ p = (Person) s.createQuery( "from Person p where p.firstName = :name" )
+ .setParameter( "name", "Gavin" ).uniqueResult();
+ assertFalse( Hibernate.isInitialized( p.getOldStays() ) );
+ assertEquals( 1, p.getOldStays().size() );
+ assertFalse( "lazy extra is failing", Hibernate.isInitialized( p.getOldStays() ) );
+ s.clear();
+ stay = (Stay) s.get( Stay.class, stay.getId() );
+ assertTrue( ! Hibernate.isInitialized( stay.getOldPerson() ) );
+ s.clear();
+ stay3 = (Stay) s.get( Stay.class, stay3.getId() );
+ assertTrue( "FetchMode.JOIN should overrides lazy options", Hibernate.isInitialized( stay3.getVeryOldPerson() ) );
+ s.delete( stay3.getVeryOldPerson() );
+ tx.commit();
+ s.close();
+ }
+
+ public FetchingTest(String x) {
+ super( x );
+ }
+
+ protected String[] getMappings() {
+ return new String[] { "ejb3/fetch/Person.hbm.xml" };
+ }
+
+ public static Test suite() {
+ return new TestSuite( FetchingTest.class );
+ }
+}
Added: trunk/Hibernate3/test/org/hibernate/test/ejb3/fetch/Person.hbm.xml
===================================================================
--- trunk/Hibernate3/test/org/hibernate/test/ejb3/fetch/Person.hbm.xml 2006-05-06 00:13:44 UTC (rev 9900)
+++ trunk/Hibernate3/test/org/hibernate/test/ejb3/fetch/Person.hbm.xml 2006-05-06 03:33:33 UTC (rev 9901)
@@ -0,0 +1,48 @@
+<!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.ejb3.fetch">
+
+ <class name="Person" table="PERSON">
+ <id name="id" column="ID" type="long">
+ <generator class="increment"/>
+ </id>
+ <property name="firstName"/>
+ <property name="lastName"/>
+ <property name="companyName"/>
+
+ <bag name="stays" cascade="all" lazy="true" inverse="true">
+ <key column="PERS_ID"/>
+ <one-to-many class="Stay"/>
+ </bag>
+
+ <bag name="oldStays" cascade="all" lazy="extra" fetch="subselect" inverse="true">
+ <key column="OLD_PERS_ID"/>
+ <one-to-many class="Stay"/>
+ </bag>
+
+ <bag name="veryOldStays" cascade="all" lazy="true" fetch="select" inverse="true">
+ <key column="VERY_OLD_PERS_ID"/>
+ <one-to-many class="Stay"/>
+ </bag>
+ </class>
+
+ <class name="Stay" table="STAY">
+ <id name="id" column="ID" type="long">
+ <generator class="increment"/>
+ </id>
+
+ <property name="startDate"/>
+ <property name="endDate"/>
+ <property name="vessel"/>
+ <property name="authoriser"/>
+ <property name="comments"/>
+
+ <many-to-one name="person" column="PERS_ID" class="Person" cascade="all"/>
+ <many-to-one name="oldPerson" column="OLD_PERS_ID" class="Person" cascade="all" fetch="select"/>
+ <many-to-one name="veryOldPerson" column="VERY_OLD_PERS_ID" class="Person" cascade="all" fetch="join"/>
+
+ </class>
+
+</hibernate-mapping>
\ No newline at end of file
Added: trunk/Hibernate3/test/org/hibernate/test/ejb3/fetch/Person.java
===================================================================
--- trunk/Hibernate3/test/org/hibernate/test/ejb3/fetch/Person.java 2006-05-06 00:13:44 UTC (rev 9900)
+++ trunk/Hibernate3/test/org/hibernate/test/ejb3/fetch/Person.java 2006-05-06 03:33:33 UTC (rev 9901)
@@ -0,0 +1,137 @@
+package org.hibernate.test.ejb3.fetch;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Date;
+
+/**
+ * Copied over from annotations test suite...
+ *
+ * @author Emmanuel Bernard
+ */
+public class Person implements Serializable {
+
+ // member declaration
+ private Long id;
+ private String firstName;
+ private String lastName;
+ private String companyName;
+ private Collection stays;
+ private Collection oldStays;
+ private Collection veryOldStays;
+
+ // constructors
+ public Person() {
+ }
+
+ public Person(String firstName, String lastName, String companyName) {
+ this.firstName = firstName;
+ this.lastName = lastName;
+ this.companyName = companyName;
+ }
+
+ public Long getId() {
+ return id;
+ }
+
+ public void setId(Long id) {
+ this.id = id;
+ }
+
+ public String getFirstName() {
+ return firstName;
+ }
+
+ public void setFirstName(String firstName) {
+ this.firstName = firstName;
+ }
+
+ public String getLastName() {
+ return lastName;
+ }
+
+ public void setLastName(String lastName) {
+ this.lastName = lastName;
+ }
+
+ public String getCompanyName() {
+ return companyName;
+ }
+
+ public void setCompanyName(String companyName) {
+ this.companyName = companyName;
+ }
+
+ public Collection getStays() {
+ return stays;
+ }
+
+ public void setStays(Collection stays) {
+ this.stays = stays;
+ }
+
+ public Collection getOldStays() {
+ return oldStays;
+ }
+
+ public void setOldStays(Collection oldStays) {
+ this.oldStays = oldStays;
+ }
+
+ public Collection getVeryOldStays() {
+ return veryOldStays;
+ }
+
+ public void setVeryOldStays(Collection veryOldStays) {
+ this.veryOldStays = veryOldStays;
+ }
+
+
+ // business logic
+ public void addStay(Date startDate, Date endDate, String vessel, String authoriser, String comments) {
+ Stay stay = new Stay( this, startDate, endDate, vessel, authoriser, comments );
+ addStay( stay );
+ }
+
+ public void addStay(Stay stay) {
+ Collection stays = getStays();
+ if ( stays == null ) {
+ stays = new ArrayList();
+ }
+ stays.add( stay );
+
+ this.stays = stays;
+ }
+
+ public void addOldStay(Date startDate, Date endDate, String vessel, String authoriser, String comments) {
+ Stay stay = new Stay( this, startDate, endDate, vessel, authoriser, comments );
+ addOldStay( stay );
+ }
+
+ public void addOldStay(Stay stay) {
+ Collection stays = getOldStays();
+ if ( stays == null ) {
+ stays = new ArrayList();
+ }
+ stays.add( stay );
+
+ this.oldStays = stays;
+ }
+
+ public void addVeryOldStay(Date startDate, Date endDate, String vessel, String authoriser, String comments) {
+ Stay stay = new Stay( this, startDate, endDate, vessel, authoriser, comments );
+ addVeryOldStay( stay );
+ }
+
+ public void addVeryOldStay(Stay stay) {
+ Collection stays = getVeryOldStays();
+ if ( stays == null ) {
+ stays = new ArrayList();
+ }
+ stays.add( stay );
+
+ this.veryOldStays = stays;
+ }
+}
+
Added: trunk/Hibernate3/test/org/hibernate/test/ejb3/fetch/Stay.java
===================================================================
--- trunk/Hibernate3/test/org/hibernate/test/ejb3/fetch/Stay.java 2006-05-06 00:13:44 UTC (rev 9900)
+++ trunk/Hibernate3/test/org/hibernate/test/ejb3/fetch/Stay.java 2006-05-06 03:33:33 UTC (rev 9901)
@@ -0,0 +1,107 @@
+package org.hibernate.test.ejb3.fetch;
+
+import java.io.Serializable;
+import java.util.Date;
+
+/**
+ * @author Emmanuel Bernard
+ */
+public class Stay implements Serializable {
+
+ // member declaration
+ private Long id;
+ private Person person;
+ private Person oldPerson;
+ private Person veryOldPerson;
+ private Date startDate;
+ private Date endDate;
+ private String vessel;
+ private String authoriser;
+ private String comments;
+
+
+ // constructors
+ public Stay() {
+ }
+
+ public Stay(Person person, Date startDate, Date endDate, String vessel, String authoriser, String comments) {
+ this.authoriser = authoriser;
+ this.endDate = endDate;
+ this.person = person;
+ this.startDate = startDate;
+ this.vessel = vessel;
+ this.comments = comments;
+ }
+
+ public Long getId() {
+ return id;
+ }
+
+ public void setId(Long id) {
+ this.id = id;
+ }
+
+ public Person getPerson() {
+ return person;
+ }
+
+ public void setPerson(Person person) {
+ this.person = person;
+ }
+
+ public Person getOldPerson() {
+ return oldPerson;
+ }
+
+ public void setOldPerson(Person oldPerson) {
+ this.oldPerson = oldPerson;
+ }
+
+ public Person getVeryOldPerson() {
+ return veryOldPerson;
+ }
+
+ public void setVeryOldPerson(Person veryOldPerson) {
+ this.veryOldPerson = veryOldPerson;
+ }
+
+ public Date getStartDate() {
+ return startDate;
+ }
+
+ public void setStartDate(Date startDate) {
+ this.startDate = startDate;
+ }
+
+ public Date getEndDate() {
+ return endDate;
+ }
+
+ public void setEndDate(Date endDate) {
+ this.endDate = endDate;
+ }
+
+ public String getVessel() {
+ return vessel;
+ }
+
+ public void setVessel(String vessel) {
+ this.vessel = vessel;
+ }
+
+ public String getAuthoriser() {
+ return authoriser;
+ }
+
+ public void setAuthoriser(String authoriser) {
+ this.authoriser = authoriser;
+ }
+
+ public String getComments() {
+ return comments;
+ }
+
+ public void setComments(String comments) {
+ this.comments = comments;
+ }
+}
\ No newline at end of file
|