HHH-12104 : Test cases using annotations and hbm.xml

This commit is contained in:
Gail Badner 2018-10-15 20:55:16 -07:00 committed by Guillaume Smet
parent 7615df2ddf
commit 19dd186d11
6 changed files with 779 additions and 0 deletions

View File

@ -0,0 +1,160 @@
/*
* 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.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.ElementCollection;
import javax.persistence.Embeddable;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
import org.hibernate.annotations.Where;
import org.hibernate.testing.FailureExpected;
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.assertSame;
/**
* @author Gail Badner
*/
public class EagerManyToOneFetchModeJoinWhereTest extends BaseNonConfigCoreFunctionalTestCase {
@Override
protected Class[] getAnnotatedClasses() {
return new Class[] { Product.class, Category.class };
}
@Test
@TestForIssue( jiraKey = "HHH-12104" )
@FailureExpected( jiraKey = "HHH-12104")
public void testAssociatedWhereClause() {
Product product = new Product();
Category category = new Category();
category.name = "flowers";
product.category = category;
product.containedCategory = new ContainedCategory();
product.containedCategory.category = category;
product.containedCategories.add( new ContainedCategory( category ) );
doInHibernate(
this::sessionFactory,
session -> {
session.persist( product );
}
);
doInHibernate(
this::sessionFactory,
session -> {
Product p = session.get( Product.class, product.id );
assertNotNull( p );
assertNotNull( p.category );
assertNotNull( p.containedCategory.category );
assertEquals( 1, p.containedCategories.size() );
assertSame( p.category, p.containedCategory.category );
assertSame( p.category, p.containedCategories.iterator().next().category );
}
);
doInHibernate(
this::sessionFactory,
session -> {
Category c = session.get( Category.class, category.id );
assertNotNull( c );
c.inactive = true;
}
);
doInHibernate(
this::sessionFactory,
session -> {
Category c = session.get( Category.class, category.id );
assertNull( c );
}
);
doInHibernate(
this::sessionFactory,
session -> {
// Entity's where clause is ignored when to-one associations to that
// association is loaded eagerly using FetchMode.JOIN, so the result
// should be the same as before the Category was made inactive.
Product p = session.get( Product.class, product.id );
assertNotNull( p );
assertNull( p.category );
assertNull( p.containedCategory.category );
assertEquals( 1, p.containedCategories.size() );
assertNull( p.containedCategories.iterator().next().category );
}
);
}
@Entity(name = "Product")
public static class Product {
@Id
@GeneratedValue
private int id;
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@NotFound(action = NotFoundAction.IGNORE)
@JoinColumn(name = "categoryId")
@Fetch(FetchMode.JOIN)
private Category category;
private ContainedCategory containedCategory;
@ElementCollection(fetch = FetchType.EAGER)
private Set<ContainedCategory> containedCategories = new HashSet<>();
}
@Entity(name = "Category")
@Table(name = "CATEGORY")
@Where(clause = "inactive = 0")
public static class Category {
@Id
@GeneratedValue
private int id;
private String name;
private boolean inactive;
}
@Embeddable
public static class ContainedCategory {
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@NotFound(action = NotFoundAction.IGNORE)
@JoinColumn(name = "containedCategoryId")
@Fetch(FetchMode.JOIN)
private Category category;
public ContainedCategory() {
}
public ContainedCategory(Category category) {
this.category = category;
}
}
}

View File

@ -0,0 +1,159 @@
/*
* 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.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.ElementCollection;
import javax.persistence.Embeddable;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
import org.hibernate.annotations.Where;
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.assertSame;
/**
* @author Gail Badner
*/
public class EagerManyToOneFetchModeSelectWhereTest extends BaseNonConfigCoreFunctionalTestCase {
@Override
protected Class[] getAnnotatedClasses() {
return new Class[] { Product.class, Category.class };
}
@Test
@TestForIssue( jiraKey = "HHH-12104" )
public void testAssociatedWhereClause() {
Product product = new Product();
Category category = new Category();
category.name = "flowers";
product.category = category;
product.containedCategory = new ContainedCategory();
product.containedCategory.category = category;
product.containedCategories.add( new ContainedCategory( category ) );
doInHibernate(
this::sessionFactory,
session -> {
session.persist( product );
}
);
doInHibernate(
this::sessionFactory,
session -> {
Product p = session.get( Product.class, product.id );
assertNotNull( p );
assertNotNull( p.category );
assertNotNull( p.containedCategory.category );
assertEquals( 1, p.containedCategories.size() );
assertSame( p.category, p.containedCategory.category );
assertSame( p.category, p.containedCategories.iterator().next().category );
}
);
doInHibernate(
this::sessionFactory,
session -> {
Category c = session.get( Category.class, category.id );
assertNotNull( c );
c.inactive = true;
}
);
doInHibernate(
this::sessionFactory,
session -> {
Category c = session.get( Category.class, category.id );
assertNull( c );
}
);
doInHibernate(
this::sessionFactory,
session -> {
// Entity's where clause is taken into account when to-one associations
// to that entity is loaded eagerly using FetchMode.SELECT, so Category
// associations will be null.
Product p = session.get( Product.class, product.id );
assertNotNull( p );
assertNull( p.category );
assertNull( p.containedCategory.category );
assertEquals( 1, p.containedCategories.size() );
assertNull( p.containedCategories.iterator().next().category );
}
);
}
@Entity(name = "Product")
public static class Product {
@Id
@GeneratedValue
private int id;
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@NotFound(action = NotFoundAction.IGNORE)
@JoinColumn(name = "categoryId")
@Fetch(FetchMode.SELECT)
private Category category;
private ContainedCategory containedCategory;
@ElementCollection(fetch = FetchType.EAGER)
private Set<ContainedCategory> containedCategories = new HashSet<>();
}
@Entity(name = "Category")
@Table(name = "CATEGORY")
@Where(clause = "inactive = 0")
public static class Category {
@Id
@GeneratedValue
private int id;
private String name;
private boolean inactive;
}
@Embeddable
public static class ContainedCategory {
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@NotFound(action = NotFoundAction.IGNORE)
@JoinColumn(name = "containedCategoryId")
@Fetch(FetchMode.SELECT)
private Category category;
public ContainedCategory() {
}
public ContainedCategory(Category category) {
this.category = category;
}
}
}

View File

@ -0,0 +1,42 @@
<?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="EagerManyToOneFetchModeJoinWhereTest$Product" table="PRODUCT">
<id name="id" column="ID">
<generator class="increment" />
</id>
<many-to-one name="category" lazy="false" fetch="join" cascade="all"
not-found="ignore" column="categoryId"/>
<component name="containedCategory" class="EagerManyToOneFetchModeJoinWhereTest$ContainedCategory">
<many-to-one name="category" lazy="false" fetch="join" cascade="all"
not-found="ignore" column="containedCategoryId"/>
</component>
<set name="containedCategories" lazy="false">
<key column="PRODUCT_ID"/>
<composite-element class="EagerManyToOneFetchModeJoinWhereTest$ContainedCategory">
<many-to-one name="category" lazy="false" fetch="join" cascade="all"
not-found="ignore" column="containedCategoryId"/>
</composite-element>
</set>
</class>
<class name="EagerManyToOneFetchModeJoinWhereTest$Category" table="CATEGORY" where="inactive = 0">
<id name="id" column="ID">
<generator class="increment" />
</id>
<property name="name"/>
<property name="inactive"/>
</class>
</hibernate-mapping>

View File

@ -0,0 +1,189 @@
/*
* 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;
import org.hibernate.testing.FailureExpected;
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.assertSame;
/**
* @author Gail Badner
*/
public class EagerManyToOneFetchModeJoinWhereTest extends BaseNonConfigCoreFunctionalTestCase {
protected String[] getMappings() {
return new String[] { "where/hbm/EagerManyToOneFetchModeJoinWhereTest.hbm.xml" };
}
@Test
@TestForIssue( jiraKey = "HHH-12104" )
@FailureExpected( jiraKey = "HHH-12104")
public void testAssociatedWhereClause() {
Product product = new Product();
Category category = new Category();
category.name = "flowers";
product.category = category;
product.containedCategory = new ContainedCategory();
product.containedCategory.category = category;
product.containedCategories.add( new ContainedCategory( category ) );
doInHibernate(
this::sessionFactory,
session -> {
session.persist( product );
}
);
doInHibernate(
this::sessionFactory,
session -> {
Product p = session.get( Product.class, product.id );
assertNotNull( p );
assertNotNull( p.category );
assertNotNull( p.containedCategory.category );
assertEquals( 1, p.containedCategories.size() );
assertSame( p.category, p.containedCategory.category );
assertSame( p.category, p.containedCategories.iterator().next().category );
}
);
doInHibernate(
this::sessionFactory,
session -> {
Category c = session.get( Category.class, category.id );
assertNotNull( c );
c.inactive = true;
}
);
doInHibernate(
this::sessionFactory,
session -> {
Category c = session.get( Category.class, category.id );
assertNull( c );
}
);
doInHibernate(
this::sessionFactory,
session -> {
// Entity's where clause is ignored when to-one associations to that
// association is loaded eagerly using FetchMode.JOIN, so the result
// should be the same as before the Category was made inactive.
Product p = session.get( Product.class, product.id );
assertNotNull( p );
assertNull( p.category );
assertNull( p.containedCategory.category );
assertEquals( 1, p.containedCategories.size() );
assertNull( p.containedCategories.iterator().next().category );
}
);
}
public static class Product {
private int id;
private Category category;
private ContainedCategory containedCategory;
private Set<ContainedCategory> containedCategories = new HashSet<>();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
public ContainedCategory getContainedCategory() {
return containedCategory;
}
public void setContainedCategory(ContainedCategory containedCategory) {
this.containedCategory = containedCategory;
}
public Set<ContainedCategory> getContainedCategories() {
return containedCategories;
}
public void setContainedCategories(Set<ContainedCategory> containedCategories) {
this.containedCategories = containedCategories;
}
}
public static class Category {
private int id;
private String name;
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 boolean isInactive() {
return inactive;
}
public void setInactive(boolean inactive) {
this.inactive = inactive;
}
}
public static class ContainedCategory {
private Category category;
public ContainedCategory() {
}
public ContainedCategory(Category category) {
this.category = category;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
}
}

View File

@ -0,0 +1,42 @@
<?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="EagerManyToOneFetchModeSelectWhereTest$Product" table="PRODUCT">
<id name="id" column="ID">
<generator class="increment" />
</id>
<many-to-one name="category" lazy="false" fetch="select" cascade="all"
not-found="ignore" column="categoryId"/>
<component name="containedCategory" class="EagerManyToOneFetchModeSelectWhereTest$ContainedCategory">
<many-to-one name="category" lazy="false" fetch="select" cascade="all"
not-found="ignore" column="containedCategoryId"/>
</component>
<set name="containedCategories" lazy="false">
<key column="PRODUCT_ID"/>
<composite-element class="EagerManyToOneFetchModeSelectWhereTest$ContainedCategory">
<many-to-one name="category" lazy="false" fetch="select" cascade="all"
not-found="ignore" column="containedCategoryId"/>
</composite-element>
</set>
</class>
<class name="EagerManyToOneFetchModeSelectWhereTest$Category" table="CATEGORY" where="inactive = 0">
<id name="id" column="ID">
<generator class="increment" />
</id>
<property name="name"/>
<property name="inactive"/>
</class>
</hibernate-mapping>

View File

@ -0,0 +1,187 @@
/*
* 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;
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.assertSame;
/**
* @author Gail Badner
*/
public class EagerManyToOneFetchModeSelectWhereTest extends BaseNonConfigCoreFunctionalTestCase {
protected String[] getMappings() {
return new String[] { "where/hbm/EagerManyToOneFetchModeSelectWhereTest.hbm.xml" };
}
@Test
@TestForIssue( jiraKey = "HHH-12104" )
public void testAssociatedWhereClause() {
Product product = new Product();
Category category = new Category();
category.name = "flowers";
product.category = category;
product.containedCategory = new ContainedCategory();
product.containedCategory.category = category;
product.containedCategories.add( new ContainedCategory( category ) );
doInHibernate(
this::sessionFactory,
session -> {
session.persist( product );
}
);
doInHibernate(
this::sessionFactory,
session -> {
Product p = session.get( Product.class, product.id );
assertNotNull( p );
assertNotNull( p.category );
assertNotNull( p.containedCategory.category );
assertEquals( 1, p.containedCategories.size() );
assertSame( p.category, p.containedCategory.category );
assertSame( p.category, p.containedCategories.iterator().next().category );
}
);
doInHibernate(
this::sessionFactory,
session -> {
Category c = session.get( Category.class, category.id );
assertNotNull( c );
c.inactive = true;
}
);
doInHibernate(
this::sessionFactory,
session -> {
Category c = session.get( Category.class, category.id );
assertNull( c );
}
);
doInHibernate(
this::sessionFactory,
session -> {
// Entity's where clause is taken into account when to-one associations
// to that entity is loaded eagerly using FetchMode.SELECT, so Category
// associations will be null.
Product p = session.get( Product.class, product.id );
assertNotNull( p );
assertNull( p.category );
assertNull( p.containedCategory.category );
assertEquals( 1, p.containedCategories.size() );
assertNull( p.containedCategories.iterator().next().category );
}
);
}
public static class Product {
private int id;
private Category category;
private ContainedCategory containedCategory;
private Set<ContainedCategory> containedCategories = new HashSet<>();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
public ContainedCategory getContainedCategory() {
return containedCategory;
}
public void setContainedCategory(ContainedCategory containedCategory) {
this.containedCategory = containedCategory;
}
public Set<ContainedCategory> getContainedCategories() {
return containedCategories;
}
public void setContainedCategories(Set<ContainedCategory> containedCategories) {
this.containedCategories = containedCategories;
}
}
public static class Category {
private int id;
private String name;
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 boolean isInactive() {
return inactive;
}
public void setInactive(boolean inactive) {
this.inactive = inactive;
}
}
public static class ContainedCategory {
private Category category;
public ContainedCategory() {
}
public ContainedCategory(Category category) {
this.category = category;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
}
}