parent
83a9adbdb8
commit
7c4d18404c
|
@ -0,0 +1,309 @@
|
|||
/*
|
||||
* 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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.test.where.annotations;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import javax.persistence.CollectionTable;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.ElementCollection;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.Table;
|
||||
|
||||
import org.hibernate.Hibernate;
|
||||
import org.hibernate.Session;
|
||||
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.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* @author Gail Badner
|
||||
*/
|
||||
public class LazyElementCollectionBasicNonUniqueIdWhereTest extends BaseCoreFunctionalTestCase {
|
||||
|
||||
protected Class<?>[] getAnnotatedClasses() {
|
||||
return new Class[] { Material.class, Building.class };
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
Session session = openSession();
|
||||
session.beginTransaction();
|
||||
{
|
||||
session.createSQLQuery( "DROP TABLE MAIN_TABLE" ).executeUpdate();
|
||||
session.createSQLQuery( "DROP TABLE COLLECTION_TABLE" ).executeUpdate();
|
||||
session.createSQLQuery( "DROP TABLE MATERIAL_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 COLLECTION_TABLE( " +
|
||||
"MAIN_ID integer not null, MAIN_CODE varchar(10) not null, " +
|
||||
"VALUE varchar(10) not null, VALUE_CODE varchar(10) not null, " +
|
||||
"primary key (MAIN_ID, MAIN_CODE, VALUE, VALUE_CODE))"
|
||||
).executeUpdate();
|
||||
|
||||
session.createSQLQuery(
|
||||
"insert into COLLECTION_TABLE(MAIN_ID, MAIN_CODE, VALUE, VALUE_CODE) " +
|
||||
"VALUES( 1, 'MATERIAL', 'high', 'RATING' )"
|
||||
).executeUpdate();
|
||||
session.createSQLQuery(
|
||||
"insert into COLLECTION_TABLE(MAIN_ID, MAIN_CODE, VALUE, VALUE_CODE) " +
|
||||
"VALUES( 1, 'MATERIAL', 'medium', 'RATING' )"
|
||||
).executeUpdate();
|
||||
session.createSQLQuery(
|
||||
"insert into COLLECTION_TABLE(MAIN_ID, MAIN_CODE, VALUE, VALUE_CODE) " +
|
||||
"VALUES( 1, 'MATERIAL', 'low', 'RATING' )"
|
||||
).executeUpdate();
|
||||
|
||||
session.createSQLQuery(
|
||||
"insert into COLLECTION_TABLE(MAIN_ID, MAIN_CODE, VALUE, VALUE_CODE) " +
|
||||
"VALUES( 1, 'MATERIAL', 'medium', 'SIZE' )"
|
||||
).executeUpdate();
|
||||
|
||||
session.createSQLQuery(
|
||||
"insert into COLLECTION_TABLE(MAIN_ID, MAIN_CODE, VALUE, VALUE_CODE) " +
|
||||
"VALUES( 1, 'BUILDING', 'high', 'RATING' )"
|
||||
).executeUpdate();
|
||||
|
||||
session.createSQLQuery(
|
||||
"insert into COLLECTION_TABLE(MAIN_ID, MAIN_CODE, VALUE, VALUE_CODE) " +
|
||||
"VALUES( 1, 'BUILDING', 'small', 'SIZE' )"
|
||||
).executeUpdate();
|
||||
|
||||
|
||||
session.createSQLQuery(
|
||||
"create table MATERIAL_RATINGS( " +
|
||||
"MATERIAL_ID integer not null, RATING varchar(10) not null," +
|
||||
" primary key (MATERIAL_ID, RATING))"
|
||||
).executeUpdate();
|
||||
|
||||
session.createSQLQuery(
|
||||
"insert into MATERIAL_RATINGS(MATERIAL_ID, RATING) VALUES( 1, 'high' )"
|
||||
).executeUpdate();
|
||||
|
||||
}
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanup() {
|
||||
Session session = openSession();
|
||||
session.beginTransaction();
|
||||
{
|
||||
session.createSQLQuery( "delete from MATERIAL_RATINGS" ).executeUpdate();
|
||||
session.createSQLQuery( "delete from COLLECTION_TABLE" ).executeUpdate();
|
||||
session.createSQLQuery( "delete from MAIN_TABLE" ).executeUpdate();
|
||||
}
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue( jiraKey = "HHH-12937")
|
||||
public void testInitializeFromUniqueAssociationTable() {
|
||||
Session session = openSession();
|
||||
session.beginTransaction();
|
||||
{
|
||||
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() ) );
|
||||
|
||||
assertEquals( "high", material.getRatings().iterator().next() );
|
||||
}
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue( jiraKey = "HHH-12937")
|
||||
public void testInitializeFromNonUniqueAssociationTable() {
|
||||
Session session = openSession();
|
||||
session.beginTransaction();
|
||||
{
|
||||
Material material = session.get( Material.class, 1 );
|
||||
assertEquals( "plastic", material.getName() );
|
||||
|
||||
// Material#sizesFromCombined is mapped with lazy="true"
|
||||
assertFalse( Hibernate.isInitialized( material.getSizesFromCombined() ) );
|
||||
assertEquals( 1, material.getSizesFromCombined().size() );
|
||||
assertTrue( Hibernate.isInitialized( material.getSizesFromCombined() ) );
|
||||
|
||||
assertEquals( "medium", material.getSizesFromCombined().iterator().next() );
|
||||
|
||||
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() ) );
|
||||
assertEquals( "high", 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() );
|
||||
}
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
}
|
||||
|
||||
@Entity( name = "Material" )
|
||||
@Table( name = "MAIN_TABLE" )
|
||||
@Where( clause = "CODE = 'MATERIAL'" )
|
||||
public static class Material {
|
||||
private int id;
|
||||
|
||||
private String name;
|
||||
private Set<String> sizesFromCombined = new HashSet<>();
|
||||
private List<String> mediumOrHighRatingsFromCombined = new ArrayList<>();
|
||||
private Set<String> 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;
|
||||
}
|
||||
|
||||
@ElementCollection
|
||||
@CollectionTable(
|
||||
name = "COLLECTION_TABLE",
|
||||
joinColumns = { @JoinColumn( name = "MAIN_ID" ) }
|
||||
)
|
||||
@Column( name="VALUE")
|
||||
@Where( clause = "MAIN_CODE='MATERIAL' AND VALUE_CODE='SIZE'")
|
||||
@Immutable
|
||||
public Set<String> getSizesFromCombined() {
|
||||
return sizesFromCombined;
|
||||
}
|
||||
public void setSizesFromCombined(Set<String> sizesFromCombined) {
|
||||
this.sizesFromCombined = sizesFromCombined;
|
||||
}
|
||||
|
||||
@ElementCollection
|
||||
@CollectionTable(
|
||||
name = "MATERIAL_RATINGS",
|
||||
joinColumns = { @JoinColumn( name = "MATERIAL_ID" ) }
|
||||
)
|
||||
@Column( name="RATING")
|
||||
@Immutable
|
||||
public Set<String> getRatings() {
|
||||
return ratings;
|
||||
}
|
||||
public void setRatings(Set<String> 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<String> sizesFromCombined = new HashSet<>();
|
||||
private Set<String> ratingsFromCombined = new HashSet<>();
|
||||
private List<String> 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;
|
||||
}
|
||||
|
||||
@ElementCollection
|
||||
@CollectionTable(
|
||||
name = "COLLECTION_TABLE",
|
||||
joinColumns = { @JoinColumn( name = "MAIN_ID" ) }
|
||||
)
|
||||
@Column( name="VALUE")
|
||||
@Where( clause = "MAIN_CODE='BUILDING' AND VALUE_CODE='SIZE'")
|
||||
@Immutable
|
||||
public Set<String> getSizesFromCombined() {
|
||||
return sizesFromCombined;
|
||||
}
|
||||
public void setSizesFromCombined(Set<String> sizesFromCombined) {
|
||||
this.sizesFromCombined = sizesFromCombined;
|
||||
}
|
||||
|
||||
@ElementCollection
|
||||
@CollectionTable(
|
||||
name = "COLLECTION_TABLE",
|
||||
joinColumns = { @JoinColumn( name = "MAIN_ID" ) }
|
||||
)
|
||||
@Column( name="VALUE")
|
||||
@Where( clause = "MAIN_CODE='BUILDING' AND VALUE_CODE='RATING'" )
|
||||
@Immutable
|
||||
public Set<String> getRatingsFromCombined() {
|
||||
return ratingsFromCombined;
|
||||
}
|
||||
public void setRatingsFromCombined(Set<String> ratingsFromCombined) {
|
||||
this.ratingsFromCombined = ratingsFromCombined;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,402 @@
|
|||
/*
|
||||
* 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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.test.where.annotations;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import javax.persistence.AssociationOverride;
|
||||
import javax.persistence.AssociationOverrides;
|
||||
import javax.persistence.CollectionTable;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.ElementCollection;
|
||||
import javax.persistence.Embeddable;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.Table;
|
||||
|
||||
import org.hibernate.Hibernate;
|
||||
import org.hibernate.Session;
|
||||
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.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* @author Gail Badner
|
||||
*/
|
||||
public class LazyElementCollectionWithLazyManyToOneNonUniqueIdWhereTest extends BaseCoreFunctionalTestCase {
|
||||
|
||||
protected Class<?>[] getAnnotatedClasses() {
|
||||
return new Class[] { Material.class, Building.class, Rating.class, Size.class };
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
Session session = openSession();
|
||||
session.beginTransaction();
|
||||
{
|
||||
session.createSQLQuery( "DROP TABLE MAIN_TABLE" ).executeUpdate();
|
||||
session.createSQLQuery( "DROP TABLE COLLECTION_TABLE" ).executeUpdate();
|
||||
session.createSQLQuery( "DROP TABLE MATERIAL_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 COLLECTION_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 COLLECTION_TABLE(MAIN_ID, MAIN_CODE, ASSOCIATION_ID, ASSOCIATION_CODE) " +
|
||||
"VALUES( 1, 'MATERIAL', 1, 'RATING' )"
|
||||
).executeUpdate();
|
||||
session.createSQLQuery(
|
||||
"insert into COLLECTION_TABLE(MAIN_ID, MAIN_CODE, ASSOCIATION_ID, ASSOCIATION_CODE) " +
|
||||
"VALUES( 1, 'MATERIAL', 2, 'RATING' )"
|
||||
).executeUpdate();
|
||||
session.createSQLQuery(
|
||||
"insert into COLLECTION_TABLE(MAIN_ID, MAIN_CODE, ASSOCIATION_ID, ASSOCIATION_CODE) " +
|
||||
"VALUES( 1, 'MATERIAL', 3, 'RATING' )"
|
||||
).executeUpdate();
|
||||
|
||||
session.createSQLQuery(
|
||||
"insert into COLLECTION_TABLE(MAIN_ID, MAIN_CODE, ASSOCIATION_ID, ASSOCIATION_CODE) " +
|
||||
"VALUES( 1, 'MATERIAL', 2, 'SIZE' )"
|
||||
).executeUpdate();
|
||||
|
||||
session.createSQLQuery(
|
||||
"insert into COLLECTION_TABLE(MAIN_ID, MAIN_CODE, ASSOCIATION_ID, ASSOCIATION_CODE) " +
|
||||
"VALUES( 1, 'BUILDING', 1, 'RATING' )"
|
||||
).executeUpdate();
|
||||
|
||||
session.createSQLQuery(
|
||||
"insert into COLLECTION_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.getTransaction().commit();
|
||||
session.close();
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanup() {
|
||||
Session session = openSession();
|
||||
session.beginTransaction();
|
||||
{
|
||||
session.createSQLQuery( "delete from MATERIAL_RATINGS" ).executeUpdate();
|
||||
session.createSQLQuery( "delete from COLLECTION_TABLE" ).executeUpdate();
|
||||
session.createSQLQuery( "delete from MAIN_TABLE" ).executeUpdate();
|
||||
}
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue( jiraKey = "HHH-12937")
|
||||
public void testInitializeFromUniqueAssociationTable() {
|
||||
Session session = openSession();
|
||||
session.beginTransaction();
|
||||
{
|
||||
Material material = session.get( Material.class, 1 );
|
||||
assertEquals( "plastic", material.getName() );
|
||||
|
||||
// Material#ratings is mapped with lazy="true"
|
||||
assertFalse( Hibernate.isInitialized( material.getContainedRatings() ) );
|
||||
assertEquals( 1, material.getContainedRatings().size() );
|
||||
assertTrue( Hibernate.isInitialized( material.getContainedRatings() ) );
|
||||
|
||||
final ContainedRating containedRating = material.getContainedRatings().iterator().next();
|
||||
assertTrue( Hibernate.isInitialized( containedRating ) );
|
||||
assertEquals( "high", containedRating.getRating().getName() );
|
||||
|
||||
}
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue( jiraKey = "HHH-12937")
|
||||
public void testInitializeFromNonUniqueAssociationTable() {
|
||||
Session session = openSession();
|
||||
session.beginTransaction();
|
||||
{
|
||||
Material material = session.get( Material.class, 1 );
|
||||
assertEquals( "plastic", material.getName() );
|
||||
|
||||
// Material#containedSizesFromCombined is mapped with lazy="true"
|
||||
assertFalse( Hibernate.isInitialized( material.getContainedSizesFromCombined() ) );
|
||||
assertEquals( 1, material.getContainedSizesFromCombined().size() );
|
||||
assertTrue( Hibernate.isInitialized( material.getContainedSizesFromCombined() ) );
|
||||
|
||||
ContainedSize containedSize = material.getContainedSizesFromCombined().iterator().next();
|
||||
assertFalse( Hibernate.isInitialized( containedSize.getSize() ) );
|
||||
assertEquals( "medium", containedSize.getSize().getName() );
|
||||
|
||||
Building building = session.get( Building.class, 1 );
|
||||
|
||||
// building.ratingsFromCombined is mapped with lazy="true"
|
||||
assertFalse( Hibernate.isInitialized( building.getContainedRatingsFromCombined() ) );
|
||||
assertEquals( 1, building.getContainedRatingsFromCombined().size() );
|
||||
assertTrue( Hibernate.isInitialized( building.getContainedRatingsFromCombined() ) );
|
||||
ContainedRating containedRating = building.getContainedRatingsFromCombined().iterator().next();
|
||||
assertFalse( Hibernate.isInitialized( containedRating.getRating() ) );
|
||||
assertEquals( "high", containedRating.getRating().getName() );
|
||||
|
||||
// Building#containedSizesFromCombined is mapped with lazy="true"
|
||||
assertFalse( Hibernate.isInitialized( building.getContainedSizesFromCombined() ) );
|
||||
assertEquals( 1, building.getContainedSizesFromCombined().size() );
|
||||
assertTrue( Hibernate.isInitialized( building.getContainedSizesFromCombined() ) );
|
||||
containedSize = building.getContainedSizesFromCombined().iterator().next();
|
||||
assertFalse( Hibernate.isInitialized( containedSize.getSize() ) );
|
||||
assertEquals( "small", containedSize.getSize().getName() );
|
||||
}
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
}
|
||||
|
||||
@Entity( name = "Material" )
|
||||
@Table( name = "MAIN_TABLE" )
|
||||
@Where( clause = "CODE = 'MATERIAL'" )
|
||||
public static class Material {
|
||||
private int id;
|
||||
|
||||
private String name;
|
||||
private Set<ContainedSize> containedSizesFromCombined = new HashSet<>();
|
||||
private List<Rating> mediumOrHighRatingsFromCombined = new ArrayList<>();
|
||||
private Set<ContainedRating> containedRatings = 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;
|
||||
}
|
||||
|
||||
@ElementCollection
|
||||
@CollectionTable(
|
||||
name = "COLLECTION_TABLE",
|
||||
joinColumns = { @JoinColumn( name = "MAIN_ID" ) }
|
||||
)
|
||||
@AssociationOverrides(
|
||||
value = { @AssociationOverride( name = "size", joinColumns = { @JoinColumn(name = "ASSOCIATION_ID") } ) }
|
||||
)
|
||||
@Where( clause = "MAIN_CODE='MATERIAL' AND ASSOCIATION_CODE='SIZE'")
|
||||
@Immutable
|
||||
public Set<ContainedSize> getContainedSizesFromCombined() {
|
||||
return containedSizesFromCombined;
|
||||
}
|
||||
public void setContainedSizesFromCombined(Set<ContainedSize> containedSizesFromCombined) {
|
||||
this.containedSizesFromCombined = containedSizesFromCombined;
|
||||
}
|
||||
|
||||
@ElementCollection
|
||||
@CollectionTable(
|
||||
name = "MATERIAL_RATINGS",
|
||||
joinColumns = { @JoinColumn( name = "MATERIAL_ID" ) }
|
||||
)
|
||||
@AssociationOverrides(
|
||||
value = { @AssociationOverride( name = "rating", joinColumns = { @JoinColumn(name = "RATING_ID") } ) }
|
||||
)
|
||||
@Immutable
|
||||
public Set<ContainedRating> getContainedRatings() {
|
||||
return containedRatings;
|
||||
}
|
||||
public void setContainedRatings(Set<ContainedRating> containedRatings) {
|
||||
this.containedRatings = containedRatings;
|
||||
}
|
||||
}
|
||||
|
||||
@Entity( name = "Building" )
|
||||
@Table( name = "MAIN_TABLE" )
|
||||
@Where( clause = "CODE = 'BUILDING'" )
|
||||
public static class Building {
|
||||
private int id;
|
||||
private String name;
|
||||
private Set<ContainedSize> containedSizesFromCombined = new HashSet<>();
|
||||
private Set<ContainedRating> containedRatingsFromCombined = new HashSet<>();
|
||||
private List<Rating> 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;
|
||||
}
|
||||
|
||||
@ElementCollection
|
||||
@CollectionTable(
|
||||
name = "COLLECTION_TABLE",
|
||||
joinColumns = { @JoinColumn( name = "MAIN_ID" ) }
|
||||
)
|
||||
@Where( clause = "MAIN_CODE='BUILDING' AND ASSOCIATION_CODE='SIZE'")
|
||||
@Immutable
|
||||
public Set<ContainedSize> getContainedSizesFromCombined() {
|
||||
return containedSizesFromCombined;
|
||||
}
|
||||
public void setContainedSizesFromCombined(Set<ContainedSize> containedSizesFromCombined) {
|
||||
this.containedSizesFromCombined = containedSizesFromCombined;
|
||||
}
|
||||
|
||||
@ElementCollection
|
||||
@CollectionTable(
|
||||
name = "COLLECTION_TABLE",
|
||||
joinColumns = { @JoinColumn( name = "MAIN_ID" ) }
|
||||
)
|
||||
@Where( clause = "MAIN_CODE='BUILDING' AND ASSOCIATION_CODE='RATING'" )
|
||||
@Immutable
|
||||
public Set<ContainedRating> getContainedRatingsFromCombined() {
|
||||
return containedRatingsFromCombined;
|
||||
}
|
||||
public void setContainedRatingsFromCombined(Set<ContainedRating> containedRatingsFromCombined) {
|
||||
this.containedRatingsFromCombined = containedRatingsFromCombined;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
}
|
||||
|
||||
@Embeddable
|
||||
public static class ContainedSize {
|
||||
private Size size;
|
||||
|
||||
@ManyToOne( fetch = FetchType.LAZY )
|
||||
@JoinColumn( name = "ASSOCIATION_ID" )
|
||||
public Size getSize() {
|
||||
return size;
|
||||
}
|
||||
public void setSize(Size size) {
|
||||
this.size = size;
|
||||
}
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
}
|
||||
|
||||
@Embeddable
|
||||
public static class ContainedRating {
|
||||
private Rating rating;
|
||||
|
||||
@ManyToOne( fetch = FetchType.LAZY )
|
||||
@JoinColumn( name = "ASSOCIATION_ID" )
|
||||
public Rating getRating() {
|
||||
return rating;
|
||||
}
|
||||
public void setRating(Rating rating) {
|
||||
this.rating = rating;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -352,7 +352,7 @@ public class LazyManyToManyNonUniqueIdWhereTest extends BaseCoreFunctionalTestCa
|
|||
joinColumns = { @JoinColumn( name = "MAIN_ID" ) },
|
||||
inverseJoinColumns = { @JoinColumn( name = "ASSOCIATION_ID" ) }
|
||||
)
|
||||
@WhereJoinTable( clause = "MAIN_CODE='BUILDING' AND ASSOCIATION_CODE='RATING'")
|
||||
@WhereJoinTable( clause = "MAIN_CODE='BUILDING' AND ASSOCIATION_CODE='SIZE'")
|
||||
@Immutable
|
||||
public Set<Size> getSizesFromCombined() {
|
||||
return sizesFromCombined;
|
||||
|
|
|
@ -0,0 +1,52 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<!DOCTYPE hibernate-mapping PUBLIC
|
||||
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
|
||||
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
|
||||
|
||||
<hibernate-mapping package="org.hibernate.test.where.hbm" default-access="property">
|
||||
<class name="LazyElementCollectionBasicNonUniqueIdWhereTest$Material" table="MAIN_TABLE" where="CODE = 'MATERIAL'">
|
||||
<id name="id" column="ID">
|
||||
<generator class="assigned" />
|
||||
</id>
|
||||
|
||||
<property name="name" column="NAME"/>
|
||||
|
||||
<set name="sizesFromCombined" table="COLLECTION_TABLE" lazy="true" mutable="false"
|
||||
where="MAIN_CODE='MATERIAL' AND VALUE_CODE='SIZE'">
|
||||
<key column="MAIN_ID"/>
|
||||
<element type="string" column="VALUE"/>
|
||||
</set>
|
||||
|
||||
<set name="ratings" table="MATERIAL_RATINGS" lazy="true" mutable="false">
|
||||
<key column="MATERIAL_ID"/>
|
||||
<element type="string" column="RATING"/>
|
||||
</set>
|
||||
|
||||
</class>
|
||||
|
||||
<class name="LazyElementCollectionBasicNonUniqueIdWhereTest$Building" table="MAIN_TABLE" where="CODE = 'BUILDING'">
|
||||
<id name="id" column="ID">
|
||||
<generator class="assigned" />
|
||||
</id>
|
||||
|
||||
<property name="name" column="NAME"/>
|
||||
|
||||
<set name="sizesFromCombined" table="COLLECTION_TABLE" lazy="true" mutable="false"
|
||||
where="MAIN_CODE='BUILDING' AND VALUE_CODE='SIZE'">
|
||||
<key column="MAIN_ID"/>
|
||||
<element type="string" column="VALUE"/>
|
||||
</set>
|
||||
|
||||
<set name="ratingsFromCombined" table="COLLECTION_TABLE" lazy="true" mutable="false"
|
||||
where="MAIN_CODE='BUILDING' AND VALUE_CODE='RATING'">
|
||||
<key column="MAIN_ID"/>
|
||||
<element type="string" column="VALUE"/>
|
||||
</set>
|
||||
|
||||
</class>
|
||||
|
||||
</hibernate-mapping>
|
||||
|
||||
|
||||
|
|
@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.test.where.hbm;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.hibernate.Hibernate;
|
||||
import org.hibernate.Session;
|
||||
|
||||
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.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* @author Gail Badner
|
||||
*/
|
||||
public class LazyElementCollectionBasicNonUniqueIdWhereTest extends BaseCoreFunctionalTestCase {
|
||||
|
||||
protected String[] getMappings() {
|
||||
return new String[] { "where/hbm/LazyElementCollectionBasicNonUniqueIdWhereTest.hbm.xml" };
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
Session session = openSession();
|
||||
session.beginTransaction();
|
||||
{
|
||||
session.createSQLQuery( "DROP TABLE MAIN_TABLE" ).executeUpdate();
|
||||
session.createSQLQuery( "DROP TABLE COLLECTION_TABLE" ).executeUpdate();
|
||||
session.createSQLQuery( "DROP TABLE MATERIAL_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 COLLECTION_TABLE( " +
|
||||
"MAIN_ID integer not null, MAIN_CODE varchar(10) not null, " +
|
||||
"VALUE varchar(10) not null, VALUE_CODE varchar(10) not null, " +
|
||||
"primary key (MAIN_ID, MAIN_CODE, VALUE, VALUE_CODE))"
|
||||
).executeUpdate();
|
||||
|
||||
session.createSQLQuery(
|
||||
"insert into COLLECTION_TABLE(MAIN_ID, MAIN_CODE, VALUE, VALUE_CODE) " +
|
||||
"VALUES( 1, 'MATERIAL', 'high', 'RATING' )"
|
||||
).executeUpdate();
|
||||
session.createSQLQuery(
|
||||
"insert into COLLECTION_TABLE(MAIN_ID, MAIN_CODE, VALUE, VALUE_CODE) " +
|
||||
"VALUES( 1, 'MATERIAL', 'medium', 'RATING' )"
|
||||
).executeUpdate();
|
||||
session.createSQLQuery(
|
||||
"insert into COLLECTION_TABLE(MAIN_ID, MAIN_CODE, VALUE, VALUE_CODE) " +
|
||||
"VALUES( 1, 'MATERIAL', 'low', 'RATING' )"
|
||||
).executeUpdate();
|
||||
|
||||
session.createSQLQuery(
|
||||
"insert into COLLECTION_TABLE(MAIN_ID, MAIN_CODE, VALUE, VALUE_CODE) " +
|
||||
"VALUES( 1, 'MATERIAL', 'medium', 'SIZE' )"
|
||||
).executeUpdate();
|
||||
|
||||
session.createSQLQuery(
|
||||
"insert into COLLECTION_TABLE(MAIN_ID, MAIN_CODE, VALUE, VALUE_CODE) " +
|
||||
"VALUES( 1, 'BUILDING', 'high', 'RATING' )"
|
||||
).executeUpdate();
|
||||
|
||||
session.createSQLQuery(
|
||||
"insert into COLLECTION_TABLE(MAIN_ID, MAIN_CODE, VALUE, VALUE_CODE) " +
|
||||
"VALUES( 1, 'BUILDING', 'small', 'SIZE' )"
|
||||
).executeUpdate();
|
||||
|
||||
|
||||
session.createSQLQuery(
|
||||
"create table MATERIAL_RATINGS( " +
|
||||
"MATERIAL_ID integer not null, RATING varchar(10) not null," +
|
||||
" primary key (MATERIAL_ID, RATING))"
|
||||
).executeUpdate();
|
||||
|
||||
session.createSQLQuery(
|
||||
"insert into MATERIAL_RATINGS(MATERIAL_ID, RATING) VALUES( 1, 'high' )"
|
||||
).executeUpdate();
|
||||
|
||||
}
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanup() {
|
||||
Session session = openSession();
|
||||
session.beginTransaction();
|
||||
{
|
||||
session.createSQLQuery( "delete from MATERIAL_RATINGS" ).executeUpdate();
|
||||
// session.createSQLQuery( "delete from BUILDING_RATINGS" ).executeUpdate();
|
||||
session.createSQLQuery( "delete from COLLECTION_TABLE" ).executeUpdate();
|
||||
session.createSQLQuery( "delete from MAIN_TABLE" ).executeUpdate();
|
||||
}
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue( jiraKey = "HHH-12937")
|
||||
public void testInitializeFromUniqueAssociationTable() {
|
||||
Session session = openSession();
|
||||
session.beginTransaction();
|
||||
{
|
||||
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() ) );
|
||||
|
||||
assertEquals( "high", material.getRatings().iterator().next() );
|
||||
}
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue( jiraKey = "HHH-12937")
|
||||
public void testInitializeFromNonUniqueAssociationTable() {
|
||||
Session session = openSession();
|
||||
session.beginTransaction();
|
||||
{
|
||||
Material material = session.get( Material.class, 1 );
|
||||
assertEquals( "plastic", material.getName() );
|
||||
|
||||
// Material#sizesFromCombined is mapped with lazy="true"
|
||||
assertFalse( Hibernate.isInitialized( material.getSizesFromCombined() ) );
|
||||
assertEquals( 1, material.getSizesFromCombined().size() );
|
||||
assertTrue( Hibernate.isInitialized( material.getSizesFromCombined() ) );
|
||||
|
||||
assertEquals( "medium", material.getSizesFromCombined().iterator().next() );
|
||||
|
||||
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() ) );
|
||||
assertEquals( "high", 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() );
|
||||
}
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
}
|
||||
|
||||
public static class Material {
|
||||
private int id;
|
||||
|
||||
private String name;
|
||||
private Set<String> sizesFromCombined = new HashSet<>();
|
||||
private List<String> mediumOrHighRatingsFromCombined = new ArrayList<>();
|
||||
private Set<String> ratings = new HashSet<>();
|
||||
|
||||
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<String> getSizesFromCombined() {
|
||||
return sizesFromCombined;
|
||||
}
|
||||
public void setSizesFromCombined(Set<String> sizesFromCombined) {
|
||||
this.sizesFromCombined = sizesFromCombined;
|
||||
}
|
||||
|
||||
public Set<String> getRatings() {
|
||||
return ratings;
|
||||
}
|
||||
public void setRatings(Set<String> ratings) {
|
||||
this.ratings = ratings;
|
||||
}
|
||||
}
|
||||
|
||||
public static class Building {
|
||||
private int id;
|
||||
private String name;
|
||||
private Set<String> sizesFromCombined = new HashSet<>();
|
||||
private Set<String> ratingsFromCombined = new HashSet<>();
|
||||
private List<String> 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<String> getSizesFromCombined() {
|
||||
return sizesFromCombined;
|
||||
}
|
||||
public void setSizesFromCombined(Set<String> sizesFromCombined) {
|
||||
this.sizesFromCombined = sizesFromCombined;
|
||||
}
|
||||
|
||||
public Set<String> getRatingsFromCombined() {
|
||||
return ratingsFromCombined;
|
||||
}
|
||||
public void setRatingsFromCombined(Set<String> ratingsFromCombined) {
|
||||
this.ratingsFromCombined = ratingsFromCombined;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,71 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<!DOCTYPE hibernate-mapping PUBLIC
|
||||
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
|
||||
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
|
||||
|
||||
<hibernate-mapping package="org.hibernate.test.where.hbm" default-access="property">
|
||||
<class name="LazyElementCollectionWithLazyManyToOneNonUniqueIdWhereTest$Material" table="MAIN_TABLE" where="CODE = 'MATERIAL'">
|
||||
<id name="id" column="ID">
|
||||
<generator class="assigned" />
|
||||
</id>
|
||||
|
||||
<property name="name" column="NAME"/>
|
||||
|
||||
<set name="containedSizesFromCombined" table="COLLECTION_TABLE" lazy="true" mutable="false"
|
||||
where="MAIN_CODE='MATERIAL' AND ASSOCIATION_CODE='SIZE'">
|
||||
<key column="MAIN_ID"/>
|
||||
<composite-element class="LazyElementCollectionWithLazyManyToOneNonUniqueIdWhereTest$ContainedSize">
|
||||
<many-to-one name="size" column="ASSOCIATION_ID"/>
|
||||
</composite-element>
|
||||
</set>
|
||||
|
||||
<set name="containedRatings" table="MATERIAL_RATINGS" lazy="true" mutable="false">
|
||||
<key column="MATERIAL_ID"/>
|
||||
<composite-element class="LazyElementCollectionWithLazyManyToOneNonUniqueIdWhereTest$ContainedRating">
|
||||
<many-to-one name="rating" column="RATING_ID"/>
|
||||
</composite-element>
|
||||
</set>
|
||||
|
||||
</class>
|
||||
|
||||
<class name="LazyElementCollectionWithLazyManyToOneNonUniqueIdWhereTest$Building" table="MAIN_TABLE" where="CODE = 'BUILDING'">
|
||||
<id name="id" column="ID">
|
||||
<generator class="assigned" />
|
||||
</id>
|
||||
|
||||
<property name="name" column="NAME"/>
|
||||
|
||||
<set name="containedSizesFromCombined" table="COLLECTION_TABLE" lazy="true" mutable="false"
|
||||
where="MAIN_CODE='BUILDING' AND ASSOCIATION_CODE='SIZE'">
|
||||
<key column="MAIN_ID"/>
|
||||
<composite-element class="LazyElementCollectionWithLazyManyToOneNonUniqueIdWhereTest$ContainedSize">
|
||||
<many-to-one name="size" column="ASSOCIATION_ID"/>
|
||||
</composite-element>
|
||||
</set>
|
||||
|
||||
<set name="containedRatingsFromCombined" table="COLLECTION_TABLE" lazy="true" mutable="false"
|
||||
where="MAIN_CODE='BUILDING' AND ASSOCIATION_CODE='RATING'">
|
||||
<key column="MAIN_ID"/>
|
||||
<composite-element class="LazyElementCollectionWithLazyManyToOneNonUniqueIdWhereTest$ContainedRating">
|
||||
<many-to-one name="rating" column="ASSOCIATION_ID"/>
|
||||
</composite-element>
|
||||
</set>
|
||||
|
||||
</class>
|
||||
|
||||
<class name="LazyElementCollectionWithLazyManyToOneNonUniqueIdWhereTest$Size" table="MAIN_TABLE" where="CODE = 'SIZE'">
|
||||
<id name="id" column="ID">
|
||||
<generator class="assigned" />
|
||||
</id>
|
||||
<property name="name" column="NAME"/>
|
||||
</class>
|
||||
|
||||
<class name="LazyElementCollectionWithLazyManyToOneNonUniqueIdWhereTest$Rating" table="MAIN_TABLE" where="CODE = 'RATING'">
|
||||
<id name="id" column="ID">
|
||||
<generator class="assigned" />
|
||||
</id>
|
||||
<property name="name" column="NAME"/>
|
||||
</class>
|
||||
|
||||
</hibernate-mapping>
|
|
@ -0,0 +1,325 @@
|
|||
/*
|
||||
* 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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.test.where.hbm;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.hibernate.Hibernate;
|
||||
import org.hibernate.Session;
|
||||
|
||||
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.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* @author Gail Badner
|
||||
*/
|
||||
public class LazyElementCollectionWithLazyManyToOneNonUniqueIdWhereTest extends BaseCoreFunctionalTestCase {
|
||||
|
||||
protected String[] getMappings() {
|
||||
return new String[] { "where/hbm/LazyElementCollectionWithLazyManyToOneNonUniqueIdWhereTest.hbm.xml" };
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
Session session = openSession();
|
||||
session.beginTransaction();
|
||||
{
|
||||
session.createSQLQuery( "DROP TABLE MAIN_TABLE" ).executeUpdate();
|
||||
session.createSQLQuery( "DROP TABLE COLLECTION_TABLE" ).executeUpdate();
|
||||
session.createSQLQuery( "DROP TABLE MATERIAL_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 COLLECTION_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 COLLECTION_TABLE(MAIN_ID, MAIN_CODE, ASSOCIATION_ID, ASSOCIATION_CODE) " +
|
||||
"VALUES( 1, 'MATERIAL', 1, 'RATING' )"
|
||||
).executeUpdate();
|
||||
session.createSQLQuery(
|
||||
"insert into COLLECTION_TABLE(MAIN_ID, MAIN_CODE, ASSOCIATION_ID, ASSOCIATION_CODE) " +
|
||||
"VALUES( 1, 'MATERIAL', 2, 'RATING' )"
|
||||
).executeUpdate();
|
||||
session.createSQLQuery(
|
||||
"insert into COLLECTION_TABLE(MAIN_ID, MAIN_CODE, ASSOCIATION_ID, ASSOCIATION_CODE) " +
|
||||
"VALUES( 1, 'MATERIAL', 3, 'RATING' )"
|
||||
).executeUpdate();
|
||||
|
||||
session.createSQLQuery(
|
||||
"insert into COLLECTION_TABLE(MAIN_ID, MAIN_CODE, ASSOCIATION_ID, ASSOCIATION_CODE) " +
|
||||
"VALUES( 1, 'MATERIAL', 2, 'SIZE' )"
|
||||
).executeUpdate();
|
||||
|
||||
session.createSQLQuery(
|
||||
"insert into COLLECTION_TABLE(MAIN_ID, MAIN_CODE, ASSOCIATION_ID, ASSOCIATION_CODE) " +
|
||||
"VALUES( 1, 'BUILDING', 1, 'RATING' )"
|
||||
).executeUpdate();
|
||||
|
||||
session.createSQLQuery(
|
||||
"insert into COLLECTION_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.getTransaction().commit();
|
||||
session.close();
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanup() {
|
||||
Session session = openSession();
|
||||
session.beginTransaction();
|
||||
{
|
||||
session.createSQLQuery( "delete from MATERIAL_RATINGS" ).executeUpdate();
|
||||
session.createSQLQuery( "delete from COLLECTION_TABLE" ).executeUpdate();
|
||||
session.createSQLQuery( "delete from MAIN_TABLE" ).executeUpdate();
|
||||
}
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue( jiraKey = "HHH-12937")
|
||||
public void testInitializeFromUniqueAssociationTable() {
|
||||
Session session = openSession();
|
||||
session.beginTransaction();
|
||||
{
|
||||
Material material = session.get( Material.class, 1 );
|
||||
assertEquals( "plastic", material.getName() );
|
||||
|
||||
// Material#ratings is mapped with lazy="true"
|
||||
assertFalse( Hibernate.isInitialized( material.getContainedRatings() ) );
|
||||
assertEquals( 1, material.getContainedRatings().size() );
|
||||
assertTrue( Hibernate.isInitialized( material.getContainedRatings() ) );
|
||||
|
||||
final ContainedRating containedRating = material.getContainedRatings().iterator().next();
|
||||
assertTrue( Hibernate.isInitialized( containedRating ) );
|
||||
assertEquals( "high", containedRating.getRating().getName() );
|
||||
}
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue( jiraKey = "HHH-12937")
|
||||
public void testInitializeFromNonUniqueAssociationTable() {
|
||||
Session session = openSession();
|
||||
session.beginTransaction();
|
||||
{
|
||||
Material material = session.get( Material.class, 1 );
|
||||
assertEquals( "plastic", material.getName() );
|
||||
|
||||
// Material#containedSizesFromCombined is mapped with lazy="true"
|
||||
assertFalse( Hibernate.isInitialized( material.getContainedSizesFromCombined() ) );
|
||||
assertEquals( 1, material.getContainedSizesFromCombined().size() );
|
||||
assertTrue( Hibernate.isInitialized( material.getContainedSizesFromCombined() ) );
|
||||
|
||||
ContainedSize containedSize = material.getContainedSizesFromCombined().iterator().next();
|
||||
assertFalse( Hibernate.isInitialized( containedSize.getSize() ) );
|
||||
assertEquals( "medium", containedSize.getSize().getName() );
|
||||
|
||||
Building building = session.get( Building.class, 1 );
|
||||
|
||||
// building.ratingsFromCombined is mapped with lazy="true"
|
||||
assertFalse( Hibernate.isInitialized( building.getContainedRatingsFromCombined() ) );
|
||||
assertEquals( 1, building.getContainedRatingsFromCombined().size() );
|
||||
assertTrue( Hibernate.isInitialized( building.getContainedRatingsFromCombined() ) );
|
||||
ContainedRating containedRating = building.getContainedRatingsFromCombined().iterator().next();
|
||||
assertFalse( Hibernate.isInitialized( containedRating.getRating() ) );
|
||||
assertEquals( "high", containedRating.getRating().getName() );
|
||||
|
||||
// Building#containedSizesFromCombined is mapped with lazy="true"
|
||||
assertFalse( Hibernate.isInitialized( building.getContainedSizesFromCombined() ) );
|
||||
assertEquals( 1, building.getContainedSizesFromCombined().size() );
|
||||
assertTrue( Hibernate.isInitialized( building.getContainedSizesFromCombined() ) );
|
||||
containedSize = building.getContainedSizesFromCombined().iterator().next();
|
||||
assertFalse( Hibernate.isInitialized( containedSize.getSize() ) );
|
||||
assertEquals( "small", containedSize.getSize().getName() );
|
||||
}
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
}
|
||||
|
||||
public static class Material {
|
||||
private int id;
|
||||
|
||||
private String name;
|
||||
private Set<ContainedSize> containedSizesFromCombined = new HashSet<>();
|
||||
private List<Rating> mediumOrHighRatingsFromCombined = new ArrayList<>();
|
||||
private Set<ContainedRating> containedRatings = new HashSet<>();
|
||||
|
||||
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<ContainedSize> getContainedSizesFromCombined() {
|
||||
return containedSizesFromCombined;
|
||||
}
|
||||
public void setContainedSizesFromCombined(Set<ContainedSize> containedSizesFromCombined) {
|
||||
this.containedSizesFromCombined = containedSizesFromCombined;
|
||||
}
|
||||
|
||||
public Set<ContainedRating> getContainedRatings() {
|
||||
return containedRatings;
|
||||
}
|
||||
public void setContainedRatings(Set<ContainedRating> containedRatings) {
|
||||
this.containedRatings = containedRatings;
|
||||
}
|
||||
}
|
||||
|
||||
public static class Building {
|
||||
private int id;
|
||||
private String name;
|
||||
private Set<ContainedSize> containedSizesFromCombined = new HashSet<>();
|
||||
private Set<ContainedRating> containedRatingsFromCombined = new HashSet<>();
|
||||
private List<Rating> 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<ContainedSize> getContainedSizesFromCombined() {
|
||||
return containedSizesFromCombined;
|
||||
}
|
||||
public void setContainedSizesFromCombined(Set<ContainedSize> containedSizesFromCombined) {
|
||||
this.containedSizesFromCombined = containedSizesFromCombined;
|
||||
}
|
||||
|
||||
public Set<ContainedRating> getContainedRatingsFromCombined() {
|
||||
return containedRatingsFromCombined;
|
||||
}
|
||||
public void setContainedRatingsFromCombined(Set<ContainedRating> containedRatingsFromCombined) {
|
||||
this.containedRatingsFromCombined = containedRatingsFromCombined;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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 ContainedSize {
|
||||
private Size size;
|
||||
|
||||
public Size getSize() {
|
||||
return size;
|
||||
}
|
||||
public void setSize(Size size) {
|
||||
this.size = size;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
public static class ContainedRating {
|
||||
private Rating rating;
|
||||
|
||||
public Rating getRating() {
|
||||
return rating;
|
||||
}
|
||||
public void setRating(Rating rating) {
|
||||
this.rating = rating;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue