HHH-6405 test case for setFetchMode ignored using criteria queries
This commit is contained in:
parent
b78fb983f8
commit
ed0a9fbc00
|
@ -23,6 +23,8 @@
|
|||
*/
|
||||
package org.hibernate.test.criteria;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
|
@ -34,6 +36,7 @@ import org.junit.Test;
|
|||
|
||||
import org.hibernate.Criteria;
|
||||
import org.hibernate.FetchMode;
|
||||
import org.hibernate.Hibernate;
|
||||
import org.hibernate.JDBCException;
|
||||
import org.hibernate.QueryException;
|
||||
import org.hibernate.ScrollableResults;
|
||||
|
@ -1097,7 +1100,7 @@ public class CriteriaQueryTest extends BaseCoreFunctionalTestCase {
|
|||
result = s.createCriteria( Student.class )
|
||||
.setProjection( Projections.count( "cityState.city" ) )
|
||||
.uniqueResult();
|
||||
assertEquals( 2, ( ( Long ) result ).longValue() );
|
||||
assertEquals( 2, ((Long) result).longValue() );
|
||||
|
||||
result = s.createCriteria( Student.class )
|
||||
.setProjection( Projections.countDistinct( "cityState.city" ) )
|
||||
|
@ -1454,8 +1457,8 @@ public class CriteriaQueryTest extends BaseCoreFunctionalTestCase {
|
|||
Transaction t = s.beginTransaction();
|
||||
|
||||
Course course = new Course();
|
||||
course.setCourseCode("HIB");
|
||||
course.setDescription("Hibernate Training");
|
||||
course.setCourseCode( "HIB" );
|
||||
course.setDescription( "Hibernate Training" );
|
||||
course.getCourseMeetings().add( new CourseMeeting( course, "Monday", 1, "1313 Mockingbird Lane" ) );
|
||||
s.save(course);
|
||||
s.flush();
|
||||
|
@ -1477,7 +1480,7 @@ public class CriteriaQueryTest extends BaseCoreFunctionalTestCase {
|
|||
s.save( gaith );
|
||||
s.flush();
|
||||
|
||||
List cityStates = ( List ) s.createCriteria( Student.class).setProjection( Projections.property( "cityState" )).list();
|
||||
List cityStates = ( List ) s.createCriteria( Student.class).setProjection( Projections.property( "cityState" ) ).list();
|
||||
t.rollback();
|
||||
s.close();
|
||||
}
|
||||
|
@ -1610,8 +1613,8 @@ public class CriteriaQueryTest extends BaseCoreFunctionalTestCase {
|
|||
.list();
|
||||
assertEquals( 3, result.size() );
|
||||
assertNotNull( result.get(0) );
|
||||
assertNotNull( result.get(1) );
|
||||
assertNotNull( result.get(2) );
|
||||
assertNotNull( result.get( 1 ) );
|
||||
assertNotNull( result.get( 2 ) );
|
||||
|
||||
result = session.createCriteria( Student.class )
|
||||
.setFetchMode( "preferredCourse", FetchMode.JOIN )
|
||||
|
@ -1621,7 +1624,7 @@ public class CriteriaQueryTest extends BaseCoreFunctionalTestCase {
|
|||
assertEquals( 3, result.size() );
|
||||
assertNotNull( result.get(0) );
|
||||
assertNotNull( result.get(1) );
|
||||
assertNotNull( result.get(2) );
|
||||
assertNotNull( result.get( 2 ) );
|
||||
|
||||
session.delete(gavin);
|
||||
session.delete(leonardo);
|
||||
|
@ -1956,5 +1959,58 @@ public class CriteriaQueryTest extends BaseCoreFunctionalTestCase {
|
|||
t.rollback();
|
||||
session.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCriteriaFetchMode() {
|
||||
Session session = openSession();
|
||||
Transaction t = session.beginTransaction();
|
||||
|
||||
Student gavin = new Student();
|
||||
gavin.setName("Gavin King");
|
||||
gavin.setStudentNumber(232);
|
||||
|
||||
Country gb = new Country("GB", "United Kingdom");
|
||||
Country fr = new Country("FR", "France");
|
||||
session.persist(gb);
|
||||
session.persist(fr);
|
||||
|
||||
List studyAbroads = new ArrayList();
|
||||
StudyAbroad sa1 = new StudyAbroad(gb, new Date());
|
||||
StudyAbroad sa2 = new StudyAbroad(fr, new Date(sa1.getDate().getTime()+1));
|
||||
studyAbroads.add(sa1);
|
||||
studyAbroads.add(sa2);
|
||||
gavin.setStudyAbroads(studyAbroads);
|
||||
|
||||
session.persist(gavin);
|
||||
|
||||
session.flush();
|
||||
session.clear();
|
||||
|
||||
List results = session.createCriteria(Student.class)
|
||||
.setFetchMode("studyAbroads", FetchMode.JOIN)
|
||||
.setFetchMode("studyAbroads.country", FetchMode.JOIN)
|
||||
.list();
|
||||
|
||||
|
||||
assertEquals(results.size(), 2);
|
||||
Student st = (Student)results.get(0);
|
||||
|
||||
assertNotNull(st.getStudyAbroads());
|
||||
assertTrue(Hibernate.isInitialized(st.getStudyAbroads()));
|
||||
assertEquals(st.getStudyAbroads().size(), 2);
|
||||
Country c1 = ((StudyAbroad)st.getStudyAbroads().get(0)).getCountry();
|
||||
Country c2 = ((StudyAbroad)st.getStudyAbroads().get(1)).getCountry();
|
||||
assertTrue( Hibernate.isInitialized( c1 ));
|
||||
assertEquals(c1.getName(), "United Kingdom");
|
||||
|
||||
session.delete(st);
|
||||
session.delete(c1);
|
||||
session.delete(c2);
|
||||
|
||||
t.commit();
|
||||
session.close();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -58,8 +58,21 @@
|
|||
<property name="zip" />
|
||||
</composite-element>
|
||||
</map>
|
||||
<list name="studyAbroads">
|
||||
<key column="studentId" />
|
||||
<list-index column="ind" />
|
||||
<composite-element class="org.hibernate.test.criteria.StudyAbroad">
|
||||
<property name="date" />
|
||||
<many-to-one name="country" />
|
||||
</composite-element>
|
||||
</list>
|
||||
</class>
|
||||
|
||||
<class name="Country">
|
||||
<id name="code" />
|
||||
<property name="name" />
|
||||
</class>
|
||||
|
||||
<class name="Enrolment">
|
||||
<composite-id>
|
||||
<key-property name="studentNumber">
|
||||
|
|
|
@ -1,9 +1,34 @@
|
|||
//$Id: Student.java 9116 2006-01-23 21:21:01Z steveebersole $
|
||||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* Copyright (c) 2012, Red Hat Inc. or third-party contributors as
|
||||
* indicated by the @author tags or express copyright attribution
|
||||
* statements applied by the authors. All third-party contributions are
|
||||
* distributed under license by Red Hat Inc.
|
||||
*
|
||||
* This copyrighted material is made available to anyone wishing to use, modify,
|
||||
* copy, or redistribute it subject to the terms and conditions of the GNU
|
||||
* Lesser General Public License, as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this distribution; if not, write to:
|
||||
* Free Software Foundation, Inc.
|
||||
* 51 Franklin Street, Fifth Floor
|
||||
* Boston, MA 02110-1301 USA
|
||||
*/
|
||||
package org.hibernate.test.criteria;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
|
||||
/**
|
||||
* @author Gavin King
|
||||
*/
|
||||
|
@ -14,6 +39,7 @@ public class Student {
|
|||
private Course preferredCourse;
|
||||
private Set enrolments = new HashSet();
|
||||
private Map addresses;
|
||||
private List studyAbroads;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
|
@ -62,4 +88,12 @@ public class Student {
|
|||
public void setAddresses(Map addresses) {
|
||||
this.addresses = addresses;
|
||||
}
|
||||
|
||||
public List getStudyAbroads() {
|
||||
return studyAbroads;
|
||||
}
|
||||
|
||||
public void setStudyAbroads(List studyAbroads) {
|
||||
this.studyAbroads = studyAbroads;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,32 @@
|
|||
package org.hibernate.test.criteria;
|
||||
|
||||
/**
|
||||
* @author David Mansfield
|
||||
*/
|
||||
public class Country {
|
||||
String code;
|
||||
String name;
|
||||
|
||||
public Country() {}
|
||||
|
||||
public Country(String code, String name) {
|
||||
this.code = code;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public void setCode(String code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package org.hibernate.test.criteria;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class StudyAbroad {
|
||||
private Country country;
|
||||
private Date date;
|
||||
|
||||
public StudyAbroad() {}
|
||||
|
||||
public StudyAbroad(Country country, Date date) {
|
||||
this.country = country;
|
||||
this.date = date;
|
||||
}
|
||||
|
||||
public Country getCountry() {
|
||||
return country;
|
||||
}
|
||||
|
||||
public void setCountry(Country country) {
|
||||
this.country = country;
|
||||
}
|
||||
|
||||
public Date getDate() {
|
||||
return date;
|
||||
}
|
||||
|
||||
public void setDate(Date date) {
|
||||
this.date = date;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue