| CODENOTIFIER | HelpYou are not signed inSign in |
Project: Hibernate
Revision: 15138
Author: steve.ebersole@jboss.com
Date: 21 Aug 2008 08:28:41
Changes:HHH-3414 : fetch profiles
Files:| ... | ...@@ -0,0 +1,96 @@ | |
| 1 | <?xml version="1.0"?> | |
| 2 | <!DOCTYPE hibernate-mapping | |
| 3 | SYSTEM | |
| 4 | "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > | |
| 5 | ||
| 6 | <!-- | |
| 7 | ~ Hibernate, Relational Persistence for Idiomatic Java | |
| 8 | ~ | |
| 9 | ~ Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as | |
| 10 | ~ indicated by the @author tags or express copyright attribution | |
| 11 | ~ statements applied by the authors. All third-party contributions are | |
| 12 | ~ distributed under license by Red Hat Middleware LLC. | |
| 13 | ~ | |
| 14 | ~ This copyrighted material is made available to anyone wishing to use, modify, | |
| 15 | ~ copy, or redistribute it subject to the terms and conditions of the GNU | |
| 16 | ~ Lesser General Public License, as published by the Free Software Foundation. | |
| 17 | ~ | |
| 18 | ~ This program is distributed in the hope that it will be useful, | |
| 19 | ~ but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | |
| 20 | ~ or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License | |
| 21 | ~ for more details. | |
| 22 | ~ | |
| 23 | ~ You should have received a copy of the GNU Lesser General Public License | |
| 24 | ~ along with this distribution; if not, write to: | |
| 25 | ~ Free Software Foundation, Inc. | |
| 26 | ~ 51 Franklin Street, Fifth Floor | |
| 27 | ~ Boston, MA 02110-1301 USA | |
| 28 | ~ | |
| 29 | --> | |
| 30 | ||
| 31 | <hibernate-mapping package="org.hibernate.test.fetchprofiles.join"> | |
| 32 | ||
| 33 | <class name="Department"> | |
| 34 | <id name="id" type="long"> | |
| 35 | <generator class="increment"/> | |
| 36 | </id> | |
| 37 | <property name="code" column="CODE" type="string"/> | |
| 38 | <property name="name" column="NAME" type="string"/> | |
| 39 | </class> | |
| 40 | ||
| 41 | <class name="Student"> | |
| 42 | <id name="id" type="long"> | |
| 43 | <generator class="increment"/> | |
| 44 | </id> | |
| 45 | <property name="name" column="NAME" type="string"/> | |
| 46 | </class> | |
| 47 | ||
| 48 | <class name="Course"> | |
| 49 | <id name="id" type="long"> | |
| 50 | <generator class="increment"/> | |
| 51 | </id> | |
| 52 | <property name="name" column="NAME" type="string"/> | |
| 53 | <component name="code" class="Course$Code"> | |
| 54 | <many-to-one name="department" class="Department" column="DEPT_ID" cascade="save-update"/> | |
| 55 | <property name="number" type="int" column="CODE_NUMBER"/> | |
| 56 | </component> | |
| 57 | <fetch-profile name="course.details"> | |
| 58 | <fetch association="code.department" style="join"/> | |
| 59 | </fetch-profile> | |
| 60 | </class> | |
| 61 | ||
| 62 | <class name="CourseOffering" table="SECTION"> | |
| 63 | <id name="id" type="long"> | |
| 64 | <generator class="increment"/> | |
| 65 | </id> | |
| 66 | <many-to-one name="course" column="COURSE_ID" class="Course"/> | |
| 67 | <property name="semester" type="int" column="SEMESTER"/> | |
| 68 | <property name="year" type="int" column="YEAR"/> | |
| 69 | <set name="enrollments" lazy="true" cascade="all"> | |
| 70 | <key column="SECTION_ID"/> | |
| 71 | <one-to-many class="Enrollment"/> | |
| 72 | </set> | |
| 73 | <fetch-profile name="offering.details"> | |
| 74 | <fetch association="enrollments" style="join"/> | |
| 75 | <fetch association="course" style="join"/> | |
| 76 | </fetch-profile> | |
| 77 | <fetch-profile name="offering.details2"> | |
| 78 | <fetch entity="CourseOffering" association="enrollments" style="join"/> | |
| 79 | </fetch-profile> | |
| 80 | </class> | |
| 81 | ||
| 82 | <class name="Enrollment"> | |
| 83 | <id name="id" type="long"> | |
| 84 | <generator class="increment"/> | |
| 85 | </id> | |
| 86 | <many-to-one name="offering" column="SECTION_ID" class="CourseOffering" cascade="none"/> | |
| 87 | <many-to-one name="student" column="STUDENT_ID" class="Student" cascade="none"/> | |
| 88 | <property name="finalGrade" column="FINAL_GRADE" type="int"/> | |
| 89 | </class> | |
| 90 | ||
| 91 | <fetch-profile name="enrollment.details"> | |
| 92 | <fetch entity="Enrollment" association="student" style="join"/> | |
| 93 | <fetch entity="Enrollment" association="offering" style="join"/> | |
| 94 | </fetch-profile> | |
| 95 | ||
| 96 | </hibernate-mapping> |
| ... | ...@@ -0,0 +1,68 @@ | |
| 1 | /* | |
| 2 | * Hibernate, Relational Persistence for Idiomatic Java | |
| 3 | * | |
| 4 | * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as | |
| 5 | * indicated by the @author tags or express copyright attribution | |
| 6 | * statements applied by the authors. All third-party contributions are | |
| 7 | * distributed under license by Red Hat Middleware LLC. | |
| 8 | * | |
| 9 | * This copyrighted material is made available to anyone wishing to use, modify, | |
| 10 | * copy, or redistribute it subject to the terms and conditions of the GNU | |
| 11 | * Lesser General Public License, as published by the Free Software Foundation. | |
| 12 | * | |
| 13 | * This program is distributed in the hope that it will be useful, | |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | |
| 15 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License | |
| 16 | * for more details. | |
| 17 | * | |
| 18 | * You should have received a copy of the GNU Lesser General Public License | |
| 19 | * along with this distribution; if not, write to: | |
| 20 | * Free Software Foundation, Inc. | |
| 21 | * 51 Franklin Street, Fifth Floor | |
| 22 | * Boston, MA 02110-1301 USA | |
| 23 | * | |
| 24 | */ | |
| 25 | package org.hibernate.test.fetchprofiles.join; | |
| 26 | ||
| 27 | /** | |
| 28 | * TODO : javadoc | |
| 29 | * | |
| 30 | * @author Steve Ebersole | |
| 31 | */ | |
| 32 | public class Department { | |
| 33 | private Long id; | |
| 34 | private String code; | |
| 35 | private String name; | |
| 36 | ||
| 37 | public Department() { | |
| 38 | } | |
| 39 | ||
| 40 | public Department(String code, String name) { | |
| 41 | this.code = code; | |
| 42 | this.name = name; | |
| 43 | } | |
| 44 | ||
| 45 | public Long getId() { | |
| 46 | return id; | |
| 47 | } | |
| 48 | ||
| 49 | public void setId(Long id) { | |
| 50 | this.id = id; | |
| 51 | } | |
| 52 | ||
| 53 | public String getCode() { | |
| 54 | return code; | |
| 55 | } | |
| 56 | ||
| 57 | public void setCode(String code) { | |
| 58 | this.code = code; | |
| 59 | } | |
| 60 | ||
| 61 | public String getName() { | |
| 62 | return name; | |
| 63 | } | |
| 64 | ||
| 65 | public void setName(String name) { | |
| 66 | this.name = name; | |
| 67 | } | |
| 68 | } |
| ... | ...@@ -0,0 +1,58 @@ | |
| 1 | /* | |
| 2 | * Hibernate, Relational Persistence for Idiomatic Java | |
| 3 | * | |
| 4 | * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as | |
| 5 | * indicated by the @author tags or express copyright attribution | |
| 6 | * statements applied by the authors. All third-party contributions are | |
| 7 | * distributed under license by Red Hat Middleware LLC. | |
| 8 | * | |
| 9 | * This copyrighted material is made available to anyone wishing to use, modify, | |
| 10 | * copy, or redistribute it subject to the terms and conditions of the GNU | |
| 11 | * Lesser General Public License, as published by the Free Software Foundation. | |
| 12 | * | |
| 13 | * This program is distributed in the hope that it will be useful, | |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | |
| 15 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License | |
| 16 | * for more details. | |
| 17 | * | |
| 18 | * You should have received a copy of the GNU Lesser General Public License | |
| 19 | * along with this distribution; if not, write to: | |
| 20 | * Free Software Foundation, Inc. | |
| 21 | * 51 Franklin Street, Fifth Floor | |
| 22 | * Boston, MA 02110-1301 USA | |
| 23 | * | |
| 24 | */ | |
| 25 | package org.hibernate.test.fetchprofiles.join; | |
| 26 | ||
| 27 | /** | |
| 28 | * TODO : javadoc | |
| 29 | * | |
| 30 | * @author Steve Ebersole | |
| 31 | */ | |
| 32 | public class Student { | |
| 33 | private Long id; | |
| 34 | private String name; | |
| 35 | ||
| 36 | public Student() { | |
| 37 | } | |
| 38 | ||
| 39 | public Student(String name) { | |
| 40 | this.name = name; | |
| 41 | } | |
| 42 | ||
| 43 | public Long getId() { | |
| 44 | return id; | |
| 45 | } | |
| 46 | ||
| 47 | public void setId(Long id) { | |
| 48 | this.id = id; | |
| 49 | } | |
| 50 | ||
| 51 | public String getName() { | |
| 52 | return name; | |
| 53 | } | |
| 54 | ||
| 55 | public void setName(String name) { | |
| 56 | this.name = name; | |
| 57 | } | |
| 58 | } |
| ... | ...@@ -0,0 +1,311 @@ | |
| 1 | /* | |
| 2 | * Hibernate, Relational Persistence for Idiomatic Java | |
| 3 | * | |
| 4 | * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as | |
| 5 | * indicated by the @author tags or express copyright attribution | |
| 6 | * statements applied by the authors. All third-party contributions are | |
| 7 | * distributed under license by Red Hat Middleware LLC. | |
| 8 | * | |
| 9 | * This copyrighted material is made available to anyone wishing to use, modify, | |
| 10 | * copy, or redistribute it subject to the terms and conditions of the GNU | |
| 11 | * Lesser General Public License, as published by the Free Software Foundation. | |
| 12 | * | |
| 13 | * This program is distributed in the hope that it will be useful, | |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | |
| 15 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License | |
| 16 | * for more details. | |
| 17 | * | |
| 18 | * You should have received a copy of the GNU Lesser General Public License | |
| 19 | * along with this distribution; if not, write to: | |
| 20 | * Free Software Foundation, Inc. | |
| 21 | * 51 Franklin Street, Fifth Floor | |
| 22 | * Boston, MA 02110-1301 USA | |
| 23 | * | |
| 24 | */ | |
| 25 | package org.hibernate.test.fetchprofiles.join; | |
| 26 | ||
| 27 | import java.util.List; | |
| 28 | import java.util.Iterator; | |
| 29 | ||
| 30 | import org.hibernate.junit.functional.FunctionalTestCase; | |
| 31 | import org.hibernate.Session; | |
| 32 | import org.hibernate.Hibernate; | |
| 33 | import org.hibernate.UnknownProfileException; | |
| 34 | import org.hibernate.cfg.Configuration; | |
| 35 | import org.hibernate.cfg.Environment; | |
| 36 | import org.hibernate.engine.SessionImplementor; | |
| 37 | ||
| 38 | /** | |
| 39 | * Various tests related to join-style fetch profiles. | |
| 40 | * | |
| 41 | * @author Steve Ebersole | |
| 42 | */ | |
| 43 | public class JoinFetchProfileTest extends FunctionalTestCase { | |
| 44 | private List sections; | |
| 45 | ||
| 46 | public JoinFetchProfileTest(String string) { | |
| 47 | super( string ); | |
| 48 | } | |
| 49 | ||
| 50 | public String[] getMappings() { | |
| 51 | return new String[] { "fetchprofiles/join/Mappings.hbm.xml" }; | |
| 52 | } | |
| 53 | ||
| 54 | public String getCacheConcurrencyStrategy() { | |
| 55 | return null; | |
| 56 | } | |
| 57 | ||
| 58 | public void configure(Configuration cfg) { | |
| 59 | cfg.setProperty( Environment.GENERATE_STATISTICS, "true" ); | |
| 60 | } | |
| 61 | ||
| 62 | private static interface TestData { | |
| 63 | public Long getStudentId(); | |
| 64 | public Long getDepartmentId(); | |
| 65 | public Long getCourseId(); | |
| 66 | public Long getSectionId(); | |
| 67 | public Long getEnrollmentId(); | |
| 68 | } | |
| 69 | ||
| 70 | private interface TestCode { | |
| 71 | public void perform(TestData data); | |
| 72 | } | |
| 73 | ||
| 74 | private void performWithStandardData(TestCode testCode) { | |
| 75 | Session session = openSession(); | |
| 76 | session.beginTransaction(); | |
| 77 | final Department literatureDepartment = new Department( "lit", "Literature" ); | |
| 78 | session.save( literatureDepartment ); | |
| 79 | final Course lit101 = new Course( new Course.Code( literatureDepartment, 101 ), "Introduction to Literature" ); | |
| 80 | session.save( lit101 ); | |
| 81 | final CourseOffering section = new CourseOffering( lit101, 1, 2008 ); | |
| 82 | session.save( section ); | |
| 83 | final Student me = new Student( "Steve" ); | |
| 84 | session.save( me ); | |
| 85 | final Enrollment enrollment = new Enrollment( section, me ); | |
| 86 | section.getEnrollments().add( enrollment ); | |
| 87 | session.save( enrollment ); | |
| 88 | session.getTransaction().commit(); | |
| 89 | session.close(); | |
| 90 | ||
| 91 | sfi().getStatistics().clear(); | |
| 92 | ||
| 93 | testCode.perform( | |
| 94 | new TestData() { | |
| 95 | public Long getStudentId() { | |
| 96 | return me.getId(); | |
| 97 | } | |
| 98 | ||
| 99 | public Long getDepartmentId() { | |
| 100 | return literatureDepartment.getId(); | |
| 101 | } | |
| 102 | ||
| 103 | public Long getCourseId() { | |
| 104 | return lit101.getId(); | |
| 105 | } | |
| 106 | ||
| 107 | public Long getSectionId() { | |
| 108 | return section.getId(); | |
| 109 | } | |
| 110 | ||
| 111 | public Long getEnrollmentId() { | |
| 112 | return enrollment.getId(); | |
| 113 | } | |
| 114 | } | |
| 115 | ); | |
| 116 | ||
| 117 | session = openSession(); | |
| 118 | session.beginTransaction(); | |
| 119 | session.delete( enrollment ); | |
| 120 | session.delete( me ); | |
| 121 | session.delete( enrollment.getOffering() ); | |
| 122 | session.delete( enrollment.getOffering().getCourse() ); | |
| 123 | session.delete( enrollment.getOffering().getCourse().getCode().getDepartment() ); | |
| 124 | session.getTransaction().commit(); | |
| 125 | session.close(); | |
| 126 | } | |
| 127 | ||
| 128 | public void testNormalLoading() { | |
| 129 | performWithStandardData( | |
| 130 | new TestCode() { | |
| 131 | public void perform(TestData data) { | |
| 132 | Session session = openSession(); | |
| 133 | session.beginTransaction(); | |
| 134 | CourseOffering section = ( CourseOffering ) session.get( CourseOffering.class, data.getSectionId() ); | |
| 135 | assertEquals( 1, sfi().getStatistics().getEntityLoadCount() ); | |
| 136 | assertEquals( 0, sfi().getStatistics().getEntityFetchCount() ); | |
| 137 | assertFalse( Hibernate.isInitialized( section.getCourse() ) ); | |
| 138 | assertFalse( Hibernate.isInitialized( section.getEnrollments() ) ); | |
| 139 | assertFalse( Hibernate.isInitialized( section.getCourse().getCode().getDepartment() ) ); | |
| 140 | assertTrue( Hibernate.isInitialized( section.getCourse() ) ); | |
| 141 | assertEquals( 1, sfi().getStatistics().getEntityFetchCount() ); | |
| 142 | session.getTransaction().commit(); | |
| 143 | session.close(); | |
| 144 | } | |
| 145 | } | |
| 146 | ); | |
| 147 | } | |
| 148 | ||
| 149 | public void testNormalCriteria() { | |
| 150 | performWithStandardData( | |
| 151 | new TestCode() { | |
| 152 | public void perform(TestData data) { | |
| 153 | Session session = openSession(); | |
| 154 | session.beginTransaction(); | |
| 155 | CourseOffering section = ( CourseOffering ) session.createCriteria( CourseOffering.class ).uniqueResult(); | |
| 156 | assertEquals( 1, sfi().getStatistics().getEntityLoadCount() ); | |
| 157 | assertEquals( 0, sfi().getStatistics().getEntityFetchCount() ); | |
| 158 | assertFalse( Hibernate.isInitialized( section.getCourse() ) ); | |
| 159 | assertFalse( Hibernate.isInitialized( section.getEnrollments() ) ); | |
| 160 | assertFalse( Hibernate.isInitialized( section.getCourse().getCode().getDepartment() ) ); | |
| 161 | assertTrue( Hibernate.isInitialized( section.getCourse() ) ); | |
| 162 | assertEquals( 1, sfi().getStatistics().getEntityFetchCount() ); | |
| 163 | session.getTransaction().commit(); | |
| 164 | session.close(); | |
| 165 | } | |
| 166 | } | |
| 167 | ); | |
| 168 | } | |
| 169 | ||
| 170 | public void testBasicFetchProfileOperation() { | |
| 171 | assertTrue( "fetch profile not parsed properly", sfi().containsFetchProfileDefition( "enrollment.details" ) ); | |
| 172 | assertTrue( "fetch profile not parsed properly", sfi().containsFetchProfileDefition( "offering.details" ) ); | |
| 173 | assertTrue( "fetch profile not parsed properly", sfi().containsFetchProfileDefition( "course.details" ) ); | |
| 174 | Session s = openSession(); | |
| 175 | SessionImplementor si = ( SessionImplementor ) s; | |
| 176 | s.enableFetchProfile( "enrollment.details" ); | |
| 177 | assertTrue( si.getLoadQueryInfluencers().hasEnabledFetchProfiles() ); | |
| 178 | s.disableFetchProfile( "enrollment.details" ); | |
| 179 | assertFalse( si.getLoadQueryInfluencers().hasEnabledFetchProfiles() ); | |
| 180 | try { | |
| 181 | s.enableFetchProfile( "never-gonna-get-it" ); | |
| 182 | fail( "expecting failure on undefined fetch-profile" ); | |
| 183 | } | |
| 184 | catch ( UnknownProfileException expected ) { | |
| 185 | } | |
| 186 | s.close(); | |
| 187 | } | |
| 188 | ||
| 189 | public void testLoadManyToOneFetchProfile() { | |
| 190 | performWithStandardData( | |
| 191 | new TestCode() { | |
| 192 | public void perform(TestData data) { | |
| 193 | Session session = openSession(); | |
| 194 | session.beginTransaction(); | |
| 195 | session.enableFetchProfile( "enrollment.details" ); | |
| 196 | Enrollment enrollment = ( Enrollment ) session.get( Enrollment.class, data.getEnrollmentId() ); | |
| 197 | assertEquals( 3, sfi().getStatistics().getEntityLoadCount() ); // enrollment + (section + student) | |
| 198 | assertEquals( 0, sfi().getStatistics().getEntityFetchCount() ); | |
| 199 | assertTrue( Hibernate.isInitialized( enrollment.getOffering() ) ); | |
| 200 | assertTrue( Hibernate.isInitialized( enrollment.getStudent() ) ); | |
| 201 | assertEquals( 0, sfi().getStatistics().getEntityFetchCount() ); | |
| 202 | session.getTransaction().commit(); | |
| 203 | session.close(); | |
| 204 | } | |
| 205 | } | |
| 206 | ); | |
| 207 | } | |
| 208 | ||
| 209 | public void testCriteriaManyToOneFetchProfile() { | |
| 210 | performWithStandardData( | |
| 211 | new TestCode() { | |
| 212 | public void perform(TestData data) { | |
| 213 | Session session = openSession(); | |
| 214 | session.beginTransaction(); | |
| 215 | session.enableFetchProfile( "enrollment.details" ); | |
| 216 | Enrollment enrollment = ( Enrollment ) session.createCriteria( Enrollment.class ).uniqueResult(); | |
| 217 | assertEquals( 3, sfi().getStatistics().getEntityLoadCount() ); // enrollment + (section + student) | |
| 218 | assertEquals( 0, sfi().getStatistics().getEntityFetchCount() ); | |
| 219 | assertTrue( Hibernate.isInitialized( enrollment.getOffering() ) ); | |
| 220 | assertTrue( Hibernate.isInitialized( enrollment.getStudent() ) ); | |
| 221 | assertEquals( 0, sfi().getStatistics().getEntityFetchCount() ); | |
| 222 | session.getTransaction().commit(); | |
| 223 | session.close(); | |
| 224 | } | |
| 225 | } | |
| 226 | ); | |
| 227 | } | |
| 228 | ||
| 229 | public void testLoadOneToManyFetchProfile() { | |
| 230 | performWithStandardData( | |
| 231 | new TestCode() { | |
| 232 | public void perform(TestData data) { | |
| 233 | Session session = openSession(); | |
| 234 | session.beginTransaction(); | |
| 235 | session.enableFetchProfile( "offering.details" ); | |
| 236 | CourseOffering section = ( CourseOffering ) session.get( CourseOffering.class, data.getSectionId() ); | |
| 237 | assertEquals( 3, sfi().getStatistics().getEntityLoadCount() ); // section + (enrollments + course) | |
| 238 | assertEquals( 0, sfi().getStatistics().getEntityFetchCount() ); | |
| 239 | assertTrue( Hibernate.isInitialized( section.getEnrollments() ) ); | |
| 240 | session.getTransaction().commit(); | |
| 241 | session.close(); | |
| 242 | } | |
| 243 | } | |
| 244 | ); | |
| 245 | } | |
| 246 | ||
| 247 | public void testLoadDeepFetchProfile() { | |
| 248 | performWithStandardData( | |
| 249 | new TestCode() { | |
| 250 | public void perform(TestData data) { | |
| 251 | Session session = openSession(); | |
| 252 | session.beginTransaction(); | |
| 253 | // enable both enrollment and offering detail profiles; | |
| 254 | // then loading the section/offering should fetch the enrollment | |
| 255 | // which in turn should fetch student (+ offering). | |
| 256 | session.enableFetchProfile( "offering.details" ); | |
| 257 | session.enableFetchProfile( "enrollment.details" ); | |
| 258 | CourseOffering section = ( CourseOffering ) session.get( CourseOffering.class, data.getSectionId() ); | |
| 259 | assertEquals( 4, sfi().getStatistics().getEntityLoadCount() ); // section + (course + enrollments + (student)) | |
| 260 | assertEquals( 0, sfi().getStatistics().getEntityFetchCount() ); | |
| 261 | assertTrue( Hibernate.isInitialized( section.getEnrollments() ) ); | |
| 262 | session.getTransaction().commit(); | |
| 263 | session.close(); | |
| 264 | } | |
| 265 | } | |
| 266 | ); | |
| 267 | } | |
| 268 | ||
| 269 | public void testLoadComponentDerefFetchProfile() { | |
| 270 | performWithStandardData( | |
| 271 | new TestCode() { | |
| 272 | public void perform(TestData data) { | |
| 273 | Session session = openSession(); | |
| 274 | session.beginTransaction(); | |
| 275 | session.enableFetchProfile( "course.details" ); | |
| 276 | Course course = ( Course ) session.get( Course.class, data.getCourseId() ); | |
| 277 | assertEquals( 2, sfi().getStatistics().getEntityLoadCount() ); // course + department | |
| 278 | assertEquals( 0, sfi().getStatistics().getEntityFetchCount() ); | |
| 279 | assertTrue( Hibernate.isInitialized( course.getCode().getDepartment() ) ); | |
| 280 | session.getTransaction().commit(); | |
| 281 | session.close(); | |
| 282 | } | |
| 283 | } | |
| 284 | ); | |
| 285 | } | |
| 286 | ||
| 287 | /** | |
| 288 | * fetch-profiles should have no effect what-so-ever on the direct results of the HQL query. | |
| 289 | * | |
| 290 | * TODO : this is actually not strictly true. what we should have happen is to subsequently load those fetches | |
| 291 | */ | |
| 292 | public void testHQL() { | |
| 293 | performWithStandardData( | |
| 294 | new TestCode() { | |
| 295 | public void perform(TestData data) { | |
| 296 | Session session = openSession(); | |
| 297 | session.beginTransaction(); | |
| 298 | session.enableFetchProfile( "offering.details" ); | |
| 299 | session.enableFetchProfile( "enrollment.details" ); | |
| 300 | List sections = session.createQuery( "from CourseOffering" ).list(); | |
| 301 | int sectionCount = sections.size(); | |
| 302 | assertEquals( "unexpected CourseOffering count", 1, sectionCount ); | |
| 303 | assertEquals( 1, sfi().getStatistics().getEntityLoadCount() ); | |
| 304 | assertEquals( 0, sfi().getStatistics().getEntityFetchCount() ); | |
| 305 | session.getTransaction().commit(); | |
| 306 | session.close(); | |
| 307 | } | |
| 308 | } | |
| 309 | ); | |
| 310 | } | |
| 311 | } |
| ... | ...@@ -0,0 +1,77 @@ | |
| 1 | /* | |
| 2 | * Hibernate, Relational Persistence for Idiomatic Java | |
| 3 | * | |
| 4 | * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as | |
| 5 | * indicated by the @author tags or express copyright attribution | |
| 6 | * statements applied by the authors. All third-party contributions are | |
| 7 | * distributed under license by Red Hat Middleware LLC. | |
| 8 | * | |
| 9 | * This copyrighted material is made available to anyone wishing to use, modify, | |
| 10 | * copy, or redistribute it subject to the terms and conditions of the GNU | |
| 11 | * Lesser General Public License, as published by the Free Software Foundation. | |
| 12 | * | |
| 13 | * This program is distributed in the hope that it will be useful, | |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | |
| 15 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License | |
| 16 | * for more details. | |
| 17 | * | |
| 18 | * You should have received a copy of the GNU Lesser General Public License | |
| 19 | * along with this distribution; if not, write to: | |
| 20 | * Free Software Foundation, Inc. | |
| 21 | * 51 Franklin Street, Fifth Floor | |
| 22 | * Boston, MA 02110-1301 USA | |
| 23 | * | |
| 24 | */ | |
| 25 | package org.hibernate.test.fetchprofiles.join; | |
| 26 | ||
| 27 | /** | |
| 28 | * TODO : javadoc | |
| 29 | * | |
| 30 | * @author Steve Ebersole | |
| 31 | */ | |
| 32 | public class Enrollment { | |
| 33 | private Long id; | |
| 34 | private CourseOffering offering; | |
| 35 | private Student student; | |
| 36 | private int finalGrade; | |
| 37 | ||
| 38 | public Enrollment() { | |
| 39 | } | |
| 40 | ||
| 41 | public Enrollment(CourseOffering offering, Student student) { | |
| 42 | this.offering = offering; | |
| 43 | this.student = student; | |
| 44 | } | |
| 45 | ||
| 46 | public Long getId() { | |
| 47 | return id; | |
| 48 | } | |
| 49 | ||
| 50 | public void setId(Long id) { | |
| 51 | this.id = id; | |
| 52 | } | |
| 53 | ||
| 54 | public CourseOffering getOffering() { | |
| 55 | return offering; | |
| 56 | } | |
| 57 | ||
| 58 | public void setOffering(CourseOffering offering) { | |
| 59 | this.offering = offering; | |
| 60 | } | |
| 61 | ||
| 62 | public Student getStudent() { | |
| 63 | return student; | |
| 64 | } | |
| 65 | ||
| 66 | public void setStudent(Student student) { | |
| 67 | this.student = student; | |
| 68 | } | |
| 69 | ||
| 70 | public int getFinalGrade() { | |
| 71 | return finalGrade; | |
| 72 | } | |
| 73 | ||
| 74 | public void setFinalGrade(int finalGrade) { | |
| 75 | this.finalGrade = finalGrade; | |
| 76 | } | |
| 77 | } |
| ... | ...@@ -0,0 +1,123 @@ | |
| 1 | /* | |
| 2 | * Hibernate, Relational Persistence for Idiomatic Java | |
| 3 | * | |
| 4 | * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as | |
| 5 | * indicated by the @author tags or express copyright attribution | |
| 6 | * statements applied by the authors. All third-party contributions are | |
| 7 | * distributed under license by Red Hat Middleware LLC. | |
| 8 | * | |
| 9 | * This copyrighted material is made available to anyone wishing to use, modify, | |
| 10 | * copy, or redistribute it subject to the terms and conditions of the GNU | |
| 11 | * Lesser General Public License, as published by the Free Software Foundation. | |
| 12 | * | |
| 13 | * This program is distributed in the hope that it will be useful, | |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | |
| 15 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License | |
| 16 | * for more details. | |
| 17 | * | |
| 18 | * You should have received a copy of the GNU Lesser General Public License | |
| 19 | * along with this distribution; if not, write to: | |
| 20 | * Free Software Foundation, Inc. | |
| 21 | * 51 Franklin Street, Fifth Floor | |
| 22 | * Boston, MA 02110-1301 USA | |
| 23 | * | |
| 24 | */ | |
| 25 | package org.hibernate.test.fetchprofiles.join; | |
| 26 | ||
| 27 | /** | |
| 28 | * TODO : javadoc | |
| 29 | * | |
| 30 | * @author Steve Ebersole | |
| 31 | */ | |
| 32 | public class Course { | |
| 33 | private Long id; | |
| 34 | private Code code; | |
| 35 | private String name; | |
| 36 | ||
| 37 | public Course() { | |
| 38 | } | |
| 39 | ||
| 40 | public Course(Code code, String name) { | |
| 41 | this.code = code; | |
| 42 | this.name = name; | |
| 43 | } | |
| 44 | ||
| 45 | public Long getId() { | |
| 46 | return id; | |
| 47 | } | |
| 48 | ||
| 49 | public void setId(Long id) { | |
| 50 | this.id = id; | |
| 51 | } | |
| 52 | ||
| 53 | public Code getCode() { | |
| 54 | return code; | |
| 55 | } | |
| 56 | ||
| 57 | public void setCode(Code code) { | |
| 58 | this.code = code; | |
| 59 | } | |
| 60 | ||
| 61 | public String getName() { | |
| 62 | return name; | |
| 63 | } | |
| 64 | ||
| 65 | public void setName(String name) { | |
| 66 | this.name = name; | |
| 67 | } | |
| 68 | public static class Code { | |
| 69 | private Department department; | |
| 70 | private int number; | |
| 71 | ||
| 72 | public Code() { | |
| 73 | } | |
| 74 | ||
| 75 | public Code(Department department, int number) { | |
| 76 | this.department = department; | |
| 77 | this.number = number; | |
| 78 | } | |
| 79 | ||
| 80 | public Department getDepartment() { | |
| 81 | return department; | |
| 82 | } | |
| 83 | ||
| 84 | public void setDepartment(Department department) { | |
| 85 | this.department = department; | |
| 86 | } | |
| 87 | ||
| 88 | public int getNumber() { | |
| 89 | return number; | |
| 90 | } | |
| 91 | ||
| 92 | public void setNumber(int number) { | |
| 93 | this.number = number; | |
| 94 | } | |
| 95 | ||
| 96 | public boolean equals(Object o) { | |
| 97 | if ( this == o ) { | |
| 98 | return true; | |
| 99 | } | |
| 100 | if ( !( o instanceof Code ) ) { | |
| 101 | return false; | |
| 102 | } | |
| 103 | ||
| 104 | Code code = ( Code ) o; | |
| 105 | ||
| 106 | if ( number != code.number ) { | |
| 107 | return false; | |
| 108 | } | |
| 109 | if ( !department.equals( code.department ) ) { | |
| 110 | return false; | |
| 111 | } | |
| 112 | ||
| 113 | return true; | |
| 114 | } | |
| 115 | ||
| 116 | public int hashCode() { | |
| 117 | int result; | |
| 118 | result = department.hashCode(); | |
| 119 | result = 31 * result + number; | |
| 120 | return result; | |
| 121 | } | |
| 122 | } | |
| 123 | } |
| ... | ...@@ -0,0 +1,90 @@ | |
| 1 | /* | |
| 2 | * Hibernate, Relational Persistence for Idiomatic Java | |
| 3 | * | |
| 4 | * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as | |
| 5 | * indicated by the @author tags or express copyright attribution | |
| 6 | * statements applied by the authors. All third-party contributions are | |
| 7 | * distributed under license by Red Hat Middleware LLC. | |
| 8 | * | |
| 9 | * This copyrighted material is made available to anyone wishing to use, modify, | |
| 10 | * copy, or redistribute it subject to the terms and conditions of the GNU | |
| 11 | * Lesser General Public License, as published by the Free Software Foundation. | |
| 12 | * | |
| 13 | * This program is distributed in the hope that it will be useful, | |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | |
| 15 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License | |
| 16 | * for more details. | |
| 17 | * | |
| 18 | * You should have received a copy of the GNU Lesser General Public License | |
| 19 | * along with this distribution; if not, write to: | |
| 20 | * Free Software Foundation, Inc. | |
| 21 | * 51 Franklin Street, Fifth Floor | |
| 22 | * Boston, MA 02110-1301 USA | |
| 23 | * | |
| 24 | */ | |
| 25 | package org.hibernate.test.fetchprofiles.join; | |
| 26 | ||
| 27 | import java.util.Set; | |
| 28 | import java.util.HashSet; | |
| 29 | ||
| 30 | /** | |
| 31 | * TODO : javadoc | |
| 32 | * | |
| 33 | * @author Steve Ebersole | |
| 34 | */ | |
| 35 | public class CourseOffering { | |
| 36 | private Long id; | |
| 37 | private Course course; | |
| 38 | private int semester; | |
| 39 | private int year; | |
| 40 | private Set enrollments = new HashSet(); | |
| 41 | ||
| 42 | public CourseOffering() { | |
| 43 | } | |
| 44 | ||
| 45 | public CourseOffering(Course course, int semester, int year) { | |
| 46 | this.course = course; | |
| 47 | this.semester = semester; | |
| 48 | this.year = year; | |
| 49 | } | |
| 50 | ||
| 51 | public Long getId() { | |
| 52 | return id; | |
| 53 | } | |
| 54 | ||
| 55 | public void setId(Long id) { | |
| 56 | this.id = id; | |
| 57 | } | |
| 58 | ||
| 59 | public Course getCourse() { | |
| 60 | return course; | |
| 61 | } | |
| 62 | ||
| 63 | public void setCourse(Course course) { | |
| 64 | this.course = course; | |
| 65 | } | |
| 66 | ||
| 67 | public int getSemester() { | |
| 68 | return semester; | |
| 69 | } | |
| 70 | ||
| 71 | public void setSemester(int semester) { | |
| 72 | this.semester = semester; | |
| 73 | } | |
| 74 | ||
| 75 | public int getYear() { | |
| 76 | return year; | |
| 77 | } | |
| 78 | ||
| 79 | public void setYear(int year) { | |
| 80 | this.year = year; | |
| 81 | } | |
| 82 | ||
| 83 | public Set getEnrollments() { | |
| 84 | return enrollments; | |
| 85 | } | |
| 86 | ||
| 87 | public void setEnrollments(Set enrollments) { | |
| 88 | this.enrollments = enrollments; | |
| 89 | } | |
| 90 | } |