HHH-13011 : test cases
This commit is contained in:
parent
4735c2d5aa
commit
d1a40af1b7
|
@ -0,0 +1,213 @@
|
|||
/*
|
||||
* 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.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.JoinTable;
|
||||
import javax.persistence.ManyToMany;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Table;
|
||||
|
||||
import org.hibernate.annotations.Where;
|
||||
import org.hibernate.annotations.WhereJoinTable;
|
||||
import org.hibernate.cfg.AvailableSettings;
|
||||
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* Tests association collections with AvailableSettings.USE_ENTITY_WHERE_CLAUSE_FOR_COLLECTIONS = false
|
||||
*
|
||||
* @author Gail Badner
|
||||
*/
|
||||
public class EagerToManyWhereDontUseClassWhereTest extends BaseNonConfigCoreFunctionalTestCase {
|
||||
|
||||
@Override
|
||||
protected Class[] getAnnotatedClasses() {
|
||||
return new Class[] { Product.class, Category.class };
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void addSettings(Map settings) {
|
||||
settings.put( AvailableSettings.USE_ENTITY_WHERE_CLAUSE_FOR_COLLECTIONS, "false" );
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue( jiraKey = "HHH-13011" )
|
||||
public void testAssociatedWhereClause() {
|
||||
|
||||
Product product = new Product();
|
||||
Category flowers = new Category();
|
||||
flowers.id = 1;
|
||||
flowers.name = "flowers";
|
||||
flowers.description = "FLOWERS";
|
||||
product.categoriesOneToMany.add( flowers );
|
||||
product.categoriesWithDescOneToMany.add( flowers );
|
||||
product.categoriesManyToMany.add( flowers );
|
||||
product.categoriesWithDescManyToMany.add( flowers );
|
||||
product.categoriesWithDescIdLt4ManyToMany.add( flowers );
|
||||
Category vegetables = new Category();
|
||||
vegetables.id = 2;
|
||||
vegetables.name = "vegetables";
|
||||
vegetables.description = "VEGETABLES";
|
||||
product.categoriesOneToMany.add( vegetables );
|
||||
product.categoriesWithDescOneToMany.add( vegetables );
|
||||
product.categoriesManyToMany.add( vegetables );
|
||||
product.categoriesWithDescManyToMany.add( vegetables );
|
||||
product.categoriesWithDescIdLt4ManyToMany.add( vegetables );
|
||||
Category dogs = new Category();
|
||||
dogs.id = 3;
|
||||
dogs.name = "dogs";
|
||||
dogs.description = null;
|
||||
product.categoriesOneToMany.add( dogs );
|
||||
product.categoriesWithDescOneToMany.add( dogs );
|
||||
product.categoriesManyToMany.add( dogs );
|
||||
product.categoriesWithDescManyToMany.add( dogs );
|
||||
product.categoriesWithDescIdLt4ManyToMany.add( dogs );
|
||||
Category building = new Category();
|
||||
building.id = 4;
|
||||
building.name = "building";
|
||||
building.description = "BUILDING";
|
||||
product.categoriesOneToMany.add( building );
|
||||
product.categoriesWithDescOneToMany.add( building );
|
||||
product.categoriesManyToMany.add( building );
|
||||
product.categoriesWithDescManyToMany.add( building );
|
||||
product.categoriesWithDescIdLt4ManyToMany.add( building );
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
session.persist( flowers );
|
||||
session.persist( vegetables );
|
||||
session.persist( dogs );
|
||||
session.persist( building );
|
||||
session.persist( product );
|
||||
}
|
||||
);
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
Product p = session.get( Product.class, product.id );
|
||||
assertNotNull( p );
|
||||
assertEquals( 4, p.categoriesOneToMany.size() );
|
||||
checkIds( p.categoriesOneToMany, new Integer[] { 1, 2, 3, 4 } );
|
||||
assertEquals( 3, p.categoriesWithDescOneToMany.size() );
|
||||
checkIds( p.categoriesWithDescOneToMany, new Integer[] { 1, 2, 4 } );
|
||||
assertEquals( 4, p.categoriesManyToMany.size() );
|
||||
checkIds( p.categoriesManyToMany, new Integer[] { 1, 2, 3, 4 } );
|
||||
assertEquals( 3, p.categoriesWithDescManyToMany.size() );
|
||||
checkIds( p.categoriesWithDescManyToMany, new Integer[] { 1, 2, 4 } );
|
||||
assertEquals( 2, p.categoriesWithDescIdLt4ManyToMany.size() );
|
||||
checkIds( p.categoriesWithDescIdLt4ManyToMany, new Integer[] { 1, 2 } );
|
||||
}
|
||||
);
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
Category c = session.get( Category.class, flowers.id );
|
||||
assertNotNull( c );
|
||||
c.inactive = true;
|
||||
}
|
||||
);
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
Category c = session.get( Category.class, flowers.id );
|
||||
assertNull( c );
|
||||
}
|
||||
);
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
Product p = session.get( Product.class, product.id );
|
||||
assertNotNull( p );
|
||||
assertEquals( 4, p.categoriesOneToMany.size() );
|
||||
checkIds( p.categoriesOneToMany, new Integer[] { 1, 2, 3, 4 } );
|
||||
assertEquals( 3, p.categoriesWithDescOneToMany.size() );
|
||||
checkIds( p.categoriesWithDescOneToMany, new Integer[] { 1, 2, 4 } );
|
||||
assertEquals( 4, p.categoriesManyToMany.size() );
|
||||
checkIds( p.categoriesManyToMany, new Integer[] { 1, 2, 3, 4 } );
|
||||
assertEquals( 3, p.categoriesWithDescManyToMany.size() );
|
||||
checkIds( p.categoriesWithDescManyToMany, new Integer[] { 1, 2, 4 } );
|
||||
assertEquals( 2, p.categoriesWithDescIdLt4ManyToMany.size() );
|
||||
checkIds( p.categoriesWithDescIdLt4ManyToMany, new Integer[] { 1, 2 } );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
private void checkIds(Set<Category> categories, Integer[] expectedIds) {
|
||||
final Set<Integer> expectedIdSet = new HashSet<>( Arrays.asList( expectedIds ) );
|
||||
for ( Category category : categories ) {
|
||||
expectedIdSet.remove( category.id );
|
||||
}
|
||||
assertTrue( expectedIdSet.isEmpty() );
|
||||
}
|
||||
|
||||
@Entity(name = "Product")
|
||||
public static class Product {
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private int id;
|
||||
|
||||
@OneToMany(fetch = FetchType.EAGER)
|
||||
@JoinColumn
|
||||
private Set<Category> categoriesOneToMany = new HashSet<>();
|
||||
|
||||
@OneToMany(fetch = FetchType.EAGER)
|
||||
@JoinColumn
|
||||
@Where( clause = "description is not null" )
|
||||
private Set<Category> categoriesWithDescOneToMany = new HashSet<>();
|
||||
|
||||
@ManyToMany(fetch = FetchType.EAGER)
|
||||
@JoinTable(name = "categoriesManyToMany")
|
||||
private Set<Category> categoriesManyToMany = new HashSet<>();
|
||||
|
||||
@ManyToMany(fetch = FetchType.EAGER)
|
||||
@JoinTable(name = "categoriesWithDescManyToMany")
|
||||
@Where( clause = "description is not null" )
|
||||
private Set<Category> categoriesWithDescManyToMany = new HashSet<>();
|
||||
|
||||
@ManyToMany(fetch = FetchType.EAGER)
|
||||
@JoinTable(name = "categoriesWithDescIdLt4MToM", inverseJoinColumns = { @JoinColumn( name = "categoryId" )})
|
||||
@Where( clause = "description is not null" )
|
||||
@WhereJoinTable( clause = "categoryId < 4")
|
||||
private Set<Category> categoriesWithDescIdLt4ManyToMany = new HashSet<>();
|
||||
}
|
||||
|
||||
@Entity(name = "Category")
|
||||
@Table(name = "CATEGORY")
|
||||
@Where(clause = "inactive = 0")
|
||||
public static class Category {
|
||||
@Id
|
||||
private int id;
|
||||
|
||||
private String name;
|
||||
|
||||
private String description;
|
||||
|
||||
private boolean inactive;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,208 @@
|
|||
/*
|
||||
* 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.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.JoinTable;
|
||||
import javax.persistence.ManyToMany;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Table;
|
||||
|
||||
import org.hibernate.annotations.Where;
|
||||
import org.hibernate.annotations.WhereJoinTable;
|
||||
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* Tests association collections with default AvailableSettings.USE_ENTITY_WHERE_CLAUSE_FOR_COLLECTIONS,
|
||||
* which is true.
|
||||
*
|
||||
* @author Gail Badner
|
||||
*/
|
||||
public class EagerToManyWhereTest extends BaseNonConfigCoreFunctionalTestCase {
|
||||
|
||||
@Override
|
||||
protected Class[] getAnnotatedClasses() {
|
||||
return new Class[] { Product.class, Category.class };
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue( jiraKey = "HHH-13011" )
|
||||
public void testAssociatedWhereClause() {
|
||||
|
||||
Product product = new Product();
|
||||
Category flowers = new Category();
|
||||
flowers.id = 1;
|
||||
flowers.name = "flowers";
|
||||
flowers.description = "FLOWERS";
|
||||
product.categoriesOneToMany.add( flowers );
|
||||
product.categoriesWithDescOneToMany.add( flowers );
|
||||
product.categoriesManyToMany.add( flowers );
|
||||
product.categoriesWithDescManyToMany.add( flowers );
|
||||
product.categoriesWithDescIdLt4ManyToMany.add( flowers );
|
||||
Category vegetables = new Category();
|
||||
vegetables.id = 2;
|
||||
vegetables.name = "vegetables";
|
||||
vegetables.description = "VEGETABLES";
|
||||
product.categoriesOneToMany.add( vegetables );
|
||||
product.categoriesWithDescOneToMany.add( vegetables );
|
||||
product.categoriesManyToMany.add( vegetables );
|
||||
product.categoriesWithDescManyToMany.add( vegetables );
|
||||
product.categoriesWithDescIdLt4ManyToMany.add( vegetables );
|
||||
Category dogs = new Category();
|
||||
dogs.id = 3;
|
||||
dogs.name = "dogs";
|
||||
dogs.description = null;
|
||||
product.categoriesOneToMany.add( dogs );
|
||||
product.categoriesWithDescOneToMany.add( dogs );
|
||||
product.categoriesManyToMany.add( dogs );
|
||||
product.categoriesWithDescManyToMany.add( dogs );
|
||||
product.categoriesWithDescIdLt4ManyToMany.add( dogs );
|
||||
Category building = new Category();
|
||||
building.id = 4;
|
||||
building.name = "building";
|
||||
building.description = "BUILDING";
|
||||
product.categoriesOneToMany.add( building );
|
||||
product.categoriesWithDescOneToMany.add( building );
|
||||
product.categoriesManyToMany.add( building );
|
||||
product.categoriesWithDescManyToMany.add( building );
|
||||
product.categoriesWithDescIdLt4ManyToMany.add( building );
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
session.persist( flowers );
|
||||
session.persist( vegetables );
|
||||
session.persist( dogs );
|
||||
session.persist( building );
|
||||
session.persist( product );
|
||||
}
|
||||
);
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
Product p = session.get( Product.class, product.id );
|
||||
assertNotNull( p );
|
||||
assertEquals( 4, p.categoriesOneToMany.size() );
|
||||
checkIds( p.categoriesOneToMany, new Integer[] { 1, 2, 3, 4 } );
|
||||
assertEquals( 3, p.categoriesWithDescOneToMany.size() );
|
||||
checkIds( p.categoriesWithDescOneToMany, new Integer[] { 1, 2, 4 } );
|
||||
assertEquals( 4, p.categoriesManyToMany.size() );
|
||||
checkIds( p.categoriesManyToMany, new Integer[] { 1, 2, 3, 4 } );
|
||||
assertEquals( 3, p.categoriesWithDescManyToMany.size() );
|
||||
checkIds( p.categoriesWithDescManyToMany, new Integer[] { 1, 2, 4 } );
|
||||
assertEquals( 2, p.categoriesWithDescIdLt4ManyToMany.size() );
|
||||
checkIds( p.categoriesWithDescIdLt4ManyToMany, new Integer[] { 1, 2 } );
|
||||
}
|
||||
);
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
Category c = session.get( Category.class, flowers.id );
|
||||
assertNotNull( c );
|
||||
c.inactive = true;
|
||||
}
|
||||
);
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
Category c = session.get( Category.class, flowers.id );
|
||||
assertNull( c );
|
||||
}
|
||||
);
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
Product p = session.get( Product.class, product.id );
|
||||
assertNotNull( p );
|
||||
assertEquals( 3, p.categoriesOneToMany.size() );
|
||||
checkIds( p.categoriesOneToMany, new Integer[] { 2, 3, 4 } );
|
||||
assertEquals( 2, p.categoriesWithDescOneToMany.size() );
|
||||
checkIds( p.categoriesWithDescOneToMany, new Integer[] { 2, 4 } );
|
||||
assertEquals( 3, p.categoriesManyToMany.size() );
|
||||
checkIds( p.categoriesManyToMany, new Integer[] { 2, 3, 4 } );
|
||||
assertEquals( 2, p.categoriesWithDescManyToMany.size() );
|
||||
checkIds( p.categoriesWithDescManyToMany, new Integer[] { 2, 4 } );
|
||||
assertEquals( 1, p.categoriesWithDescIdLt4ManyToMany.size() );
|
||||
checkIds( p.categoriesWithDescIdLt4ManyToMany, new Integer[] { 2 } );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
private void checkIds(Set<Category> categories, Integer[] expectedIds) {
|
||||
final Set<Integer> expectedIdSet = new HashSet<>( Arrays.asList( expectedIds ) );
|
||||
for ( Category category : categories ) {
|
||||
expectedIdSet.remove( category.id );
|
||||
}
|
||||
assertTrue( expectedIdSet.isEmpty() );
|
||||
}
|
||||
|
||||
@Entity(name = "Product")
|
||||
public static class Product {
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private int id;
|
||||
|
||||
@OneToMany(fetch = FetchType.EAGER)
|
||||
@JoinColumn
|
||||
private Set<Category> categoriesOneToMany = new HashSet<>();
|
||||
|
||||
@OneToMany(fetch = FetchType.EAGER)
|
||||
@JoinColumn
|
||||
@Where( clause = "description is not null" )
|
||||
private Set<Category> categoriesWithDescOneToMany = new HashSet<>();
|
||||
|
||||
@ManyToMany(fetch = FetchType.EAGER)
|
||||
@JoinTable(name = "categoriesManyToMany")
|
||||
private Set<Category> categoriesManyToMany = new HashSet<>();
|
||||
|
||||
@ManyToMany(fetch = FetchType.EAGER)
|
||||
@JoinTable(name = "categoriesWithDescManyToMany")
|
||||
@Where( clause = "description is not null" )
|
||||
private Set<Category> categoriesWithDescManyToMany = new HashSet<>();
|
||||
|
||||
@ManyToMany(fetch = FetchType.EAGER)
|
||||
@JoinTable(name = "categoriesWithDescIdLt4MToM", inverseJoinColumns = { @JoinColumn( name = "categoryId" )})
|
||||
@Where( clause = "description is not null" )
|
||||
@WhereJoinTable( clause = "categoryId < 4")
|
||||
private Set<Category> categoriesWithDescIdLt4ManyToMany = new HashSet<>();
|
||||
}
|
||||
|
||||
@Entity(name = "Category")
|
||||
@Table(name = "CATEGORY")
|
||||
@Where(clause = "inactive = 0")
|
||||
public static class Category {
|
||||
@Id
|
||||
private int id;
|
||||
|
||||
private String name;
|
||||
|
||||
private String description;
|
||||
|
||||
private boolean inactive;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,213 @@
|
|||
/*
|
||||
* 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.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.JoinTable;
|
||||
import javax.persistence.ManyToMany;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Table;
|
||||
|
||||
import org.hibernate.annotations.Where;
|
||||
import org.hibernate.annotations.WhereJoinTable;
|
||||
import org.hibernate.cfg.AvailableSettings;
|
||||
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* Tests association collections with AvailableSettings.USE_ENTITY_WHERE_CLAUSE_FOR_COLLECTIONS = true
|
||||
*
|
||||
* @author Gail Badner
|
||||
*/
|
||||
public class EagerToManyWhereUseClassWhereTest extends BaseNonConfigCoreFunctionalTestCase {
|
||||
|
||||
@Override
|
||||
protected Class[] getAnnotatedClasses() {
|
||||
return new Class[] { Product.class, Category.class };
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void addSettings(Map settings) {
|
||||
settings.put( AvailableSettings.USE_ENTITY_WHERE_CLAUSE_FOR_COLLECTIONS, "true" );
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue( jiraKey = "HHH-13011" )
|
||||
public void testAssociatedWhereClause() {
|
||||
|
||||
Product product = new Product();
|
||||
Category flowers = new Category();
|
||||
flowers.id = 1;
|
||||
flowers.name = "flowers";
|
||||
flowers.description = "FLOWERS";
|
||||
product.categoriesOneToMany.add( flowers );
|
||||
product.categoriesWithDescOneToMany.add( flowers );
|
||||
product.categoriesManyToMany.add( flowers );
|
||||
product.categoriesWithDescManyToMany.add( flowers );
|
||||
product.categoriesWithDescIdLt4ManyToMany.add( flowers );
|
||||
Category vegetables = new Category();
|
||||
vegetables.id = 2;
|
||||
vegetables.name = "vegetables";
|
||||
vegetables.description = "VEGETABLES";
|
||||
product.categoriesOneToMany.add( vegetables );
|
||||
product.categoriesWithDescOneToMany.add( vegetables );
|
||||
product.categoriesManyToMany.add( vegetables );
|
||||
product.categoriesWithDescManyToMany.add( vegetables );
|
||||
product.categoriesWithDescIdLt4ManyToMany.add( vegetables );
|
||||
Category dogs = new Category();
|
||||
dogs.id = 3;
|
||||
dogs.name = "dogs";
|
||||
dogs.description = null;
|
||||
product.categoriesOneToMany.add( dogs );
|
||||
product.categoriesWithDescOneToMany.add( dogs );
|
||||
product.categoriesManyToMany.add( dogs );
|
||||
product.categoriesWithDescManyToMany.add( dogs );
|
||||
product.categoriesWithDescIdLt4ManyToMany.add( dogs );
|
||||
Category building = new Category();
|
||||
building.id = 4;
|
||||
building.name = "building";
|
||||
building.description = "BUILDING";
|
||||
product.categoriesOneToMany.add( building );
|
||||
product.categoriesWithDescOneToMany.add( building );
|
||||
product.categoriesManyToMany.add( building );
|
||||
product.categoriesWithDescManyToMany.add( building );
|
||||
product.categoriesWithDescIdLt4ManyToMany.add( building );
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
session.persist( flowers );
|
||||
session.persist( vegetables );
|
||||
session.persist( dogs );
|
||||
session.persist( building );
|
||||
session.persist( product );
|
||||
}
|
||||
);
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
Product p = session.get( Product.class, product.id );
|
||||
assertNotNull( p );
|
||||
assertEquals( 4, p.categoriesOneToMany.size() );
|
||||
checkIds( p.categoriesOneToMany, new Integer[] { 1, 2, 3, 4 } );
|
||||
assertEquals( 3, p.categoriesWithDescOneToMany.size() );
|
||||
checkIds( p.categoriesWithDescOneToMany, new Integer[] { 1, 2, 4 } );
|
||||
assertEquals( 4, p.categoriesManyToMany.size() );
|
||||
checkIds( p.categoriesManyToMany, new Integer[] { 1, 2, 3, 4 } );
|
||||
assertEquals( 3, p.categoriesWithDescManyToMany.size() );
|
||||
checkIds( p.categoriesWithDescManyToMany, new Integer[] { 1, 2, 4 } );
|
||||
assertEquals( 2, p.categoriesWithDescIdLt4ManyToMany.size() );
|
||||
checkIds( p.categoriesWithDescIdLt4ManyToMany, new Integer[] { 1, 2 } );
|
||||
}
|
||||
);
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
Category c = session.get( Category.class, flowers.id );
|
||||
assertNotNull( c );
|
||||
c.inactive = true;
|
||||
}
|
||||
);
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
Category c = session.get( Category.class, flowers.id );
|
||||
assertNull( c );
|
||||
}
|
||||
);
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
Product p = session.get( Product.class, product.id );
|
||||
assertNotNull( p );
|
||||
assertEquals( 3, p.categoriesOneToMany.size() );
|
||||
checkIds( p.categoriesOneToMany, new Integer[] { 2, 3, 4 } );
|
||||
assertEquals( 2, p.categoriesWithDescOneToMany.size() );
|
||||
checkIds( p.categoriesWithDescOneToMany, new Integer[] { 2, 4 } );
|
||||
assertEquals( 3, p.categoriesManyToMany.size() );
|
||||
checkIds( p.categoriesManyToMany, new Integer[] { 2, 3, 4 } );
|
||||
assertEquals( 2, p.categoriesWithDescManyToMany.size() );
|
||||
checkIds( p.categoriesWithDescManyToMany, new Integer[] { 2, 4 } );
|
||||
assertEquals( 1, p.categoriesWithDescIdLt4ManyToMany.size() );
|
||||
checkIds( p.categoriesWithDescIdLt4ManyToMany, new Integer[] { 2 } );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
private void checkIds(Set<Category> categories, Integer[] expectedIds) {
|
||||
final Set<Integer> expectedIdSet = new HashSet<>( Arrays.asList( expectedIds ) );
|
||||
for ( Category category : categories ) {
|
||||
expectedIdSet.remove( category.id );
|
||||
}
|
||||
assertTrue( expectedIdSet.isEmpty() );
|
||||
}
|
||||
|
||||
@Entity(name = "Product")
|
||||
public static class Product {
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private int id;
|
||||
|
||||
@OneToMany(fetch = FetchType.EAGER)
|
||||
@JoinColumn
|
||||
private Set<Category> categoriesOneToMany = new HashSet<>();
|
||||
|
||||
@OneToMany(fetch = FetchType.EAGER)
|
||||
@JoinColumn
|
||||
@Where( clause = "description is not null" )
|
||||
private Set<Category> categoriesWithDescOneToMany = new HashSet<>();
|
||||
|
||||
@ManyToMany(fetch = FetchType.EAGER)
|
||||
@JoinTable(name = "categoriesManyToMany")
|
||||
private Set<Category> categoriesManyToMany = new HashSet<>();
|
||||
|
||||
@ManyToMany(fetch = FetchType.EAGER)
|
||||
@JoinTable(name = "categoriesWithDescManyToMany")
|
||||
@Where( clause = "description is not null" )
|
||||
private Set<Category> categoriesWithDescManyToMany = new HashSet<>();
|
||||
|
||||
@ManyToMany(fetch = FetchType.EAGER)
|
||||
@JoinTable(name = "categoriesWithDescIdLt4MToM", inverseJoinColumns = { @JoinColumn( name = "categoryId" )})
|
||||
@Where( clause = "description is not null" )
|
||||
@WhereJoinTable( clause = "categoryId < 4")
|
||||
private Set<Category> categoriesWithDescIdLt4ManyToMany = new HashSet<>();
|
||||
}
|
||||
|
||||
@Entity(name = "Category")
|
||||
@Table(name = "CATEGORY")
|
||||
@Where(clause = "inactive = 0")
|
||||
public static class Category {
|
||||
@Id
|
||||
private int id;
|
||||
|
||||
private String name;
|
||||
|
||||
private String description;
|
||||
|
||||
private boolean inactive;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,213 @@
|
|||
/*
|
||||
* 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.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.JoinTable;
|
||||
import javax.persistence.ManyToMany;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Table;
|
||||
|
||||
import org.hibernate.annotations.Where;
|
||||
import org.hibernate.annotations.WhereJoinTable;
|
||||
import org.hibernate.cfg.AvailableSettings;
|
||||
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* Tests association collections with AvailableSettings.USE_ENTITY_WHERE_CLAUSE_FOR_COLLECTIONS = false
|
||||
*
|
||||
* @author Gail Badner
|
||||
*/
|
||||
public class LazyToManyWhereDontUseClassWhereTest extends BaseNonConfigCoreFunctionalTestCase {
|
||||
|
||||
@Override
|
||||
protected Class[] getAnnotatedClasses() {
|
||||
return new Class[] { Product.class, Category.class };
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void addSettings(Map settings) {
|
||||
settings.put( AvailableSettings.USE_ENTITY_WHERE_CLAUSE_FOR_COLLECTIONS, "false" );
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue( jiraKey = "HHH-13011" )
|
||||
public void testAssociatedWhereClause() {
|
||||
|
||||
Product product = new Product();
|
||||
Category flowers = new Category();
|
||||
flowers.id = 1;
|
||||
flowers.name = "flowers";
|
||||
flowers.description = "FLOWERS";
|
||||
product.categoriesOneToMany.add( flowers );
|
||||
product.categoriesWithDescOneToMany.add( flowers );
|
||||
product.categoriesManyToMany.add( flowers );
|
||||
product.categoriesWithDescManyToMany.add( flowers );
|
||||
product.categoriesWithDescIdLt4ManyToMany.add( flowers );
|
||||
Category vegetables = new Category();
|
||||
vegetables.id = 2;
|
||||
vegetables.name = "vegetables";
|
||||
vegetables.description = "VEGETABLES";
|
||||
product.categoriesOneToMany.add( vegetables );
|
||||
product.categoriesWithDescOneToMany.add( vegetables );
|
||||
product.categoriesManyToMany.add( vegetables );
|
||||
product.categoriesWithDescManyToMany.add( vegetables );
|
||||
product.categoriesWithDescIdLt4ManyToMany.add( vegetables );
|
||||
Category dogs = new Category();
|
||||
dogs.id = 3;
|
||||
dogs.name = "dogs";
|
||||
dogs.description = null;
|
||||
product.categoriesOneToMany.add( dogs );
|
||||
product.categoriesWithDescOneToMany.add( dogs );
|
||||
product.categoriesManyToMany.add( dogs );
|
||||
product.categoriesWithDescManyToMany.add( dogs );
|
||||
product.categoriesWithDescIdLt4ManyToMany.add( dogs );
|
||||
Category building = new Category();
|
||||
building.id = 4;
|
||||
building.name = "building";
|
||||
building.description = "BUILDING";
|
||||
product.categoriesOneToMany.add( building );
|
||||
product.categoriesWithDescOneToMany.add( building );
|
||||
product.categoriesManyToMany.add( building );
|
||||
product.categoriesWithDescManyToMany.add( building );
|
||||
product.categoriesWithDescIdLt4ManyToMany.add( building );
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
session.persist( flowers );
|
||||
session.persist( vegetables );
|
||||
session.persist( dogs );
|
||||
session.persist( building );
|
||||
session.persist( product );
|
||||
}
|
||||
);
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
Product p = session.get( Product.class, product.id );
|
||||
assertNotNull( p );
|
||||
assertEquals( 4, p.categoriesOneToMany.size() );
|
||||
checkIds( p.categoriesOneToMany, new Integer[] { 1, 2, 3, 4 } );
|
||||
assertEquals( 3, p.categoriesWithDescOneToMany.size() );
|
||||
checkIds( p.categoriesWithDescOneToMany, new Integer[] { 1, 2, 4 } );
|
||||
assertEquals( 4, p.categoriesManyToMany.size() );
|
||||
checkIds( p.categoriesManyToMany, new Integer[] { 1, 2, 3, 4 } );
|
||||
assertEquals( 3, p.categoriesWithDescManyToMany.size() );
|
||||
checkIds( p.categoriesWithDescManyToMany, new Integer[] { 1, 2, 4 } );
|
||||
assertEquals( 2, p.categoriesWithDescIdLt4ManyToMany.size() );
|
||||
checkIds( p.categoriesWithDescIdLt4ManyToMany, new Integer[] { 1, 2 } );
|
||||
}
|
||||
);
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
Category c = session.get( Category.class, flowers.id );
|
||||
assertNotNull( c );
|
||||
c.inactive = true;
|
||||
}
|
||||
);
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
Category c = session.get( Category.class, flowers.id );
|
||||
assertNull( c );
|
||||
}
|
||||
);
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
Product p = session.get( Product.class, product.id );
|
||||
assertNotNull( p );
|
||||
assertEquals( 4, p.categoriesOneToMany.size() );
|
||||
checkIds( p.categoriesOneToMany, new Integer[] { 1, 2, 3, 4 } );
|
||||
assertEquals( 3, p.categoriesWithDescOneToMany.size() );
|
||||
checkIds( p.categoriesWithDescOneToMany, new Integer[] { 1, 2, 4 } );
|
||||
assertEquals( 4, p.categoriesManyToMany.size() );
|
||||
checkIds( p.categoriesManyToMany, new Integer[] { 1, 2, 3, 4 } );
|
||||
assertEquals( 3, p.categoriesWithDescManyToMany.size() );
|
||||
checkIds( p.categoriesWithDescManyToMany, new Integer[] { 1, 2, 4 } );
|
||||
assertEquals( 2, p.categoriesWithDescIdLt4ManyToMany.size() );
|
||||
checkIds( p.categoriesWithDescIdLt4ManyToMany, new Integer[] { 1, 2 } );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
private void checkIds(Set<Category> categories, Integer[] expectedIds) {
|
||||
final Set<Integer> expectedIdSet = new HashSet<>( Arrays.asList( expectedIds ) );
|
||||
for ( Category category : categories ) {
|
||||
expectedIdSet.remove( category.id );
|
||||
}
|
||||
assertTrue( expectedIdSet.isEmpty() );
|
||||
}
|
||||
|
||||
@Entity(name = "Product")
|
||||
public static class Product {
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private int id;
|
||||
|
||||
@OneToMany(fetch = FetchType.LAZY)
|
||||
@JoinColumn
|
||||
private Set<Category> categoriesOneToMany = new HashSet<>();
|
||||
|
||||
@OneToMany(fetch = FetchType.LAZY)
|
||||
@JoinColumn
|
||||
@Where( clause = "description is not null" )
|
||||
private Set<Category> categoriesWithDescOneToMany = new HashSet<>();
|
||||
|
||||
@ManyToMany(fetch = FetchType.LAZY)
|
||||
@JoinTable(name = "categoriesManyToMany")
|
||||
private Set<Category> categoriesManyToMany = new HashSet<>();
|
||||
|
||||
@ManyToMany(fetch = FetchType.LAZY)
|
||||
@JoinTable(name = "categoriesWithDescManyToMany")
|
||||
@Where( clause = "description is not null" )
|
||||
private Set<Category> categoriesWithDescManyToMany = new HashSet<>();
|
||||
|
||||
@ManyToMany(fetch = FetchType.LAZY)
|
||||
@JoinTable(name = "categoriesWithDescIdLt4MToM", inverseJoinColumns = { @JoinColumn( name = "categoryId" )})
|
||||
@Where( clause = "description is not null" )
|
||||
@WhereJoinTable( clause = "categoryId < 4")
|
||||
private Set<Category> categoriesWithDescIdLt4ManyToMany = new HashSet<>();
|
||||
}
|
||||
|
||||
@Entity(name = "Category")
|
||||
@Table(name = "CATEGORY")
|
||||
@Where(clause = "inactive = 0")
|
||||
public static class Category {
|
||||
@Id
|
||||
private int id;
|
||||
|
||||
private String name;
|
||||
|
||||
private String description;
|
||||
|
||||
private boolean inactive;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,207 @@
|
|||
/*
|
||||
* 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.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.JoinTable;
|
||||
import javax.persistence.ManyToMany;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Table;
|
||||
|
||||
import org.hibernate.annotations.Where;
|
||||
import org.hibernate.annotations.WhereJoinTable;
|
||||
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* Tests association collections with default AvailableSettings.USE_ENTITY_WHERE_CLAUSE_FOR_COLLECTIONS,
|
||||
* which is true.
|
||||
*
|
||||
* @author Gail Badner
|
||||
*/
|
||||
public class LazyToManyWhereTest extends BaseNonConfigCoreFunctionalTestCase {
|
||||
|
||||
@Override
|
||||
protected Class[] getAnnotatedClasses() {
|
||||
return new Class[] { Product.class, Category.class };
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue( jiraKey = "HHH-13011" )
|
||||
public void testAssociatedWhereClause() {
|
||||
|
||||
Product product = new Product();
|
||||
Category flowers = new Category();
|
||||
flowers.id = 1;
|
||||
flowers.name = "flowers";
|
||||
flowers.description = "FLOWERS";
|
||||
product.categoriesOneToMany.add( flowers );
|
||||
product.categoriesWithDescOneToMany.add( flowers );
|
||||
product.categoriesManyToMany.add( flowers );
|
||||
product.categoriesWithDescManyToMany.add( flowers );
|
||||
product.categoriesWithDescIdLt4ManyToMany.add( flowers );
|
||||
Category vegetables = new Category();
|
||||
vegetables.id = 2;
|
||||
vegetables.name = "vegetables";
|
||||
vegetables.description = "VEGETABLES";
|
||||
product.categoriesOneToMany.add( vegetables );
|
||||
product.categoriesWithDescOneToMany.add( vegetables );
|
||||
product.categoriesManyToMany.add( vegetables );
|
||||
product.categoriesWithDescManyToMany.add( vegetables );
|
||||
product.categoriesWithDescIdLt4ManyToMany.add( vegetables );
|
||||
Category dogs = new Category();
|
||||
dogs.id = 3;
|
||||
dogs.name = "dogs";
|
||||
dogs.description = null;
|
||||
product.categoriesOneToMany.add( dogs );
|
||||
product.categoriesWithDescOneToMany.add( dogs );
|
||||
product.categoriesManyToMany.add( dogs );
|
||||
product.categoriesWithDescManyToMany.add( dogs );
|
||||
product.categoriesWithDescIdLt4ManyToMany.add( dogs );
|
||||
Category building = new Category();
|
||||
building.id = 4;
|
||||
building.name = "building";
|
||||
building.description = "BUILDING";
|
||||
product.categoriesOneToMany.add( building );
|
||||
product.categoriesWithDescOneToMany.add( building );
|
||||
product.categoriesManyToMany.add( building );
|
||||
product.categoriesWithDescManyToMany.add( building );
|
||||
product.categoriesWithDescIdLt4ManyToMany.add( building );
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
session.persist( flowers );
|
||||
session.persist( vegetables );
|
||||
session.persist( dogs );
|
||||
session.persist( building );
|
||||
session.persist( product );
|
||||
}
|
||||
);
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
Product p = session.get( Product.class, product.id );
|
||||
assertNotNull( p );
|
||||
assertEquals( 4, p.categoriesOneToMany.size() );
|
||||
checkIds( p.categoriesOneToMany, new Integer[] { 1, 2, 3, 4 } );
|
||||
assertEquals( 3, p.categoriesWithDescOneToMany.size() );
|
||||
checkIds( p.categoriesWithDescOneToMany, new Integer[] { 1, 2, 4 } );
|
||||
assertEquals( 4, p.categoriesManyToMany.size() );
|
||||
checkIds( p.categoriesManyToMany, new Integer[] { 1, 2, 3, 4 } );
|
||||
assertEquals( 3, p.categoriesWithDescManyToMany.size() );
|
||||
checkIds( p.categoriesWithDescManyToMany, new Integer[] { 1, 2, 4 } );
|
||||
assertEquals( 2, p.categoriesWithDescIdLt4ManyToMany.size() );
|
||||
checkIds( p.categoriesWithDescIdLt4ManyToMany, new Integer[] { 1, 2 } );
|
||||
}
|
||||
);
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
Category c = session.get( Category.class, flowers.id );
|
||||
assertNotNull( c );
|
||||
c.inactive = true;
|
||||
}
|
||||
);
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
Category c = session.get( Category.class, flowers.id );
|
||||
assertNull( c );
|
||||
}
|
||||
);
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
Product p = session.get( Product.class, product.id );
|
||||
assertNotNull( p );
|
||||
assertEquals( 3, p.categoriesOneToMany.size() );
|
||||
checkIds( p.categoriesOneToMany, new Integer[] { 2, 3, 4 } );
|
||||
assertEquals( 2, p.categoriesWithDescOneToMany.size() );
|
||||
checkIds( p.categoriesWithDescOneToMany, new Integer[] { 2, 4 } );
|
||||
assertEquals( 3, p.categoriesManyToMany.size() );
|
||||
checkIds( p.categoriesManyToMany, new Integer[] { 2, 3, 4 } );
|
||||
assertEquals( 2, p.categoriesWithDescManyToMany.size() );
|
||||
checkIds( p.categoriesWithDescManyToMany, new Integer[] { 2, 4 } );
|
||||
assertEquals( 1, p.categoriesWithDescIdLt4ManyToMany.size() );
|
||||
checkIds( p.categoriesWithDescIdLt4ManyToMany, new Integer[] { 2 } );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
private void checkIds(Set<Category> categories, Integer[] expectedIds) {
|
||||
final Set<Integer> expectedIdSet = new HashSet<>( Arrays.asList( expectedIds ) );
|
||||
for ( Category category : categories ) {
|
||||
expectedIdSet.remove( category.id );
|
||||
}
|
||||
assertTrue( expectedIdSet.isEmpty() );
|
||||
}
|
||||
|
||||
@Entity(name = "Product")
|
||||
public static class Product {
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private int id;
|
||||
|
||||
@OneToMany(fetch = FetchType.LAZY)
|
||||
@JoinColumn
|
||||
private Set<Category> categoriesOneToMany = new HashSet<>();
|
||||
|
||||
@OneToMany(fetch = FetchType.LAZY)
|
||||
@JoinColumn
|
||||
@Where( clause = "description is not null" )
|
||||
private Set<Category> categoriesWithDescOneToMany = new HashSet<>();
|
||||
|
||||
@ManyToMany(fetch = FetchType.LAZY)
|
||||
@JoinTable(name = "categoriesManyToMany")
|
||||
private Set<Category> categoriesManyToMany = new HashSet<>();
|
||||
|
||||
@ManyToMany(fetch = FetchType.LAZY)
|
||||
@JoinTable(name = "categoriesWithDescManyToMany")
|
||||
@Where( clause = "description is not null" )
|
||||
private Set<Category> categoriesWithDescManyToMany = new HashSet<>();
|
||||
|
||||
@ManyToMany(fetch = FetchType.LAZY)
|
||||
@JoinTable(name = "categoriesWithDescIdLt4MToM", inverseJoinColumns = { @JoinColumn( name = "categoryId" )})
|
||||
@Where( clause = "description is not null" )
|
||||
@WhereJoinTable( clause = "categoryId < 4")
|
||||
private Set<Category> categoriesWithDescIdLt4ManyToMany = new HashSet<>();
|
||||
}
|
||||
|
||||
@Entity(name = "Category")
|
||||
@Table(name = "CATEGORY")
|
||||
@Where(clause = "inactive = 0")
|
||||
public static class Category {
|
||||
@Id
|
||||
private int id;
|
||||
|
||||
private String name;
|
||||
|
||||
private String description;
|
||||
|
||||
private boolean inactive;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,213 @@
|
|||
/*
|
||||
* 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.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.JoinTable;
|
||||
import javax.persistence.ManyToMany;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Table;
|
||||
|
||||
import org.hibernate.annotations.Where;
|
||||
import org.hibernate.annotations.WhereJoinTable;
|
||||
import org.hibernate.cfg.AvailableSettings;
|
||||
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* Tests association collections with AvailableSettings.USE_ENTITY_WHERE_CLAUSE_FOR_COLLECTIONS = true
|
||||
*
|
||||
* @author Gail Badner
|
||||
*/
|
||||
public class LazyToManyWhereUseClassWhereTest extends BaseNonConfigCoreFunctionalTestCase {
|
||||
|
||||
@Override
|
||||
protected Class[] getAnnotatedClasses() {
|
||||
return new Class[] { Product.class, Category.class };
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void addSettings(Map settings) {
|
||||
settings.put( AvailableSettings.USE_ENTITY_WHERE_CLAUSE_FOR_COLLECTIONS, "true" );
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue( jiraKey = "HHH-13011" )
|
||||
public void testAssociatedWhereClause() {
|
||||
|
||||
Product product = new Product();
|
||||
Category flowers = new Category();
|
||||
flowers.id = 1;
|
||||
flowers.name = "flowers";
|
||||
flowers.description = "FLOWERS";
|
||||
product.categoriesOneToMany.add( flowers );
|
||||
product.categoriesWithDescOneToMany.add( flowers );
|
||||
product.categoriesManyToMany.add( flowers );
|
||||
product.categoriesWithDescManyToMany.add( flowers );
|
||||
product.categoriesWithDescIdLt4ManyToMany.add( flowers );
|
||||
Category vegetables = new Category();
|
||||
vegetables.id = 2;
|
||||
vegetables.name = "vegetables";
|
||||
vegetables.description = "VEGETABLES";
|
||||
product.categoriesOneToMany.add( vegetables );
|
||||
product.categoriesWithDescOneToMany.add( vegetables );
|
||||
product.categoriesManyToMany.add( vegetables );
|
||||
product.categoriesWithDescManyToMany.add( vegetables );
|
||||
product.categoriesWithDescIdLt4ManyToMany.add( vegetables );
|
||||
Category dogs = new Category();
|
||||
dogs.id = 3;
|
||||
dogs.name = "dogs";
|
||||
dogs.description = null;
|
||||
product.categoriesOneToMany.add( dogs );
|
||||
product.categoriesWithDescOneToMany.add( dogs );
|
||||
product.categoriesManyToMany.add( dogs );
|
||||
product.categoriesWithDescManyToMany.add( dogs );
|
||||
product.categoriesWithDescIdLt4ManyToMany.add( dogs );
|
||||
Category building = new Category();
|
||||
building.id = 4;
|
||||
building.name = "building";
|
||||
building.description = "BUILDING";
|
||||
product.categoriesOneToMany.add( building );
|
||||
product.categoriesWithDescOneToMany.add( building );
|
||||
product.categoriesManyToMany.add( building );
|
||||
product.categoriesWithDescManyToMany.add( building );
|
||||
product.categoriesWithDescIdLt4ManyToMany.add( building );
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
session.persist( flowers );
|
||||
session.persist( vegetables );
|
||||
session.persist( dogs );
|
||||
session.persist( building );
|
||||
session.persist( product );
|
||||
}
|
||||
);
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
Product p = session.get( Product.class, product.id );
|
||||
assertNotNull( p );
|
||||
assertEquals( 4, p.categoriesOneToMany.size() );
|
||||
checkIds( p.categoriesOneToMany, new Integer[] { 1, 2, 3, 4 } );
|
||||
assertEquals( 3, p.categoriesWithDescOneToMany.size() );
|
||||
checkIds( p.categoriesWithDescOneToMany, new Integer[] { 1, 2, 4 } );
|
||||
assertEquals( 4, p.categoriesManyToMany.size() );
|
||||
checkIds( p.categoriesManyToMany, new Integer[] { 1, 2, 3, 4 } );
|
||||
assertEquals( 3, p.categoriesWithDescManyToMany.size() );
|
||||
checkIds( p.categoriesWithDescManyToMany, new Integer[] { 1, 2, 4 } );
|
||||
assertEquals( 2, p.categoriesWithDescIdLt4ManyToMany.size() );
|
||||
checkIds( p.categoriesWithDescIdLt4ManyToMany, new Integer[] { 1, 2 } );
|
||||
}
|
||||
);
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
Category c = session.get( Category.class, flowers.id );
|
||||
assertNotNull( c );
|
||||
c.inactive = true;
|
||||
}
|
||||
);
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
Category c = session.get( Category.class, flowers.id );
|
||||
assertNull( c );
|
||||
}
|
||||
);
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
Product p = session.get( Product.class, product.id );
|
||||
assertNotNull( p );
|
||||
assertEquals( 3, p.categoriesOneToMany.size() );
|
||||
checkIds( p.categoriesOneToMany, new Integer[] { 2, 3, 4 } );
|
||||
assertEquals( 2, p.categoriesWithDescOneToMany.size() );
|
||||
checkIds( p.categoriesWithDescOneToMany, new Integer[] { 2, 4 } );
|
||||
assertEquals( 3, p.categoriesManyToMany.size() );
|
||||
checkIds( p.categoriesManyToMany, new Integer[] { 2, 3, 4 } );
|
||||
assertEquals( 2, p.categoriesWithDescManyToMany.size() );
|
||||
checkIds( p.categoriesWithDescManyToMany, new Integer[] { 2, 4 } );
|
||||
assertEquals( 1, p.categoriesWithDescIdLt4ManyToMany.size() );
|
||||
checkIds( p.categoriesWithDescIdLt4ManyToMany, new Integer[] { 2 } );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
private void checkIds(Set<Category> categories, Integer[] expectedIds) {
|
||||
final Set<Integer> expectedIdSet = new HashSet<>( Arrays.asList( expectedIds ) );
|
||||
for ( Category category : categories ) {
|
||||
expectedIdSet.remove( category.id );
|
||||
}
|
||||
assertTrue( expectedIdSet.isEmpty() );
|
||||
}
|
||||
|
||||
@Entity(name = "Product")
|
||||
public static class Product {
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private int id;
|
||||
|
||||
@OneToMany(fetch = FetchType.LAZY)
|
||||
@JoinColumn
|
||||
private Set<Category> categoriesOneToMany = new HashSet<>();
|
||||
|
||||
@OneToMany(fetch = FetchType.LAZY)
|
||||
@JoinColumn
|
||||
@Where( clause = "description is not null" )
|
||||
private Set<Category> categoriesWithDescOneToMany = new HashSet<>();
|
||||
|
||||
@ManyToMany(fetch = FetchType.LAZY)
|
||||
@JoinTable(name = "categoriesManyToMany")
|
||||
private Set<Category> categoriesManyToMany = new HashSet<>();
|
||||
|
||||
@ManyToMany(fetch = FetchType.LAZY)
|
||||
@JoinTable(name = "categoriesWithDescManyToMany")
|
||||
@Where( clause = "description is not null" )
|
||||
private Set<Category> categoriesWithDescManyToMany = new HashSet<>();
|
||||
|
||||
@ManyToMany(fetch = FetchType.LAZY)
|
||||
@JoinTable(name = "categoriesWithDescIdLt4MToM", inverseJoinColumns = { @JoinColumn( name = "categoryId" )})
|
||||
@Where( clause = "description is not null" )
|
||||
@WhereJoinTable( clause = "categoryId < 4")
|
||||
private Set<Category> categoriesWithDescIdLt4ManyToMany = new HashSet<>();
|
||||
}
|
||||
|
||||
@Entity(name = "Category")
|
||||
@Table(name = "CATEGORY")
|
||||
@Where(clause = "inactive = 0")
|
||||
public static class Category {
|
||||
@Id
|
||||
private int id;
|
||||
|
||||
private String name;
|
||||
|
||||
private String description;
|
||||
|
||||
private boolean inactive;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
/*
|
||||
* 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;
|
||||
|
||||
public class Category {
|
||||
private int id;
|
||||
|
||||
private String name;
|
||||
|
||||
private String description;
|
||||
|
||||
private boolean inactive;
|
||||
|
||||
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 String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public boolean isInactive() {
|
||||
return inactive;
|
||||
}
|
||||
|
||||
public void setInactive(boolean inactive) {
|
||||
this.inactive = inactive;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
<?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="Product" table="PRODUCT">
|
||||
<id name="id" column="ID">
|
||||
<generator class="increment" />
|
||||
</id>
|
||||
|
||||
<set name="categoriesOneToMany" lazy="false" outer-join="true">
|
||||
<key column="productOneToManyId"/>
|
||||
<one-to-many class="Category"/>
|
||||
</set>
|
||||
|
||||
<set name="categoriesWithDescOneToMany" lazy="false" outer-join="true" where="description is not null">
|
||||
<key column="productWithDescOneToManyId"/>
|
||||
<one-to-many class="Category"/>
|
||||
</set>
|
||||
|
||||
<set name="categoriesManyToMany" table="categoriesManyToMany" lazy="false" outer-join="true">
|
||||
<key column="productId"/>
|
||||
<many-to-many class="Category" column="categoryId"/>
|
||||
</set>
|
||||
|
||||
|
||||
<set name="categoriesWithDescManyToMany" table="categoriesWithDescManyToMany" lazy="false" outer-join="true">
|
||||
<key column="productId"/>
|
||||
<many-to-many class="Category" column="categoryId" where="description is not null"/>
|
||||
</set>
|
||||
|
||||
<set name="categoriesWithDescIdLt4ManyToMany" table = "categoriesWithDescIdLt4MToM" lazy="false" outer-join="true"
|
||||
where="categoryId != 4">
|
||||
<key column="productId"/>
|
||||
<many-to-many class="Category" column="categoryId" where="description is not null"/>
|
||||
</set>
|
||||
|
||||
</class>
|
||||
|
||||
<class name="Category" table="CATEGORY" where="inactive = 0">
|
||||
<id name="id" column="ID"/>
|
||||
|
||||
<property name="name"/>
|
||||
|
||||
<property name="description"/>
|
||||
|
||||
<property name="inactive"/>
|
||||
|
||||
</class>
|
||||
|
||||
</hibernate-mapping>
|
|
@ -0,0 +1,157 @@
|
|||
/*
|
||||
* 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.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.hibernate.cfg.AvailableSettings;
|
||||
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* Tests association collections with AvailableSettings.USE_ENTITY_WHERE_CLAUSE_FOR_COLLECTIONS = false
|
||||
*
|
||||
* @author Gail Badner
|
||||
*/
|
||||
public class EagerToManyWhereDontUseClassWhereTest extends BaseNonConfigCoreFunctionalTestCase {
|
||||
|
||||
@Override
|
||||
protected String[] getMappings() {
|
||||
return new String[] { "where/hbm/EagerToManyWhere.hbm.xml" };
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void addSettings(Map settings) {
|
||||
settings.put( AvailableSettings.USE_ENTITY_WHERE_CLAUSE_FOR_COLLECTIONS, "false" );
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue( jiraKey = "HHH-13011" )
|
||||
public void testAssociatedWhereClause() {
|
||||
|
||||
Product product = new Product();
|
||||
Category flowers = new Category();
|
||||
flowers.setId( 1 );
|
||||
flowers.setName( "flowers" );
|
||||
flowers.setDescription( "FLOWERS" );
|
||||
product.getCategoriesOneToMany().add( flowers );
|
||||
product.getCategoriesWithDescOneToMany().add( flowers );
|
||||
product.getCategoriesManyToMany().add( flowers );
|
||||
product.getCategoriesWithDescManyToMany().add( flowers );
|
||||
product.getCategoriesWithDescIdLt4ManyToMany().add( flowers );
|
||||
Category vegetables = new Category();
|
||||
vegetables.setId( 2 );
|
||||
vegetables.setName( "vegetables" );
|
||||
vegetables.setDescription( "VEGETABLES" );
|
||||
product.getCategoriesOneToMany().add( vegetables );
|
||||
product.getCategoriesWithDescOneToMany().add( vegetables );
|
||||
product.getCategoriesManyToMany().add( vegetables );
|
||||
product.getCategoriesWithDescManyToMany().add( vegetables );
|
||||
product.getCategoriesWithDescIdLt4ManyToMany().add( vegetables );
|
||||
Category dogs = new Category();
|
||||
dogs.setId( 3 );
|
||||
dogs.setName( "dogs" );
|
||||
dogs.setDescription( null );
|
||||
product.getCategoriesOneToMany().add( dogs );
|
||||
product.getCategoriesWithDescOneToMany().add( dogs );
|
||||
product.getCategoriesManyToMany().add( dogs );
|
||||
product.getCategoriesWithDescManyToMany().add( dogs );
|
||||
product.getCategoriesWithDescIdLt4ManyToMany().add( dogs );
|
||||
Category building = new Category();
|
||||
building.setId( 4 );
|
||||
building.setName( "building" );
|
||||
building.setDescription( "BUILDING" );
|
||||
product.getCategoriesOneToMany().add( building );
|
||||
product.getCategoriesWithDescOneToMany().add( building );
|
||||
product.getCategoriesManyToMany().add( building );
|
||||
product.getCategoriesWithDescManyToMany().add( building );
|
||||
product.getCategoriesWithDescIdLt4ManyToMany().add( building );
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
session.persist( flowers );
|
||||
session.persist( vegetables );
|
||||
session.persist( dogs );
|
||||
session.persist( building );
|
||||
session.persist( product );
|
||||
}
|
||||
);
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
Product p = session.get( Product.class, product.getId() );
|
||||
assertNotNull( p );
|
||||
assertEquals( 4, p.getCategoriesOneToMany().size() );
|
||||
checkIds( p.getCategoriesOneToMany(), new Integer[] { 1, 2, 3, 4 } );
|
||||
assertEquals( 3, p.getCategoriesWithDescOneToMany().size() );
|
||||
checkIds( p.getCategoriesWithDescOneToMany(), new Integer[] { 1, 2, 4 } );
|
||||
assertEquals( 4, p.getCategoriesManyToMany().size() );
|
||||
checkIds( p.getCategoriesManyToMany(), new Integer[] { 1, 2, 3, 4 } );
|
||||
assertEquals( 3, p.getCategoriesWithDescManyToMany().size() );
|
||||
checkIds( p.getCategoriesWithDescManyToMany(), new Integer[] { 1, 2, 4 } );
|
||||
assertEquals( 2, p.getCategoriesWithDescIdLt4ManyToMany().size() );
|
||||
checkIds( p.getCategoriesWithDescIdLt4ManyToMany(), new Integer[] { 1, 2 } );
|
||||
}
|
||||
);
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
Category c = session.get( Category.class, flowers.getId() );
|
||||
assertNotNull( c );
|
||||
c.setInactive( true );
|
||||
}
|
||||
);
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
Category c = session.get( Category.class, flowers.getId() );
|
||||
assertNull( c );
|
||||
}
|
||||
);
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
Product p = session.get( Product.class, product.getId() );
|
||||
assertNotNull( p );
|
||||
assertEquals( 4, p.getCategoriesOneToMany().size() );
|
||||
checkIds( p.getCategoriesOneToMany(), new Integer[] { 1, 2, 3, 4 } );
|
||||
assertEquals( 3, p.getCategoriesWithDescOneToMany().size() );
|
||||
checkIds( p.getCategoriesWithDescOneToMany(), new Integer[] { 1, 2, 4 } );
|
||||
assertEquals( 4, p.getCategoriesManyToMany().size() );
|
||||
checkIds( p.getCategoriesManyToMany(), new Integer[] { 1, 2, 3, 4 } );
|
||||
assertEquals( 3, p.getCategoriesWithDescManyToMany().size() );
|
||||
checkIds( p.getCategoriesWithDescManyToMany(), new Integer[] { 1, 2, 4 } );
|
||||
assertEquals( 2, p.getCategoriesWithDescIdLt4ManyToMany().size() );
|
||||
checkIds( p.getCategoriesWithDescIdLt4ManyToMany(), new Integer[] { 1, 2 } );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
private void checkIds(Set<Category> categories, Integer[] expectedIds) {
|
||||
final Set<Integer> expectedIdSet = new HashSet<>( Arrays.asList( expectedIds ) );
|
||||
for ( Category category : categories ) {
|
||||
expectedIdSet.remove( category.getId() );
|
||||
}
|
||||
assertTrue( expectedIdSet.isEmpty() );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,150 @@
|
|||
/*
|
||||
* 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.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* Tests association collections with default AvailableSettings.USE_ENTITY_WHERE_CLAUSE_FOR_COLLECTIONS,
|
||||
* which is true.
|
||||
*
|
||||
* @author Gail Badner
|
||||
*/
|
||||
public class EagerToManyWhereTest extends BaseNonConfigCoreFunctionalTestCase {
|
||||
|
||||
@Override
|
||||
protected String[] getMappings() {
|
||||
return new String[] { "where/hbm/EagerToManyWhere.hbm.xml" };
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue( jiraKey = "HHH-13011" )
|
||||
public void testAssociatedWhereClause() {
|
||||
|
||||
Product product = new Product();
|
||||
Category flowers = new Category();
|
||||
flowers.setId( 1 );
|
||||
flowers.setName( "flowers" );
|
||||
flowers.setDescription( "FLOWERS" );
|
||||
product.getCategoriesOneToMany().add( flowers );
|
||||
product.getCategoriesWithDescOneToMany().add( flowers );
|
||||
product.getCategoriesManyToMany().add( flowers );
|
||||
product.getCategoriesWithDescManyToMany().add( flowers );
|
||||
product.getCategoriesWithDescIdLt4ManyToMany().add( flowers );
|
||||
Category vegetables = new Category();
|
||||
vegetables.setId( 2 );
|
||||
vegetables.setName( "vegetables" );
|
||||
vegetables.setDescription( "VEGETABLES" );
|
||||
product.getCategoriesOneToMany().add( vegetables );
|
||||
product.getCategoriesWithDescOneToMany().add( vegetables );
|
||||
product.getCategoriesManyToMany().add( vegetables );
|
||||
product.getCategoriesWithDescManyToMany().add( vegetables );
|
||||
product.getCategoriesWithDescIdLt4ManyToMany().add( vegetables );
|
||||
Category dogs = new Category();
|
||||
dogs.setId( 3 );
|
||||
dogs.setName( "dogs" );
|
||||
dogs.setDescription( null );
|
||||
product.getCategoriesOneToMany().add( dogs );
|
||||
product.getCategoriesWithDescOneToMany().add( dogs );
|
||||
product.getCategoriesManyToMany().add( dogs );
|
||||
product.getCategoriesWithDescManyToMany().add( dogs );
|
||||
product.getCategoriesWithDescIdLt4ManyToMany().add( dogs );
|
||||
Category building = new Category();
|
||||
building.setId( 4 );
|
||||
building.setName( "building" );
|
||||
building.setDescription( "BUILDING" );
|
||||
product.getCategoriesOneToMany().add( building );
|
||||
product.getCategoriesWithDescOneToMany().add( building );
|
||||
product.getCategoriesManyToMany().add( building );
|
||||
product.getCategoriesWithDescManyToMany().add( building );
|
||||
product.getCategoriesWithDescIdLt4ManyToMany().add( building );
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
session.persist( flowers );
|
||||
session.persist( vegetables );
|
||||
session.persist( dogs );
|
||||
session.persist( building );
|
||||
session.persist( product );
|
||||
}
|
||||
);
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
Product p = session.get( Product.class, product.getId() );
|
||||
assertNotNull( p );
|
||||
assertEquals( 4, p.getCategoriesOneToMany().size() );
|
||||
checkIds( p.getCategoriesOneToMany(), new Integer[] { 1, 2, 3, 4 } );
|
||||
assertEquals( 3, p.getCategoriesWithDescOneToMany().size() );
|
||||
checkIds( p.getCategoriesWithDescOneToMany(), new Integer[] { 1, 2, 4 } );
|
||||
assertEquals( 4, p.getCategoriesManyToMany().size() );
|
||||
checkIds( p.getCategoriesManyToMany(), new Integer[] { 1, 2, 3, 4 } );
|
||||
assertEquals( 3, p.getCategoriesWithDescManyToMany().size() );
|
||||
checkIds( p.getCategoriesWithDescManyToMany(), new Integer[] { 1, 2, 4 } );
|
||||
assertEquals( 2, p.getCategoriesWithDescIdLt4ManyToMany().size() );
|
||||
checkIds( p.getCategoriesWithDescIdLt4ManyToMany(), new Integer[] { 1, 2 } );
|
||||
}
|
||||
);
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
Category c = session.get( Category.class, flowers.getId() );
|
||||
assertNotNull( c );
|
||||
c.setInactive( true );
|
||||
}
|
||||
);
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
Category c = session.get( Category.class, flowers.getId() );
|
||||
assertNull( c );
|
||||
}
|
||||
);
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
Product p = session.get( Product.class, product.getId() );
|
||||
assertNotNull( p );
|
||||
assertEquals( 3, p.getCategoriesOneToMany().size() );
|
||||
checkIds( p.getCategoriesOneToMany(), new Integer[] { 2, 3, 4 } );
|
||||
assertEquals( 2, p.getCategoriesWithDescOneToMany().size() );
|
||||
checkIds( p.getCategoriesWithDescOneToMany(), new Integer[] { 2, 4 } );
|
||||
assertEquals( 3, p.getCategoriesManyToMany().size() );
|
||||
checkIds( p.getCategoriesManyToMany(), new Integer[] { 2, 3, 4 } );
|
||||
assertEquals( 2, p.getCategoriesWithDescManyToMany().size() );
|
||||
checkIds( p.getCategoriesWithDescManyToMany(), new Integer[] { 2, 4 } );
|
||||
assertEquals( 1, p.getCategoriesWithDescIdLt4ManyToMany().size() );
|
||||
checkIds( p.getCategoriesWithDescIdLt4ManyToMany(), new Integer[] { 2 } );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
private void checkIds(Set<Category> categories, Integer[] expectedIds) {
|
||||
final Set<Integer> expectedIdSet = new HashSet<>( Arrays.asList( expectedIds ) );
|
||||
for ( Category category : categories ) {
|
||||
expectedIdSet.remove( category.getId() );
|
||||
}
|
||||
assertTrue( expectedIdSet.isEmpty() );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,157 @@
|
|||
/*
|
||||
* 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.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.hibernate.cfg.AvailableSettings;
|
||||
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* Tests association collections with AvailableSettings.USE_ENTITY_WHERE_CLAUSE_FOR_COLLECTIONS = true
|
||||
*
|
||||
* @author Gail Badner
|
||||
*/
|
||||
public class EagerToManyWhereUseClassWhereTest extends BaseNonConfigCoreFunctionalTestCase {
|
||||
|
||||
@Override
|
||||
protected String[] getMappings() {
|
||||
return new String[] { "where/hbm/EagerToManyWhere.hbm.xml" };
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void addSettings(Map settings) {
|
||||
settings.put( AvailableSettings.USE_ENTITY_WHERE_CLAUSE_FOR_COLLECTIONS, "true" );
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue( jiraKey = "HHH-13011" )
|
||||
public void testAssociatedWhereClause() {
|
||||
|
||||
Product product = new Product();
|
||||
Category flowers = new Category();
|
||||
flowers.setId( 1 );
|
||||
flowers.setName( "flowers" );
|
||||
flowers.setDescription( "FLOWERS" );
|
||||
product.getCategoriesOneToMany().add( flowers );
|
||||
product.getCategoriesWithDescOneToMany().add( flowers );
|
||||
product.getCategoriesManyToMany().add( flowers );
|
||||
product.getCategoriesWithDescManyToMany().add( flowers );
|
||||
product.getCategoriesWithDescIdLt4ManyToMany().add( flowers );
|
||||
Category vegetables = new Category();
|
||||
vegetables.setId( 2 );
|
||||
vegetables.setName( "vegetables" );
|
||||
vegetables.setDescription( "VEGETABLES" );
|
||||
product.getCategoriesOneToMany().add( vegetables );
|
||||
product.getCategoriesWithDescOneToMany().add( vegetables );
|
||||
product.getCategoriesManyToMany().add( vegetables );
|
||||
product.getCategoriesWithDescManyToMany().add( vegetables );
|
||||
product.getCategoriesWithDescIdLt4ManyToMany().add( vegetables );
|
||||
Category dogs = new Category();
|
||||
dogs.setId( 3 );
|
||||
dogs.setName( "dogs" );
|
||||
dogs.setDescription( null );
|
||||
product.getCategoriesOneToMany().add( dogs );
|
||||
product.getCategoriesWithDescOneToMany().add( dogs );
|
||||
product.getCategoriesManyToMany().add( dogs );
|
||||
product.getCategoriesWithDescManyToMany().add( dogs );
|
||||
product.getCategoriesWithDescIdLt4ManyToMany().add( dogs );
|
||||
Category building = new Category();
|
||||
building.setId( 4 );
|
||||
building.setName( "building" );
|
||||
building.setDescription( "BUILDING" );
|
||||
product.getCategoriesOneToMany().add( building );
|
||||
product.getCategoriesWithDescOneToMany().add( building );
|
||||
product.getCategoriesManyToMany().add( building );
|
||||
product.getCategoriesWithDescManyToMany().add( building );
|
||||
product.getCategoriesWithDescIdLt4ManyToMany().add( building );
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
session.persist( flowers );
|
||||
session.persist( vegetables );
|
||||
session.persist( dogs );
|
||||
session.persist( building );
|
||||
session.persist( product );
|
||||
}
|
||||
);
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
Product p = session.get( Product.class, product.getId() );
|
||||
assertNotNull( p );
|
||||
assertEquals( 4, p.getCategoriesOneToMany().size() );
|
||||
checkIds( p.getCategoriesOneToMany(), new Integer[] { 1, 2, 3, 4 } );
|
||||
assertEquals( 3, p.getCategoriesWithDescOneToMany().size() );
|
||||
checkIds( p.getCategoriesWithDescOneToMany(), new Integer[] { 1, 2, 4 } );
|
||||
assertEquals( 4, p.getCategoriesManyToMany().size() );
|
||||
checkIds( p.getCategoriesManyToMany(), new Integer[] { 1, 2, 3, 4 } );
|
||||
assertEquals( 3, p.getCategoriesWithDescManyToMany().size() );
|
||||
checkIds( p.getCategoriesWithDescManyToMany(), new Integer[] { 1, 2, 4 } );
|
||||
assertEquals( 2, p.getCategoriesWithDescIdLt4ManyToMany().size() );
|
||||
checkIds( p.getCategoriesWithDescIdLt4ManyToMany(), new Integer[] { 1, 2 } );
|
||||
}
|
||||
);
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
Category c = session.get( Category.class, flowers.getId() );
|
||||
assertNotNull( c );
|
||||
c.setInactive( true );
|
||||
}
|
||||
);
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
Category c = session.get( Category.class, flowers.getId() );
|
||||
assertNull( c );
|
||||
}
|
||||
);
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
Product p = session.get( Product.class, product.getId() );
|
||||
assertNotNull( p );
|
||||
assertEquals( 3, p.getCategoriesOneToMany().size() );
|
||||
checkIds( p.getCategoriesOneToMany(), new Integer[] { 2, 3, 4 } );
|
||||
assertEquals( 2, p.getCategoriesWithDescOneToMany().size() );
|
||||
checkIds( p.getCategoriesWithDescOneToMany(), new Integer[] { 2, 4 } );
|
||||
assertEquals( 3, p.getCategoriesManyToMany().size() );
|
||||
checkIds( p.getCategoriesManyToMany(), new Integer[] { 2, 3, 4 } );
|
||||
assertEquals( 2, p.getCategoriesWithDescManyToMany().size() );
|
||||
checkIds( p.getCategoriesWithDescManyToMany(), new Integer[] { 2, 4 } );
|
||||
assertEquals( 1, p.getCategoriesWithDescIdLt4ManyToMany().size() );
|
||||
checkIds( p.getCategoriesWithDescIdLt4ManyToMany(), new Integer[] { 2 } );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
private void checkIds(Set<Category> categories, Integer[] expectedIds) {
|
||||
final Set<Integer> expectedIdSet = new HashSet<>( Arrays.asList( expectedIds ) );
|
||||
for ( Category category : categories ) {
|
||||
expectedIdSet.remove( category.getId() );
|
||||
}
|
||||
assertTrue( expectedIdSet.isEmpty() );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
<?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="Product" table="PRODUCT">
|
||||
<id name="id" column="ID">
|
||||
<generator class="increment" />
|
||||
</id>
|
||||
|
||||
<set name="categoriesOneToMany" lazy="true">
|
||||
<key column="productOneToManyId"/>
|
||||
<one-to-many class="Category"/>
|
||||
</set>
|
||||
|
||||
<set name="categoriesWithDescOneToMany" lazy="true" where="description is not null">
|
||||
<key column="productWithDescOneToManyId"/>
|
||||
<one-to-many class="Category"/>
|
||||
</set>
|
||||
|
||||
<set name="categoriesManyToMany" table="categoriesManyToMany" lazy="true">
|
||||
<key column="productId"/>
|
||||
<many-to-many class="Category" column="categoryId"/>
|
||||
</set>
|
||||
|
||||
|
||||
<set name="categoriesWithDescManyToMany" table="categoriesWithDescManyToMany" lazy="true">
|
||||
<key column="productId"/>
|
||||
<many-to-many class="Category" column="categoryId" where="description is not null"/>
|
||||
</set>
|
||||
|
||||
<set name="categoriesWithDescIdLt4ManyToMany" table = "categoriesWithDescIdLt4MToM" lazy="true"
|
||||
where="categoryId != 4">
|
||||
<key column="productId"/>
|
||||
<many-to-many class="Category" column="categoryId" where="description is not null"/>
|
||||
</set>
|
||||
|
||||
</class>
|
||||
|
||||
<class name="Category" table="CATEGORY" where="inactive = 0">
|
||||
<id name="id" column="ID"/>
|
||||
|
||||
<property name="name"/>
|
||||
|
||||
<property name="description"/>
|
||||
|
||||
<property name="inactive"/>
|
||||
|
||||
</class>
|
||||
|
||||
</hibernate-mapping>
|
|
@ -0,0 +1,157 @@
|
|||
/*
|
||||
* 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.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.hibernate.cfg.AvailableSettings;
|
||||
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* Tests association collections with AvailableSettings.USE_ENTITY_WHERE_CLAUSE_FOR_COLLECTIONS = false
|
||||
*
|
||||
* @author Gail Badner
|
||||
*/
|
||||
public class LazyToManyWhereDontUseClassWhereTest extends BaseNonConfigCoreFunctionalTestCase {
|
||||
|
||||
@Override
|
||||
protected String[] getMappings() {
|
||||
return new String[] { "where/hbm/LazyToManyWhere.hbm.xml" };
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void addSettings(Map settings) {
|
||||
settings.put( AvailableSettings.USE_ENTITY_WHERE_CLAUSE_FOR_COLLECTIONS, "false" );
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue( jiraKey = "HHH-13011" )
|
||||
public void testAssociatedWhereClause() {
|
||||
|
||||
Product product = new Product();
|
||||
Category flowers = new Category();
|
||||
flowers.setId( 1 );
|
||||
flowers.setName( "flowers" );
|
||||
flowers.setDescription( "FLOWERS" );
|
||||
product.getCategoriesOneToMany().add( flowers );
|
||||
product.getCategoriesWithDescOneToMany().add( flowers );
|
||||
product.getCategoriesManyToMany().add( flowers );
|
||||
product.getCategoriesWithDescManyToMany().add( flowers );
|
||||
product.getCategoriesWithDescIdLt4ManyToMany().add( flowers );
|
||||
Category vegetables = new Category();
|
||||
vegetables.setId( 2 );
|
||||
vegetables.setName( "vegetables" );
|
||||
vegetables.setDescription( "VEGETABLES" );
|
||||
product.getCategoriesOneToMany().add( vegetables );
|
||||
product.getCategoriesWithDescOneToMany().add( vegetables );
|
||||
product.getCategoriesManyToMany().add( vegetables );
|
||||
product.getCategoriesWithDescManyToMany().add( vegetables );
|
||||
product.getCategoriesWithDescIdLt4ManyToMany().add( vegetables );
|
||||
Category dogs = new Category();
|
||||
dogs.setId( 3 );
|
||||
dogs.setName( "dogs" );
|
||||
dogs.setDescription( null );
|
||||
product.getCategoriesOneToMany().add( dogs );
|
||||
product.getCategoriesWithDescOneToMany().add( dogs );
|
||||
product.getCategoriesManyToMany().add( dogs );
|
||||
product.getCategoriesWithDescManyToMany().add( dogs );
|
||||
product.getCategoriesWithDescIdLt4ManyToMany().add( dogs );
|
||||
Category building = new Category();
|
||||
building.setId( 4 );
|
||||
building.setName( "building" );
|
||||
building.setDescription( "BUILDING" );
|
||||
product.getCategoriesOneToMany().add( building );
|
||||
product.getCategoriesWithDescOneToMany().add( building );
|
||||
product.getCategoriesManyToMany().add( building );
|
||||
product.getCategoriesWithDescManyToMany().add( building );
|
||||
product.getCategoriesWithDescIdLt4ManyToMany().add( building );
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
session.persist( flowers );
|
||||
session.persist( vegetables );
|
||||
session.persist( dogs );
|
||||
session.persist( building );
|
||||
session.persist( product );
|
||||
}
|
||||
);
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
Product p = session.get( Product.class, product.getId() );
|
||||
assertNotNull( p );
|
||||
assertEquals( 4, p.getCategoriesOneToMany().size() );
|
||||
checkIds( p.getCategoriesOneToMany(), new Integer[] { 1, 2, 3, 4 } );
|
||||
assertEquals( 3, p.getCategoriesWithDescOneToMany().size() );
|
||||
checkIds( p.getCategoriesWithDescOneToMany(), new Integer[] { 1, 2, 4 } );
|
||||
assertEquals( 4, p.getCategoriesManyToMany().size() );
|
||||
checkIds( p.getCategoriesManyToMany(), new Integer[] { 1, 2, 3, 4 } );
|
||||
assertEquals( 3, p.getCategoriesWithDescManyToMany().size() );
|
||||
checkIds( p.getCategoriesWithDescManyToMany(), new Integer[] { 1, 2, 4 } );
|
||||
assertEquals( 2, p.getCategoriesWithDescIdLt4ManyToMany().size() );
|
||||
checkIds( p.getCategoriesWithDescIdLt4ManyToMany(), new Integer[] { 1, 2 } );
|
||||
}
|
||||
);
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
Category c = session.get( Category.class, flowers.getId() );
|
||||
assertNotNull( c );
|
||||
c.setInactive( true );
|
||||
}
|
||||
);
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
Category c = session.get( Category.class, flowers.getId() );
|
||||
assertNull( c );
|
||||
}
|
||||
);
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
Product p = session.get( Product.class, product.getId() );
|
||||
assertNotNull( p );
|
||||
assertEquals( 4, p.getCategoriesOneToMany().size() );
|
||||
checkIds( p.getCategoriesOneToMany(), new Integer[] { 1, 2, 3, 4 } );
|
||||
assertEquals( 3, p.getCategoriesWithDescOneToMany().size() );
|
||||
checkIds( p.getCategoriesWithDescOneToMany(), new Integer[] { 1, 2, 4 } );
|
||||
assertEquals( 4, p.getCategoriesManyToMany().size() );
|
||||
checkIds( p.getCategoriesManyToMany(), new Integer[] { 1, 2, 3, 4 } );
|
||||
assertEquals( 3, p.getCategoriesWithDescManyToMany().size() );
|
||||
checkIds( p.getCategoriesWithDescManyToMany(), new Integer[] { 1, 2, 4 } );
|
||||
assertEquals( 2, p.getCategoriesWithDescIdLt4ManyToMany().size() );
|
||||
checkIds( p.getCategoriesWithDescIdLt4ManyToMany(), new Integer[] { 1, 2 } );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
private void checkIds(Set<Category> categories, Integer[] expectedIds) {
|
||||
final Set<Integer> expectedIdSet = new HashSet<>( Arrays.asList( expectedIds ) );
|
||||
for ( Category category : categories ) {
|
||||
expectedIdSet.remove( category.getId() );
|
||||
}
|
||||
assertTrue( expectedIdSet.isEmpty() );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,150 @@
|
|||
/*
|
||||
* 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.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* Tests association collections with default AvailableSettings.USE_ENTITY_WHERE_CLAUSE_FOR_COLLECTIONS,
|
||||
* which is true.
|
||||
*
|
||||
* @author Gail Badner
|
||||
*/
|
||||
public class LazyToManyWhereTest extends BaseNonConfigCoreFunctionalTestCase {
|
||||
|
||||
@Override
|
||||
protected String[] getMappings() {
|
||||
return new String[] { "where/hbm/LazyToManyWhere.hbm.xml" };
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue( jiraKey = "HHH-13011" )
|
||||
public void testAssociatedWhereClause() {
|
||||
|
||||
Product product = new Product();
|
||||
Category flowers = new Category();
|
||||
flowers.setId( 1 );
|
||||
flowers.setName( "flowers" );
|
||||
flowers.setDescription( "FLOWERS" );
|
||||
product.getCategoriesOneToMany().add( flowers );
|
||||
product.getCategoriesWithDescOneToMany().add( flowers );
|
||||
product.getCategoriesManyToMany().add( flowers );
|
||||
product.getCategoriesWithDescManyToMany().add( flowers );
|
||||
product.getCategoriesWithDescIdLt4ManyToMany().add( flowers );
|
||||
Category vegetables = new Category();
|
||||
vegetables.setId( 2 );
|
||||
vegetables.setName( "vegetables" );
|
||||
vegetables.setDescription( "VEGETABLES" );
|
||||
product.getCategoriesOneToMany().add( vegetables );
|
||||
product.getCategoriesWithDescOneToMany().add( vegetables );
|
||||
product.getCategoriesManyToMany().add( vegetables );
|
||||
product.getCategoriesWithDescManyToMany().add( vegetables );
|
||||
product.getCategoriesWithDescIdLt4ManyToMany().add( vegetables );
|
||||
Category dogs = new Category();
|
||||
dogs.setId( 3 );
|
||||
dogs.setName( "dogs" );
|
||||
dogs.setDescription( null );
|
||||
product.getCategoriesOneToMany().add( dogs );
|
||||
product.getCategoriesWithDescOneToMany().add( dogs );
|
||||
product.getCategoriesManyToMany().add( dogs );
|
||||
product.getCategoriesWithDescManyToMany().add( dogs );
|
||||
product.getCategoriesWithDescIdLt4ManyToMany().add( dogs );
|
||||
Category building = new Category();
|
||||
building.setId( 4 );
|
||||
building.setName( "building" );
|
||||
building.setDescription( "BUILDING" );
|
||||
product.getCategoriesOneToMany().add( building );
|
||||
product.getCategoriesWithDescOneToMany().add( building );
|
||||
product.getCategoriesManyToMany().add( building );
|
||||
product.getCategoriesWithDescManyToMany().add( building );
|
||||
product.getCategoriesWithDescIdLt4ManyToMany().add( building );
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
session.persist( flowers );
|
||||
session.persist( vegetables );
|
||||
session.persist( dogs );
|
||||
session.persist( building );
|
||||
session.persist( product );
|
||||
}
|
||||
);
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
Product p = session.get( Product.class, product.getId() );
|
||||
assertNotNull( p );
|
||||
assertEquals( 4, p.getCategoriesOneToMany().size() );
|
||||
checkIds( p.getCategoriesOneToMany(), new Integer[] { 1, 2, 3, 4 } );
|
||||
assertEquals( 3, p.getCategoriesWithDescOneToMany().size() );
|
||||
checkIds( p.getCategoriesWithDescOneToMany(), new Integer[] { 1, 2, 4 } );
|
||||
assertEquals( 4, p.getCategoriesManyToMany().size() );
|
||||
checkIds( p.getCategoriesManyToMany(), new Integer[] { 1, 2, 3, 4 } );
|
||||
assertEquals( 3, p.getCategoriesWithDescManyToMany().size() );
|
||||
checkIds( p.getCategoriesWithDescManyToMany(), new Integer[] { 1, 2, 4 } );
|
||||
assertEquals( 2, p.getCategoriesWithDescIdLt4ManyToMany().size() );
|
||||
checkIds( p.getCategoriesWithDescIdLt4ManyToMany(), new Integer[] { 1, 2 } );
|
||||
}
|
||||
);
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
Category c = session.get( Category.class, flowers.getId() );
|
||||
assertNotNull( c );
|
||||
c.setInactive( true );
|
||||
}
|
||||
);
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
Category c = session.get( Category.class, flowers.getId() );
|
||||
assertNull( c );
|
||||
}
|
||||
);
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
Product p = session.get( Product.class, product.getId() );
|
||||
assertNotNull( p );
|
||||
assertEquals( 3, p.getCategoriesOneToMany().size() );
|
||||
checkIds( p.getCategoriesOneToMany(), new Integer[] { 2, 3, 4 } );
|
||||
assertEquals( 2, p.getCategoriesWithDescOneToMany().size() );
|
||||
checkIds( p.getCategoriesWithDescOneToMany(), new Integer[] { 2, 4 } );
|
||||
assertEquals( 3, p.getCategoriesManyToMany().size() );
|
||||
checkIds( p.getCategoriesManyToMany(), new Integer[] { 2, 3, 4 } );
|
||||
assertEquals( 2, p.getCategoriesWithDescManyToMany().size() );
|
||||
checkIds( p.getCategoriesWithDescManyToMany(), new Integer[] { 2, 4 } );
|
||||
assertEquals( 1, p.getCategoriesWithDescIdLt4ManyToMany().size() );
|
||||
checkIds( p.getCategoriesWithDescIdLt4ManyToMany(), new Integer[] { 2 } );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
private void checkIds(Set<Category> categories, Integer[] expectedIds) {
|
||||
final Set<Integer> expectedIdSet = new HashSet<>( Arrays.asList( expectedIds ) );
|
||||
for ( Category category : categories ) {
|
||||
expectedIdSet.remove( category.getId() );
|
||||
}
|
||||
assertTrue( expectedIdSet.isEmpty() );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,157 @@
|
|||
/*
|
||||
* 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.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.hibernate.cfg.AvailableSettings;
|
||||
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* Tests association collections with AvailableSettings.USE_ENTITY_WHERE_CLAUSE_FOR_COLLECTIONS = true
|
||||
*
|
||||
* @author Gail Badner
|
||||
*/
|
||||
public class LazyToManyWhereUseClassWhereTest extends BaseNonConfigCoreFunctionalTestCase {
|
||||
|
||||
@Override
|
||||
protected String[] getMappings() {
|
||||
return new String[] { "where/hbm/LazyToManyWhere.hbm.xml" };
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void addSettings(Map settings) {
|
||||
settings.put( AvailableSettings.USE_ENTITY_WHERE_CLAUSE_FOR_COLLECTIONS, "true" );
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue( jiraKey = "HHH-13011" )
|
||||
public void testAssociatedWhereClause() {
|
||||
|
||||
Product product = new Product();
|
||||
Category flowers = new Category();
|
||||
flowers.setId( 1 );
|
||||
flowers.setName( "flowers" );
|
||||
flowers.setDescription( "FLOWERS" );
|
||||
product.getCategoriesOneToMany().add( flowers );
|
||||
product.getCategoriesWithDescOneToMany().add( flowers );
|
||||
product.getCategoriesManyToMany().add( flowers );
|
||||
product.getCategoriesWithDescManyToMany().add( flowers );
|
||||
product.getCategoriesWithDescIdLt4ManyToMany().add( flowers );
|
||||
Category vegetables = new Category();
|
||||
vegetables.setId( 2 );
|
||||
vegetables.setName( "vegetables" );
|
||||
vegetables.setDescription( "VEGETABLES" );
|
||||
product.getCategoriesOneToMany().add( vegetables );
|
||||
product.getCategoriesWithDescOneToMany().add( vegetables );
|
||||
product.getCategoriesManyToMany().add( vegetables );
|
||||
product.getCategoriesWithDescManyToMany().add( vegetables );
|
||||
product.getCategoriesWithDescIdLt4ManyToMany().add( vegetables );
|
||||
Category dogs = new Category();
|
||||
dogs.setId( 3 );
|
||||
dogs.setName( "dogs" );
|
||||
dogs.setDescription( null );
|
||||
product.getCategoriesOneToMany().add( dogs );
|
||||
product.getCategoriesWithDescOneToMany().add( dogs );
|
||||
product.getCategoriesManyToMany().add( dogs );
|
||||
product.getCategoriesWithDescManyToMany().add( dogs );
|
||||
product.getCategoriesWithDescIdLt4ManyToMany().add( dogs );
|
||||
Category building = new Category();
|
||||
building.setId( 4 );
|
||||
building.setName( "building" );
|
||||
building.setDescription( "BUILDING" );
|
||||
product.getCategoriesOneToMany().add( building );
|
||||
product.getCategoriesWithDescOneToMany().add( building );
|
||||
product.getCategoriesManyToMany().add( building );
|
||||
product.getCategoriesWithDescManyToMany().add( building );
|
||||
product.getCategoriesWithDescIdLt4ManyToMany().add( building );
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
session.persist( flowers );
|
||||
session.persist( vegetables );
|
||||
session.persist( dogs );
|
||||
session.persist( building );
|
||||
session.persist( product );
|
||||
}
|
||||
);
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
Product p = session.get( Product.class, product.getId() );
|
||||
assertNotNull( p );
|
||||
assertEquals( 4, p.getCategoriesOneToMany().size() );
|
||||
checkIds( p.getCategoriesOneToMany(), new Integer[] { 1, 2, 3, 4 } );
|
||||
assertEquals( 3, p.getCategoriesWithDescOneToMany().size() );
|
||||
checkIds( p.getCategoriesWithDescOneToMany(), new Integer[] { 1, 2, 4 } );
|
||||
assertEquals( 4, p.getCategoriesManyToMany().size() );
|
||||
checkIds( p.getCategoriesManyToMany(), new Integer[] { 1, 2, 3, 4 } );
|
||||
assertEquals( 3, p.getCategoriesWithDescManyToMany().size() );
|
||||
checkIds( p.getCategoriesWithDescManyToMany(), new Integer[] { 1, 2, 4 } );
|
||||
assertEquals( 2, p.getCategoriesWithDescIdLt4ManyToMany().size() );
|
||||
checkIds( p.getCategoriesWithDescIdLt4ManyToMany(), new Integer[] { 1, 2 } );
|
||||
}
|
||||
);
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
Category c = session.get( Category.class, flowers.getId() );
|
||||
assertNotNull( c );
|
||||
c.setInactive( true );
|
||||
}
|
||||
);
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
Category c = session.get( Category.class, flowers.getId() );
|
||||
assertNull( c );
|
||||
}
|
||||
);
|
||||
|
||||
doInHibernate(
|
||||
this::sessionFactory,
|
||||
session -> {
|
||||
Product p = session.get( Product.class, product.getId() );
|
||||
assertNotNull( p );
|
||||
assertEquals( 3, p.getCategoriesOneToMany().size() );
|
||||
checkIds( p.getCategoriesOneToMany(), new Integer[] { 2, 3, 4 } );
|
||||
assertEquals( 2, p.getCategoriesWithDescOneToMany().size() );
|
||||
checkIds( p.getCategoriesWithDescOneToMany(), new Integer[] { 2, 4 } );
|
||||
assertEquals( 3, p.getCategoriesManyToMany().size() );
|
||||
checkIds( p.getCategoriesManyToMany(), new Integer[] { 2, 3, 4 } );
|
||||
assertEquals( 2, p.getCategoriesWithDescManyToMany().size() );
|
||||
checkIds( p.getCategoriesWithDescManyToMany(), new Integer[] { 2, 4 } );
|
||||
assertEquals( 1, p.getCategoriesWithDescIdLt4ManyToMany().size() );
|
||||
checkIds( p.getCategoriesWithDescIdLt4ManyToMany(), new Integer[] { 2 } );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
private void checkIds(Set<Category> categories, Integer[] expectedIds) {
|
||||
final Set<Integer> expectedIdSet = new HashSet<>( Arrays.asList( expectedIds ) );
|
||||
for ( Category category : categories ) {
|
||||
expectedIdSet.remove( category.getId() );
|
||||
}
|
||||
assertTrue( expectedIdSet.isEmpty() );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,72 @@
|
|||
/*
|
||||
* 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.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class Product {
|
||||
private int id;
|
||||
|
||||
private Set<Category> categoriesOneToMany = new HashSet<>();
|
||||
|
||||
private Set<Category> categoriesWithDescOneToMany = new HashSet<>();
|
||||
|
||||
private Set<Category> categoriesManyToMany = new HashSet<>();
|
||||
|
||||
private Set<Category> categoriesWithDescManyToMany = new HashSet<>();
|
||||
|
||||
private Set<Category> categoriesWithDescIdLt4ManyToMany = new HashSet<>();
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Set<Category> getCategoriesOneToMany() {
|
||||
return categoriesOneToMany;
|
||||
}
|
||||
|
||||
public void setCategoriesOneToMany(Set<Category> categoriesOneToMany) {
|
||||
this.categoriesOneToMany = categoriesOneToMany;
|
||||
}
|
||||
|
||||
public Set<Category> getCategoriesWithDescOneToMany() {
|
||||
return categoriesWithDescOneToMany;
|
||||
}
|
||||
|
||||
public void setCategoriesWithDescOneToMany(Set<Category> categoriesWithDescOneToMany) {
|
||||
this.categoriesWithDescOneToMany = categoriesWithDescOneToMany;
|
||||
}
|
||||
|
||||
public Set<Category> getCategoriesManyToMany() {
|
||||
return categoriesManyToMany;
|
||||
}
|
||||
|
||||
public void setCategoriesManyToMany(Set<Category> categoriesManyToMany) {
|
||||
this.categoriesManyToMany = categoriesManyToMany;
|
||||
}
|
||||
|
||||
public Set<Category> getCategoriesWithDescManyToMany() {
|
||||
return categoriesWithDescManyToMany;
|
||||
}
|
||||
|
||||
public void setCategoriesWithDescManyToMany(Set<Category> categoriesWithDescManyToMany) {
|
||||
this.categoriesWithDescManyToMany = categoriesWithDescManyToMany;
|
||||
}
|
||||
|
||||
public Set<Category> getCategoriesWithDescIdLt4ManyToMany() {
|
||||
return categoriesWithDescIdLt4ManyToMany;
|
||||
}
|
||||
|
||||
public void setCategoriesWithDescIdLt4ManyToMany(Set<Category> categoriesWithDescIdLt4ManyToMany) {
|
||||
this.categoriesWithDescIdLt4ManyToMany = categoriesWithDescIdLt4ManyToMany;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue