From 26c911515c600bfb3344cd589dbdbd8ff955f4bf Mon Sep 17 00:00:00 2001 From: Gail Badner Date: Fri, 3 Aug 2018 01:22:48 -0700 Subject: [PATCH] HHH-12875 HHH-12882 : test cases (cherry picked from commit d75276bf60f0da0fa0b4681a24e8b0845be7ba7a) --- .../LazyManyToManyNonUniqueIdWhereTest.java | 444 ++++++++++++++++++ .../LazyOneToManyNonUniqueIdWhereTest.java | 302 ++++++++++++ .../test/where/{ => hbm}/File.hbm.xml | 2 +- .../hibernate/test/where/{ => hbm}/File.java | 2 +- ...ToManyNonUniqueIdNotFoundWhereTest.hbm.xml | 2 +- ...anyToManyNonUniqueIdNotFoundWhereTest.java | 4 +- ...LazyManyToManyNonUniqueIdWhereTest.hbm.xml | 16 +- .../LazyManyToManyNonUniqueIdWhereTest.java | 97 ++-- .../LazyOneToManyNonUniqueIdWhereTest.hbm.xml | 64 +++ .../LazyOneToManyNonUniqueIdWhereTest.java | 257 ++++++++++ .../test/where/{ => hbm}/WhereTest.java | 7 +- 11 files changed, 1155 insertions(+), 42 deletions(-) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/where/annotations/LazyManyToManyNonUniqueIdWhereTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/where/annotations/LazyOneToManyNonUniqueIdWhereTest.java rename hibernate-core/src/test/java/org/hibernate/test/where/{ => hbm}/File.hbm.xml (93%) rename hibernate-core/src/test/java/org/hibernate/test/where/{ => hbm}/File.java (96%) rename hibernate-core/src/test/java/org/hibernate/test/where/{ => hbm}/LazyManyToManyNonUniqueIdNotFoundWhereTest.hbm.xml (97%) rename hibernate-core/src/test/java/org/hibernate/test/where/{ => hbm}/LazyManyToManyNonUniqueIdNotFoundWhereTest.java (98%) rename hibernate-core/src/test/java/org/hibernate/test/where/{ => hbm}/LazyManyToManyNonUniqueIdWhereTest.hbm.xml (84%) rename hibernate-core/src/test/java/org/hibernate/test/where/{ => hbm}/LazyManyToManyNonUniqueIdWhereTest.java (71%) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/where/hbm/LazyOneToManyNonUniqueIdWhereTest.hbm.xml create mode 100644 hibernate-core/src/test/java/org/hibernate/test/where/hbm/LazyOneToManyNonUniqueIdWhereTest.java rename hibernate-core/src/test/java/org/hibernate/test/where/{ => hbm}/WhereTest.java (91%) diff --git a/hibernate-core/src/test/java/org/hibernate/test/where/annotations/LazyManyToManyNonUniqueIdWhereTest.java b/hibernate-core/src/test/java/org/hibernate/test/where/annotations/LazyManyToManyNonUniqueIdWhereTest.java new file mode 100644 index 0000000000..c21c0ddaf2 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/where/annotations/LazyManyToManyNonUniqueIdWhereTest.java @@ -0,0 +1,444 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.test.where.annotations; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Set; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.JoinTable; +import javax.persistence.ManyToMany; +import javax.persistence.Table; + +import org.hibernate.Hibernate; +import org.hibernate.annotations.Immutable; +import org.hibernate.annotations.Where; +import org.hibernate.annotations.WhereJoinTable; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +/** + * @author Gail Badner + */ +public class LazyManyToManyNonUniqueIdWhereTest extends BaseCoreFunctionalTestCase { + + protected Class[] getAnnotatedClasses() { + return new Class[] { Material.class, Building.class, Rating.class, Size.class }; + } + + @Before + public void setup() { + doInHibernate( + this::sessionFactory, session -> { + + session.createSQLQuery( "DROP TABLE MAIN_TABLE" ).executeUpdate(); + session.createSQLQuery( "DROP TABLE ASSOCIATION_TABLE" ).executeUpdate(); + session.createSQLQuery( "DROP TABLE MATERIAL_RATINGS" ).executeUpdate(); + session.createSQLQuery( "DROP TABLE BUILDING_RATINGS" ).executeUpdate(); + + session.createSQLQuery( + "create table MAIN_TABLE( " + + "ID integer not null, NAME varchar(255) not null, CODE varchar(10) not null, " + + "primary key (ID, CODE) )" + ).executeUpdate(); + + session.createSQLQuery( "insert into MAIN_TABLE(ID, NAME, CODE) VALUES( 1, 'plastic', 'MATERIAL' )" ) + .executeUpdate(); + session.createSQLQuery( "insert into MAIN_TABLE(ID, NAME, CODE) VALUES( 1, 'house', 'BUILDING' )" ) + .executeUpdate(); + session.createSQLQuery( "insert into MAIN_TABLE(ID, NAME, CODE) VALUES( 1, 'high', 'RATING' )" ) + .executeUpdate(); + session.createSQLQuery( "insert into MAIN_TABLE(ID, NAME, CODE) VALUES( 2, 'medium', 'RATING' )" ) + .executeUpdate(); + session.createSQLQuery( "insert into MAIN_TABLE(ID, NAME, CODE) VALUES( 3, 'low', 'RATING' )" ) + .executeUpdate(); + session.createSQLQuery( "insert into MAIN_TABLE(ID, NAME, CODE) VALUES( 1, 'small', 'SIZE' )" ) + .executeUpdate(); + session.createSQLQuery( "insert into MAIN_TABLE(ID, NAME, CODE) VALUES( 2, 'medium', 'SIZE' )" ) + .executeUpdate(); + + session.createSQLQuery( + "create table ASSOCIATION_TABLE( " + + "MAIN_ID integer not null, MAIN_CODE varchar(10) not null, " + + "ASSOCIATION_ID int not null, ASSOCIATION_CODE varchar(10) not null, " + + "primary key (MAIN_ID, MAIN_CODE, ASSOCIATION_ID, ASSOCIATION_CODE))" + ).executeUpdate(); + + session.createSQLQuery( + "insert into ASSOCIATION_TABLE(MAIN_ID, MAIN_CODE, ASSOCIATION_ID, ASSOCIATION_CODE) " + + "VALUES( 1, 'MATERIAL', 1, 'RATING' )" + ).executeUpdate(); + session.createSQLQuery( + "insert into ASSOCIATION_TABLE(MAIN_ID, MAIN_CODE, ASSOCIATION_ID, ASSOCIATION_CODE) " + + "VALUES( 1, 'MATERIAL', 2, 'RATING' )" + ).executeUpdate(); + session.createSQLQuery( + "insert into ASSOCIATION_TABLE(MAIN_ID, MAIN_CODE, ASSOCIATION_ID, ASSOCIATION_CODE) " + + "VALUES( 1, 'MATERIAL', 3, 'RATING' )" + ).executeUpdate(); + + session.createSQLQuery( + "insert into ASSOCIATION_TABLE(MAIN_ID, MAIN_CODE, ASSOCIATION_ID, ASSOCIATION_CODE) " + + "VALUES( 1, 'MATERIAL', 2, 'SIZE' )" + ).executeUpdate(); + + session.createSQLQuery( + "insert into ASSOCIATION_TABLE(MAIN_ID, MAIN_CODE, ASSOCIATION_ID, ASSOCIATION_CODE) " + + "VALUES( 1, 'BUILDING', 1, 'RATING' )" + ).executeUpdate(); + + session.createSQLQuery( + "insert into ASSOCIATION_TABLE(MAIN_ID, MAIN_CODE, ASSOCIATION_ID, ASSOCIATION_CODE) " + + "VALUES( 1, 'BUILDING', 1, 'SIZE' )" + ).executeUpdate(); + + + session.createSQLQuery( + "create table MATERIAL_RATINGS( " + + "MATERIAL_ID integer not null, RATING_ID integer not null," + + " primary key (MATERIAL_ID, RATING_ID))" + ).executeUpdate(); + + session.createSQLQuery( + "insert into MATERIAL_RATINGS(MATERIAL_ID, RATING_ID) VALUES( 1, 1 )" + ).executeUpdate(); + + session.createSQLQuery( + "create table BUILDING_RATINGS( " + + "BUILDING_ID integer not null, RATING_ID integer not null," + + " primary key (BUILDING_ID, RATING_ID))" + ).executeUpdate(); + + session.createSQLQuery( + "insert into BUILDING_RATINGS(BUILDING_ID, RATING_ID) VALUES( 1, 1 )" + ).executeUpdate(); + session.createSQLQuery( + "insert into BUILDING_RATINGS(BUILDING_ID, RATING_ID) VALUES( 1, 2 )" + ).executeUpdate(); + session.createSQLQuery( + "insert into BUILDING_RATINGS(BUILDING_ID, RATING_ID) VALUES( 1, 3 )" + ).executeUpdate(); + } + ); + } + + @After + public void cleanup() { + doInHibernate( + this::sessionFactory, session -> { + session.createSQLQuery( "delete from MATERIAL_RATINGS" ).executeUpdate(); + session.createSQLQuery( "delete from BUILDING_RATINGS" ).executeUpdate(); + session.createSQLQuery( "delete from ASSOCIATION_TABLE" ).executeUpdate(); + session.createSQLQuery( "delete from MAIN_TABLE" ).executeUpdate(); + } + ); + } + + @Test + @TestForIssue( jiraKey = "HHH-12875") + public void testInitializeFromUniqueAssociationTable() { + doInHibernate( + this::sessionFactory, session -> { + + Material material = session.get( Material.class, 1 ); + assertEquals( "plastic", material.getName() ); + + // Material#ratings is mapped with lazy="true" + assertFalse( Hibernate.isInitialized( material.getRatings() ) ); + assertEquals( 1, material.getRatings().size() ); + assertTrue( Hibernate.isInitialized( material.getRatings() ) ); + + final Rating rating = material.getRatings().iterator().next(); + assertEquals( "high", rating.getName() ); + + Building building = session.get( Building.class, 1 ); + assertEquals( "house", building.getName() ); + + // Building#ratings is mapped with lazy="true" + assertFalse( Hibernate.isInitialized( building.getMediumOrHighRatings() ) ); + checkMediumOrHighRatings( building.getMediumOrHighRatings() ); + } + ); + } + + @Test + @TestForIssue( jiraKey = "HHH-12875") + public void testInitializeFromNonUniqueAssociationTable() { + doInHibernate( + this::sessionFactory, session -> { + + Material material = session.get( Material.class, 1 ); + assertEquals( "plastic", material.getName() ); + + // Material#mediumOrHighRatingsFromCombined is mapped with lazy="true" + assertFalse( Hibernate.isInitialized( material.getMediumOrHighRatingsFromCombined() ) ); + checkMediumOrHighRatings( material.getMediumOrHighRatingsFromCombined() ); + Rating highRating = null; + for ( Rating rating : material.getMediumOrHighRatingsFromCombined() ) { + if ( "high".equals( rating.getName() ) ) { + highRating = rating; + } + } + assertNotNull( highRating ); + + // Material#sizesFromCombined is mapped with lazy="true" + assertFalse( Hibernate.isInitialized( material.getSizesFromCombined() ) ); + assertEquals( 1, material.getSizesFromCombined().size() ); + assertTrue( Hibernate.isInitialized( material.getSizesFromCombined() ) ); + + final Size size = material.getSizesFromCombined().iterator().next(); + assertEquals( "medium", size.getName() ); + + Building building = session.get( Building.class, 1 ); + + // building.ratingsFromCombined is mapped with lazy="true" + assertFalse( Hibernate.isInitialized( building.getRatingsFromCombined() ) ); + assertEquals( 1, building.getRatingsFromCombined().size() ); + assertTrue( Hibernate.isInitialized( building.getRatingsFromCombined() ) ); + assertSame( highRating, building.getRatingsFromCombined().iterator().next() ); + + // Building#sizesFromCombined is mapped with lazy="true" + assertFalse( Hibernate.isInitialized( building.getSizesFromCombined() ) ); + assertEquals( 1, building.getSizesFromCombined().size() ); + assertTrue( Hibernate.isInitialized( building.getSizesFromCombined() ) ); + assertEquals( "small", building.getSizesFromCombined().iterator().next().getName() ); + } + ); + } + + private void checkMediumOrHighRatings(List mediumOrHighRatings) { + assertEquals( 2, mediumOrHighRatings.size() ); + + final Iterator iterator = mediumOrHighRatings.iterator(); + final Rating firstRating = iterator.next(); + final Rating secondRating = iterator.next(); + if ( "high".equals( firstRating.getName() ) ) { + assertEquals( "medium", secondRating.getName() ); + } + else if ( "medium".equals( firstRating.getName() ) ) { + assertEquals( "high", secondRating.getName() ); + } + else { + fail( "unexpected rating" ); + } + } + + @Entity( name = "Material" ) + @Table( name = "MAIN_TABLE" ) + @Where( clause = "CODE = 'MATERIAL'" ) + public static class Material { + private int id; + + private String name; + private Set sizesFromCombined = new HashSet<>(); + private List mediumOrHighRatingsFromCombined = new ArrayList<>(); + private Set ratings = new HashSet<>(); + + @Id + @Column( name = "ID" ) + public int getId() { + return id; + } + public void setId(int id) { + this.id = id; + } + + @Column( name = "NAME") + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + + @ManyToMany + @JoinTable( + name = "ASSOCIATION_TABLE", + joinColumns = { @JoinColumn( name = "MAIN_ID" ) }, + inverseJoinColumns = { @JoinColumn( name = "ASSOCIATION_ID" ) } + ) + @WhereJoinTable( clause = "MAIN_CODE='MATERIAL' AND ASSOCIATION_CODE='SIZE'") + @Immutable + public Set getSizesFromCombined() { + return sizesFromCombined; + } + public void setSizesFromCombined(Set sizesFromCombined) { + this.sizesFromCombined = sizesFromCombined; + } + + @ManyToMany + @JoinTable( + name = "ASSOCIATION_TABLE", + joinColumns = { @JoinColumn( name = "MAIN_ID" ) }, + inverseJoinColumns = { @JoinColumn( name = "ASSOCIATION_ID" ) } + ) + @WhereJoinTable( clause = "MAIN_CODE='MATERIAL' AND ASSOCIATION_CODE='RATING'" ) + @Where( clause = "name = 'high' or name = 'medium'" ) + @Immutable + public List getMediumOrHighRatingsFromCombined() { + return mediumOrHighRatingsFromCombined; + } + public void setMediumOrHighRatingsFromCombined(List mediumOrHighRatingsFromCombined) { + this.mediumOrHighRatingsFromCombined = mediumOrHighRatingsFromCombined; + } + + @ManyToMany + @JoinTable( + name = "MATERIAL_RATINGS", + joinColumns = { @JoinColumn( name = "MATERIAL_ID") }, + inverseJoinColumns = { @JoinColumn( name = "RATING_ID" ) } + ) + @Immutable + public Set getRatings() { + return ratings; + } + public void setRatings(Set ratings) { + this.ratings = ratings; + } + } + + @Entity( name = "Building" ) + @Table( name = "MAIN_TABLE" ) + @Where( clause = "CODE = 'BUILDING'" ) + public static class Building { + private int id; + private String name; + private Set sizesFromCombined = new HashSet<>(); + private Set ratingsFromCombined = new HashSet<>(); + private List mediumOrHighRatings = new ArrayList<>(); + + @Id + @Column( name = "ID" ) + public int getId() { + return id; + } + public void setId(int id) { + this.id = id; + } + + @Column( name = "NAME") + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + + @ManyToMany + @JoinTable( + name = "ASSOCIATION_TABLE", + joinColumns = { @JoinColumn( name = "MAIN_ID" ) }, + inverseJoinColumns = { @JoinColumn( name = "ASSOCIATION_ID" ) } + ) + @WhereJoinTable( clause = "MAIN_CODE='BUILDING' AND ASSOCIATION_CODE='RATING'") + @Immutable + public Set getSizesFromCombined() { + return sizesFromCombined; + } + public void setSizesFromCombined(Set sizesFromCombined) { + this.sizesFromCombined = sizesFromCombined; + } + + @ManyToMany + @JoinTable( + name = "ASSOCIATION_TABLE", + joinColumns = { @JoinColumn( name = "MAIN_ID" ) }, + inverseJoinColumns = { @JoinColumn( name = "ASSOCIATION_ID" ) } + ) + @WhereJoinTable( clause = "MAIN_CODE='BUILDING' AND ASSOCIATION_CODE='RATING'" ) + @Immutable + public Set getRatingsFromCombined() { + return ratingsFromCombined; + } + public void setRatingsFromCombined(Set ratingsFromCombined) { + this.ratingsFromCombined = ratingsFromCombined; + } + + @ManyToMany + @JoinTable( + name = "BUILDING_RATINGS", + joinColumns = { @JoinColumn( name = "BUILDING_ID") }, + inverseJoinColumns = { @JoinColumn( name = "RATING_ID" ) } + ) + @Where( clause = "name = 'high' or name = 'medium'" ) + @Immutable + public List getMediumOrHighRatings() { + return mediumOrHighRatings; + } + public void setMediumOrHighRatings(List mediumOrHighRatings) { + this.mediumOrHighRatings = mediumOrHighRatings; + } + } + + @Entity( name = "Size" ) + @Table( name = "MAIN_TABLE" ) + @Where( clause = "CODE = 'SIZE'" ) + public static class Size { + private int id; + private String name; + + @Id + @Column( name = "ID" ) + public int getId() { + return id; + } + public void setId(int id) { + this.id = id; + } + + @Column( name = "NAME") + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + } + + @Entity( name = "Rating" ) + @Table( name = "MAIN_TABLE" ) + @Where( clause = "CODE = 'RATING'" ) + public static class Rating { + private int id; + private String name; + + @Id + @Column( name = "ID" ) + public int getId() { + return id; + } + public void setId(int id) { + this.id = id; + } + + @Column( name = "NAME") + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/where/annotations/LazyOneToManyNonUniqueIdWhereTest.java b/hibernate-core/src/test/java/org/hibernate/test/where/annotations/LazyOneToManyNonUniqueIdWhereTest.java new file mode 100644 index 0000000000..5ce2b86daf --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/where/annotations/LazyOneToManyNonUniqueIdWhereTest.java @@ -0,0 +1,302 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.test.where.annotations; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Set; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.OneToMany; +import javax.persistence.Table; + +import org.hibernate.Hibernate; +import org.hibernate.annotations.Immutable; +import org.hibernate.annotations.Where; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +/** + * @author Gail Badner + */ +public class LazyOneToManyNonUniqueIdWhereTest extends BaseCoreFunctionalTestCase { + + protected Class[] getAnnotatedClasses() { + return new Class[] { Material.class, Building.class, Rating.class, Size.class }; + } + + @Before + public void setup() { + doInHibernate( + this::sessionFactory, session -> { + + session.createSQLQuery( "DROP TABLE MAIN_TABLE" ).executeUpdate(); + + session.createSQLQuery( + "create table MAIN_TABLE( " + + "ID integer not null, NAME varchar(255) not null, CODE varchar(10) not null, " + + "MATERIAL_OWNER_ID integer, BUILDING_OWNER_ID integer, " + + "primary key (ID, CODE) )" + ).executeUpdate(); + + session.createSQLQuery( "insert into MAIN_TABLE(ID, NAME, CODE) VALUES( 1, 'plastic', 'MATERIAL' )" ) + .executeUpdate(); + session.createSQLQuery( "insert into MAIN_TABLE(ID, NAME, CODE) VALUES( 1, 'house', 'BUILDING' )" ) + .executeUpdate(); + session.createSQLQuery( "insert into MAIN_TABLE(ID, NAME, CODE, MATERIAL_OWNER_ID, BUILDING_OWNER_ID) " + + "VALUES( 1, 'high', 'RATING', 1, 1 )" ) + .executeUpdate(); + session.createSQLQuery( "insert into MAIN_TABLE(ID, NAME, CODE, MATERIAL_OWNER_ID, BUILDING_OWNER_ID) " + + "VALUES( 2, 'medium', 'RATING', 1, null )" ) + .executeUpdate(); + session.createSQLQuery( "insert into MAIN_TABLE(ID, NAME, CODE, MATERIAL_OWNER_ID, BUILDING_OWNER_ID) " + + "VALUES( 3, 'low', 'RATING', 1, null )" ) + .executeUpdate(); + session.createSQLQuery( "insert into MAIN_TABLE(ID, NAME, CODE, MATERIAL_OWNER_ID, BUILDING_OWNER_ID) " + + "VALUES( 1, 'small', 'SIZE', null, 1 )" ) + .executeUpdate(); + session.createSQLQuery( "insert into MAIN_TABLE(ID, NAME, CODE, MATERIAL_OWNER_ID, BUILDING_OWNER_ID) " + + "VALUES( 2, 'medium', 'SIZE', 1, null )" ) + .executeUpdate(); + } + ); + } + + @After + public void cleanup() { + doInHibernate( + this::sessionFactory, session -> { + session.createSQLQuery( "delete from MAIN_TABLE" ).executeUpdate(); + } + ); + } + + @Test + @TestForIssue( jiraKey = "HHH-12875") + public void testInitializeFromNonUniqueAssociationTable() { + doInHibernate( + this::sessionFactory, session -> { + + Material material = session.get( Material.class, 1 ); + assertEquals( "plastic", material.getName() ); + + // Material#mediumOrHighRatingsFromCombined is mapped with lazy="true" + assertFalse( Hibernate.isInitialized( material.getMediumOrHighRatingsFromCombined() ) ); + checkMediumOrHighRatings( material.getMediumOrHighRatingsFromCombined() ); + Rating highRating = null; + for ( Rating rating : material.getMediumOrHighRatingsFromCombined() ) { + if ( "high".equals( rating.getName() ) ) { + highRating = rating; + } + } + assertNotNull( highRating ); + + // Material#sizesFromCombined is mapped with lazy="true" + assertFalse( Hibernate.isInitialized( material.getSizesFromCombined() ) ); + assertEquals( 1, material.getSizesFromCombined().size() ); + assertTrue( Hibernate.isInitialized( material.getSizesFromCombined() ) ); + + final Size size = material.getSizesFromCombined().iterator().next(); + assertEquals( "medium", size.getName() ); + + Building building = session.get( Building.class, 1 ); + + // building.ratingsFromCombined is mapped with lazy="true" + assertFalse( Hibernate.isInitialized( building.getRatingsFromCombined() ) ); + assertEquals( 1, building.getRatingsFromCombined().size() ); + assertTrue( Hibernate.isInitialized( building.getRatingsFromCombined() ) ); + assertSame( highRating, building.getRatingsFromCombined().iterator().next() ); + + // Building#sizesFromCombined is mapped with lazy="true" + assertFalse( Hibernate.isInitialized( building.getSizesFromCombined() ) ); + assertEquals( 1, building.getSizesFromCombined().size() ); + assertTrue( Hibernate.isInitialized( building.getSizesFromCombined() ) ); + assertEquals( "small", building.getSizesFromCombined().iterator().next().getName() ); + } + ); + } + + private void checkMediumOrHighRatings(List mediumOrHighRatings) { + assertEquals( 2, mediumOrHighRatings.size() ); + + final Iterator iterator = mediumOrHighRatings.iterator(); + final Rating firstRating = iterator.next(); + final Rating secondRating = iterator.next(); + if ( "high".equals( firstRating.getName() ) ) { + assertEquals( "medium", secondRating.getName() ); + } + else if ( "medium".equals( firstRating.getName() ) ) { + assertEquals( "high", secondRating.getName() ); + } + else { + fail( "unexpected rating" ); + } + } + + @Entity( name = "Material" ) + @Table( name = "MAIN_TABLE" ) + @Where( clause = "CODE = 'MATERIAL'" ) + public static class Material { + private int id; + + private String name; + private Set sizesFromCombined = new HashSet<>(); + private List mediumOrHighRatingsFromCombined = new ArrayList<>(); + + @Id + @Column( name = "ID" ) + public int getId() { + return id; + } + public void setId(int id) { + this.id = id; + } + + @Column( name = "NAME") + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + + @OneToMany + @JoinColumn( name = "MATERIAL_OWNER_ID" ) + @Immutable + public Set getSizesFromCombined() { + return sizesFromCombined; + } + public void setSizesFromCombined(Set sizesFromCombined) { + this.sizesFromCombined = sizesFromCombined; + } + + @OneToMany + @JoinColumn( name = "MATERIAL_OWNER_ID") + @Where( clause = "name = 'high' or name = 'medium'" ) + @Immutable + public List getMediumOrHighRatingsFromCombined() { + return mediumOrHighRatingsFromCombined; + } + public void setMediumOrHighRatingsFromCombined(List mediumOrHighRatingsFromCombined) { + this.mediumOrHighRatingsFromCombined = mediumOrHighRatingsFromCombined; + } + } + + @Entity( name = "Building" ) + @Table( name = "MAIN_TABLE" ) + @Where( clause = "CODE = 'BUILDING'" ) + public static class Building { + private int id; + private String name; + private Set sizesFromCombined = new HashSet<>(); + private Set ratingsFromCombined = new HashSet<>(); + private List mediumOrHighRatings = new ArrayList<>(); + + @Id + @Column( name = "ID" ) + public int getId() { + return id; + } + public void setId(int id) { + this.id = id; + } + + @Column( name = "NAME") + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + + @OneToMany + @JoinColumn( name = "BUILDING_OWNER_ID" ) + @Immutable + public Set getSizesFromCombined() { + return sizesFromCombined; + } + public void setSizesFromCombined(Set sizesFromCombined) { + this.sizesFromCombined = sizesFromCombined; + } + + @OneToMany + @JoinColumn( name = "BUILDING_OWNER_ID" ) + @Immutable + public Set getRatingsFromCombined() { + return ratingsFromCombined; + } + public void setRatingsFromCombined(Set ratingsFromCombined) { + this.ratingsFromCombined = ratingsFromCombined; + } + } + + @Entity( name = "Size" ) + @Table( name = "MAIN_TABLE" ) + @Where( clause = "CODE = 'SIZE'" ) + public static class Size { + private int id; + private String name; + + @Id + @Column( name = "ID" ) + public int getId() { + return id; + } + public void setId(int id) { + this.id = id; + } + + @Column( name = "NAME") + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + } + + @Entity( name = "Rating" ) + @Table( name = "MAIN_TABLE" ) + @Where( clause = "CODE = 'RATING'" ) + public static class Rating { + private int id; + private String name; + + @Id + @Column( name = "ID" ) + public int getId() { + return id; + } + public void setId(int id) { + this.id = id; + } + + @Column( name = "NAME") + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/where/File.hbm.xml b/hibernate-core/src/test/java/org/hibernate/test/where/hbm/File.hbm.xml similarity index 93% rename from hibernate-core/src/test/java/org/hibernate/test/where/File.hbm.xml rename to hibernate-core/src/test/java/org/hibernate/test/where/hbm/File.hbm.xml index 1c4614a843..1ea7cf6b7b 100755 --- a/hibernate-core/src/test/java/org/hibernate/test/where/File.hbm.xml +++ b/hibernate-core/src/test/java/org/hibernate/test/where/hbm/File.hbm.xml @@ -12,7 +12,7 @@ - + diff --git a/hibernate-core/src/test/java/org/hibernate/test/where/File.java b/hibernate-core/src/test/java/org/hibernate/test/where/hbm/File.java similarity index 96% rename from hibernate-core/src/test/java/org/hibernate/test/where/File.java rename to hibernate-core/src/test/java/org/hibernate/test/where/hbm/File.java index 899bd58ace..6146cd6b44 100755 --- a/hibernate-core/src/test/java/org/hibernate/test/where/File.java +++ b/hibernate-core/src/test/java/org/hibernate/test/where/hbm/File.java @@ -6,7 +6,7 @@ */ //$Id: File.java 8043 2005-08-30 15:20:42Z oneovthafew $ -package org.hibernate.test.where; +package org.hibernate.test.where.hbm; import java.util.Set; public class File { diff --git a/hibernate-core/src/test/java/org/hibernate/test/where/LazyManyToManyNonUniqueIdNotFoundWhereTest.hbm.xml b/hibernate-core/src/test/java/org/hibernate/test/where/hbm/LazyManyToManyNonUniqueIdNotFoundWhereTest.hbm.xml similarity index 97% rename from hibernate-core/src/test/java/org/hibernate/test/where/LazyManyToManyNonUniqueIdNotFoundWhereTest.hbm.xml rename to hibernate-core/src/test/java/org/hibernate/test/where/hbm/LazyManyToManyNonUniqueIdNotFoundWhereTest.hbm.xml index 662121aa21..ef83684956 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/where/LazyManyToManyNonUniqueIdNotFoundWhereTest.hbm.xml +++ b/hibernate-core/src/test/java/org/hibernate/test/where/hbm/LazyManyToManyNonUniqueIdNotFoundWhereTest.hbm.xml @@ -4,7 +4,7 @@ "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> - + diff --git a/hibernate-core/src/test/java/org/hibernate/test/where/LazyManyToManyNonUniqueIdNotFoundWhereTest.java b/hibernate-core/src/test/java/org/hibernate/test/where/hbm/LazyManyToManyNonUniqueIdNotFoundWhereTest.java similarity index 98% rename from hibernate-core/src/test/java/org/hibernate/test/where/LazyManyToManyNonUniqueIdNotFoundWhereTest.java rename to hibernate-core/src/test/java/org/hibernate/test/where/hbm/LazyManyToManyNonUniqueIdNotFoundWhereTest.java index bb0f7b9d1e..0083f205c4 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/where/LazyManyToManyNonUniqueIdNotFoundWhereTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/where/hbm/LazyManyToManyNonUniqueIdNotFoundWhereTest.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.where; +package org.hibernate.test.where.hbm; import java.util.HashSet; import java.util.Set; @@ -29,7 +29,7 @@ import static org.junit.Assert.assertTrue; public class LazyManyToManyNonUniqueIdNotFoundWhereTest extends BaseCoreFunctionalTestCase { protected String[] getMappings() { - return new String[] { "where/LazyManyToManyNonUniqueIdNotFoundWhereTest.hbm.xml" }; + return new String[] { "where/hbm/LazyManyToManyNonUniqueIdNotFoundWhereTest.hbm.xml" }; } @Before diff --git a/hibernate-core/src/test/java/org/hibernate/test/where/LazyManyToManyNonUniqueIdWhereTest.hbm.xml b/hibernate-core/src/test/java/org/hibernate/test/where/hbm/LazyManyToManyNonUniqueIdWhereTest.hbm.xml similarity index 84% rename from hibernate-core/src/test/java/org/hibernate/test/where/LazyManyToManyNonUniqueIdWhereTest.hbm.xml rename to hibernate-core/src/test/java/org/hibernate/test/where/hbm/LazyManyToManyNonUniqueIdWhereTest.hbm.xml index 29bf2a85ef..d0b365853c 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/where/LazyManyToManyNonUniqueIdWhereTest.hbm.xml +++ b/hibernate-core/src/test/java/org/hibernate/test/where/hbm/LazyManyToManyNonUniqueIdWhereTest.hbm.xml @@ -4,7 +4,7 @@ "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> - + @@ -18,11 +18,12 @@ - - - + + @@ -50,10 +51,11 @@ - + - - + + diff --git a/hibernate-core/src/test/java/org/hibernate/test/where/LazyManyToManyNonUniqueIdWhereTest.java b/hibernate-core/src/test/java/org/hibernate/test/where/hbm/LazyManyToManyNonUniqueIdWhereTest.java similarity index 71% rename from hibernate-core/src/test/java/org/hibernate/test/where/LazyManyToManyNonUniqueIdWhereTest.java rename to hibernate-core/src/test/java/org/hibernate/test/where/hbm/LazyManyToManyNonUniqueIdWhereTest.java index 70d85d04a1..c4fdd61e7a 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/where/LazyManyToManyNonUniqueIdWhereTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/where/hbm/LazyManyToManyNonUniqueIdWhereTest.java @@ -4,9 +4,12 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.where; +package org.hibernate.test.where.hbm; +import java.util.ArrayList; import java.util.HashSet; +import java.util.Iterator; +import java.util.List; import java.util.Set; import org.hibernate.Hibernate; @@ -20,8 +23,10 @@ import org.junit.Test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; /** * @author Gail Badner @@ -29,7 +34,7 @@ import static org.junit.Assert.assertTrue; public class LazyManyToManyNonUniqueIdWhereTest extends BaseCoreFunctionalTestCase { protected String[] getMappings() { - return new String[] { "where/LazyManyToManyNonUniqueIdWhereTest.hbm.xml" }; + return new String[] { "where/hbm/LazyManyToManyNonUniqueIdWhereTest.hbm.xml" }; } @Before @@ -54,8 +59,14 @@ public class LazyManyToManyNonUniqueIdWhereTest extends BaseCoreFunctionalTestCa .executeUpdate(); session.createSQLQuery( "insert into MAIN_TABLE(ID, NAME, CODE) VALUES( 1, 'high', 'RATING' )" ) .executeUpdate(); + session.createSQLQuery( "insert into MAIN_TABLE(ID, NAME, CODE) VALUES( 2, 'medium', 'RATING' )" ) + .executeUpdate(); + session.createSQLQuery( "insert into MAIN_TABLE(ID, NAME, CODE) VALUES( 3, 'low', 'RATING' )" ) + .executeUpdate(); session.createSQLQuery( "insert into MAIN_TABLE(ID, NAME, CODE) VALUES( 1, 'small', 'SIZE' )" ) .executeUpdate(); + session.createSQLQuery( "insert into MAIN_TABLE(ID, NAME, CODE) VALUES( 2, 'medium', 'SIZE' )" ) + .executeUpdate(); session.createSQLQuery( "create table ASSOCIATION_TABLE( " + @@ -68,10 +79,18 @@ public class LazyManyToManyNonUniqueIdWhereTest extends BaseCoreFunctionalTestCa "insert into ASSOCIATION_TABLE(MAIN_ID, MAIN_CODE, ASSOCIATION_ID, ASSOCIATION_CODE) " + "VALUES( 1, 'MATERIAL', 1, 'RATING' )" ).executeUpdate(); + session.createSQLQuery( + "insert into ASSOCIATION_TABLE(MAIN_ID, MAIN_CODE, ASSOCIATION_ID, ASSOCIATION_CODE) " + + "VALUES( 1, 'MATERIAL', 2, 'RATING' )" + ).executeUpdate(); + session.createSQLQuery( + "insert into ASSOCIATION_TABLE(MAIN_ID, MAIN_CODE, ASSOCIATION_ID, ASSOCIATION_CODE) " + + "VALUES( 1, 'MATERIAL', 3, 'RATING' )" + ).executeUpdate(); session.createSQLQuery( "insert into ASSOCIATION_TABLE(MAIN_ID, MAIN_CODE, ASSOCIATION_ID, ASSOCIATION_CODE) " + - "VALUES( 1, 'MATERIAL', 1, 'SIZE' )" + "VALUES( 1, 'MATERIAL', 2, 'SIZE' )" ).executeUpdate(); session.createSQLQuery( @@ -104,6 +123,12 @@ public class LazyManyToManyNonUniqueIdWhereTest extends BaseCoreFunctionalTestCa session.createSQLQuery( "insert into BUILDING_RATINGS(BUILDING_ID, RATING_ID) VALUES( 1, 1 )" ).executeUpdate(); + session.createSQLQuery( + "insert into BUILDING_RATINGS(BUILDING_ID, RATING_ID) VALUES( 1, 2 )" + ).executeUpdate(); + session.createSQLQuery( + "insert into BUILDING_RATINGS(BUILDING_ID, RATING_ID) VALUES( 1, 3 )" + ).executeUpdate(); } session.getTransaction().commit(); session.close(); @@ -144,10 +169,8 @@ public class LazyManyToManyNonUniqueIdWhereTest extends BaseCoreFunctionalTestCa assertEquals( "house", building.getName() ); // Building#ratings is mapped with lazy="true" - assertFalse( Hibernate.isInitialized( building.getRatings() ) ); - assertEquals( 1, building.getRatings().size() ); - assertTrue( Hibernate.isInitialized( building.getRatings() ) ); - assertSame( rating, building.getRatings().iterator().next() ); + assertFalse( Hibernate.isInitialized( building.getMediumOrHighRatings() ) ); + checkMediumOrHighRatings( building.getMediumOrHighRatings() ); } session.getTransaction().commit(); session.close(); @@ -162,13 +185,16 @@ public class LazyManyToManyNonUniqueIdWhereTest extends BaseCoreFunctionalTestCa Material material = (Material) session.get( Material.class, 1 ); assertEquals( "plastic", material.getName() ); - // Material#ratingsFromCombined is mapped with lazy="true" - assertFalse( Hibernate.isInitialized( material.getRatingsFromCombined() ) ); - assertEquals( 1, material.getRatingsFromCombined().size() ); - assertTrue( Hibernate.isInitialized( material.getRatingsFromCombined() ) ); - - final Rating rating = material.getRatingsFromCombined().iterator().next(); - assertEquals( "high", rating.getName() ); + // Material#mediumOrHighRatingsFromCombined is mapped with lazy="true" + assertFalse( Hibernate.isInitialized( material.getMediumOrHighRatingsFromCombined() ) ); + checkMediumOrHighRatings( material.getMediumOrHighRatingsFromCombined() ); + Rating highRating = null; + for ( Rating rating : material.getMediumOrHighRatingsFromCombined() ) { + if ( "high".equals( rating.getName() ) ) { + highRating = rating; + } + } + assertNotNull( highRating ); // Material#sizesFromCombined is mapped with lazy="true" assertFalse( Hibernate.isInitialized( material.getSizesFromCombined() ) ); @@ -176,7 +202,7 @@ public class LazyManyToManyNonUniqueIdWhereTest extends BaseCoreFunctionalTestCa assertTrue( Hibernate.isInitialized( material.getSizesFromCombined() ) ); final Size size = material.getSizesFromCombined().iterator().next(); - assertEquals( "small", size.getName() ); + assertEquals( "medium", size.getName() ); Building building = (Building) session.get( Building.class, 1 ); @@ -184,24 +210,41 @@ public class LazyManyToManyNonUniqueIdWhereTest extends BaseCoreFunctionalTestCa assertFalse( Hibernate.isInitialized( building.getRatingsFromCombined() ) ); assertEquals( 1, building.getRatingsFromCombined().size() ); assertTrue( Hibernate.isInitialized( building.getRatingsFromCombined() ) ); - assertSame( rating, building.getRatingsFromCombined().iterator().next() ); + assertSame( highRating, building.getRatingsFromCombined().iterator().next() ); // Building#sizesFromCombined is mapped with lazy="true" assertFalse( Hibernate.isInitialized( building.getSizesFromCombined() ) ); assertEquals( 1, building.getSizesFromCombined().size() ); assertTrue( Hibernate.isInitialized( building.getSizesFromCombined() ) ); - assertSame( size, building.getSizesFromCombined().iterator().next() ); + assertEquals( "small", building.getSizesFromCombined().iterator().next().getName() ); } session.getTransaction().commit(); session.close(); } + private void checkMediumOrHighRatings(List mediumOrHighRatings) { + assertEquals( 2, mediumOrHighRatings.size() ); + + final Iterator iterator = mediumOrHighRatings.iterator(); + final Rating firstRating = iterator.next(); + final Rating secondRating = iterator.next(); + if ( "high".equals( firstRating.getName() ) ) { + assertEquals( "medium", secondRating.getName() ); + } + else if ( "medium".equals( firstRating.getName() ) ) { + assertEquals( "high", secondRating.getName() ); + } + else { + fail( "unexpected rating" ); + } + } + public static class Material { private int id; private String name; private Set sizesFromCombined = new HashSet<>(); - private Set ratingsFromCombined = new HashSet<>(); + private List mediumOrHighRatingsFromCombined = new ArrayList<>(); private Set ratings = new HashSet<>(); public int getId() { @@ -222,11 +265,11 @@ public class LazyManyToManyNonUniqueIdWhereTest extends BaseCoreFunctionalTestCa public void setSizesFromCombined(Set sizesFromCombined) { this.sizesFromCombined = sizesFromCombined; } - public Set getRatingsFromCombined() { - return ratingsFromCombined; + public List getMediumOrHighRatingsFromCombined() { + return mediumOrHighRatingsFromCombined; } - public void setRatingsFromCombined(Set ratingsFromCombined) { - this.ratingsFromCombined = ratingsFromCombined; + public void setMediumOrHighRatingsFromCombined(List mediumOrHighRatingsFromCombined) { + this.mediumOrHighRatingsFromCombined = mediumOrHighRatingsFromCombined; } public Set getRatings() { return ratings; @@ -241,7 +284,7 @@ public class LazyManyToManyNonUniqueIdWhereTest extends BaseCoreFunctionalTestCa private String name; private Set sizesFromCombined = new HashSet<>(); private Set ratingsFromCombined = new HashSet<>(); - private Set ratings = new HashSet<>(); + private List mediumOrHighRatings = new ArrayList<>(); public int getId() { return id; @@ -267,11 +310,11 @@ public class LazyManyToManyNonUniqueIdWhereTest extends BaseCoreFunctionalTestCa public void setRatingsFromCombined(Set ratingsFromCombined) { this.ratingsFromCombined = ratingsFromCombined; } - public Set getRatings() { - return ratings; + public List getMediumOrHighRatings() { + return mediumOrHighRatings; } - public void setRatings(Set ratings) { - this.ratings = ratings; + public void setMediumOrHighRatings(List mediumOrHighRatings) { + this.mediumOrHighRatings = mediumOrHighRatings; } } diff --git a/hibernate-core/src/test/java/org/hibernate/test/where/hbm/LazyOneToManyNonUniqueIdWhereTest.hbm.xml b/hibernate-core/src/test/java/org/hibernate/test/where/hbm/LazyOneToManyNonUniqueIdWhereTest.hbm.xml new file mode 100644 index 0000000000..caf4613069 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/where/hbm/LazyOneToManyNonUniqueIdWhereTest.hbm.xml @@ -0,0 +1,64 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/hibernate-core/src/test/java/org/hibernate/test/where/hbm/LazyOneToManyNonUniqueIdWhereTest.java b/hibernate-core/src/test/java/org/hibernate/test/where/hbm/LazyOneToManyNonUniqueIdWhereTest.java new file mode 100644 index 0000000000..004f5c4708 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/where/hbm/LazyOneToManyNonUniqueIdWhereTest.java @@ -0,0 +1,257 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.test.where.hbm; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Set; + +import org.hibernate.Hibernate; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +/** + * @author Gail Badner + */ +public class LazyOneToManyNonUniqueIdWhereTest extends BaseCoreFunctionalTestCase { + + protected String[] getMappings() { + return new String[] { "where/hbm/LazyOneToManyNonUniqueIdWhereTest.hbm.xml" }; + } + + @Before + public void setup() { + doInHibernate( + this::sessionFactory, session -> { + + session.createSQLQuery( "DROP TABLE MAIN_TABLE" ).executeUpdate(); + + session.createSQLQuery( + "create table MAIN_TABLE( " + + "ID integer not null, NAME varchar(255) not null, CODE varchar(10) not null, " + + "MATERIAL_OWNER_ID integer, BUILDING_OWNER_ID integer, " + + "primary key (ID, CODE) )" + ).executeUpdate(); + + session.createSQLQuery( "insert into MAIN_TABLE(ID, NAME, CODE) VALUES( 1, 'plastic', 'MATERIAL' )" ) + .executeUpdate(); + session.createSQLQuery( "insert into MAIN_TABLE(ID, NAME, CODE) VALUES( 1, 'house', 'BUILDING' )" ) + .executeUpdate(); + session.createSQLQuery( "insert into MAIN_TABLE(ID, NAME, CODE, MATERIAL_OWNER_ID, BUILDING_OWNER_ID) " + + "VALUES( 1, 'high', 'RATING', 1, 1 )" ) + .executeUpdate(); + session.createSQLQuery( "insert into MAIN_TABLE(ID, NAME, CODE, MATERIAL_OWNER_ID, BUILDING_OWNER_ID) " + + "VALUES( 2, 'medium', 'RATING', 1, null )" ) + .executeUpdate(); + session.createSQLQuery( "insert into MAIN_TABLE(ID, NAME, CODE, MATERIAL_OWNER_ID, BUILDING_OWNER_ID) " + + "VALUES( 3, 'low', 'RATING', 1, null )" ) + .executeUpdate(); + session.createSQLQuery( "insert into MAIN_TABLE(ID, NAME, CODE, MATERIAL_OWNER_ID, BUILDING_OWNER_ID) " + + "VALUES( 1, 'small', 'SIZE', null, 1 )" ) + .executeUpdate(); + session.createSQLQuery( "insert into MAIN_TABLE(ID, NAME, CODE, MATERIAL_OWNER_ID, BUILDING_OWNER_ID) " + + "VALUES( 2, 'medium', 'SIZE', 1, null )" ) + .executeUpdate(); + } + ); + } + + @After + public void cleanup() { + doInHibernate( + this::sessionFactory, session -> { + session.createSQLQuery( "delete from MAIN_TABLE" ).executeUpdate(); + } + ); + } + + @Test + @TestForIssue( jiraKey = "HHH-12875") + public void testInitializeFromNonUniqueAssociationTable() { + doInHibernate( + this::sessionFactory, session -> { + + Material material = session.get( Material.class, 1 ); + assertEquals( "plastic", material.getName() ); + + // Material#mediumOrHighRatingsFromCombined is mapped with lazy="true" + assertFalse( Hibernate.isInitialized( material.getMediumOrHighRatingsFromCombined() ) ); + checkMediumOrHighRatings( material.getMediumOrHighRatingsFromCombined() ); + Rating highRating = null; + for ( Rating rating : material.getMediumOrHighRatingsFromCombined() ) { + if ( "high".equals( rating.getName() ) ) { + highRating = rating; + } + } + assertNotNull( highRating ); + + // Material#sizesFromCombined is mapped with lazy="true" + assertFalse( Hibernate.isInitialized( material.getSizesFromCombined() ) ); + assertEquals( 1, material.getSizesFromCombined().size() ); + assertTrue( Hibernate.isInitialized( material.getSizesFromCombined() ) ); + + final Size size = material.getSizesFromCombined().iterator().next(); + assertEquals( "medium", size.getName() ); + + Building building = session.get( Building.class, 1 ); + + // building.ratingsFromCombined is mapped with lazy="true" + assertFalse( Hibernate.isInitialized( building.getRatingsFromCombined() ) ); + assertEquals( 1, building.getRatingsFromCombined().size() ); + assertTrue( Hibernate.isInitialized( building.getRatingsFromCombined() ) ); + assertSame( highRating, building.getRatingsFromCombined().iterator().next() ); + + // Building#sizesFromCombined is mapped with lazy="true" + assertFalse( Hibernate.isInitialized( building.getSizesFromCombined() ) ); + assertEquals( 1, building.getSizesFromCombined().size() ); + assertTrue( Hibernate.isInitialized( building.getSizesFromCombined() ) ); + assertEquals( "small", building.getSizesFromCombined().iterator().next().getName() ); + } + ); + } + + private void checkMediumOrHighRatings(List mediumOrHighRatings) { + assertEquals( 2, mediumOrHighRatings.size() ); + + final Iterator iterator = mediumOrHighRatings.iterator(); + final Rating firstRating = iterator.next(); + final Rating secondRating = iterator.next(); + if ( "high".equals( firstRating.getName() ) ) { + assertEquals( "medium", secondRating.getName() ); + } + else if ( "medium".equals( firstRating.getName() ) ) { + assertEquals( "high", secondRating.getName() ); + } + else { + fail( "unexpected rating" ); + } + } + + public static class Material { + private int id; + + private String name; + private Set sizesFromCombined = new HashSet<>(); + private List mediumOrHighRatingsFromCombined = new ArrayList<>(); + + public int getId() { + return id; + } + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + + public Set getSizesFromCombined() { + return sizesFromCombined; + } + public void setSizesFromCombined(Set sizesFromCombined) { + this.sizesFromCombined = sizesFromCombined; + } + + public List getMediumOrHighRatingsFromCombined() { + return mediumOrHighRatingsFromCombined; + } + public void setMediumOrHighRatingsFromCombined(List mediumOrHighRatingsFromCombined) { + this.mediumOrHighRatingsFromCombined = mediumOrHighRatingsFromCombined; + } + } + + public static class Building { + private int id; + private String name; + private Set sizesFromCombined = new HashSet<>(); + private Set ratingsFromCombined = new HashSet<>(); + private List mediumOrHighRatings = new ArrayList<>(); + + public int getId() { + return id; + } + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + + public Set getSizesFromCombined() { + return sizesFromCombined; + } + public void setSizesFromCombined(Set sizesFromCombined) { + this.sizesFromCombined = sizesFromCombined; + } + + public Set getRatingsFromCombined() { + return ratingsFromCombined; + } + public void setRatingsFromCombined(Set ratingsFromCombined) { + this.ratingsFromCombined = ratingsFromCombined; + } + } + + public static class Size { + private int id; + private String name; + + public int getId() { + return id; + } + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + } + + public static class Rating { + private int id; + private String name; + + public int getId() { + return id; + } + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/where/WhereTest.java b/hibernate-core/src/test/java/org/hibernate/test/where/hbm/WhereTest.java similarity index 91% rename from hibernate-core/src/test/java/org/hibernate/test/where/WhereTest.java rename to hibernate-core/src/test/java/org/hibernate/test/where/hbm/WhereTest.java index d67311d1ff..d3922688dd 100755 --- a/hibernate-core/src/test/java/org/hibernate/test/where/WhereTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/where/hbm/WhereTest.java @@ -4,15 +4,16 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.where; +package org.hibernate.test.where.hbm; import org.junit.Test; import org.hibernate.FetchMode; -import org.hibernate.Session; import org.hibernate.criterion.Restrictions; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.hibernate.test.where.hbm.File; + import static org.junit.Assert.assertEquals; /** @@ -20,7 +21,7 @@ import static org.junit.Assert.assertEquals; */ public class WhereTest extends BaseCoreFunctionalTestCase { public String[] getMappings() { - return new String[] { "where/File.hbm.xml" }; + return new String[] { "where/hbm/File.hbm.xml" }; } @Test