Re-enable additional tests

This commit is contained in:
Andrea Boriero 2021-05-27 11:06:21 +02:00 committed by Andrea Boriero
parent f47a44e1cf
commit 020319b4fb
27 changed files with 652 additions and 674 deletions

View File

@ -0,0 +1,163 @@
/*
* 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.orm.test.onetomany;
import java.util.ArrayList;
import org.hibernate.CacheMode;
import org.hibernate.testing.orm.junit.BaseSessionFactoryFunctionalTest;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* What is done:
* ___ ___
* | | | |
* -> 1 -> 1
* | -transform-> / \
* 2 2 3
* |
* 3
*
* @author Burkhard Graves
* @author Gail Badner
*/
@SuppressWarnings({ "UnusedDeclaration" })
public abstract class AbstractRecursiveBidirectionalOneToManyTest extends BaseSessionFactoryFunctionalTest {
@Override
protected String[] getOrmXmlFiles() {
return new String[] { "org/hibernate/orm/test/onetomany/Node.hbm.xml" };
}
protected abstract CacheMode getSessionCacheMode();
@Test
public void testOneToManyMoveElement() {
init();
transformMove();
check( false );
delete();
}
@Test
public void testOneToManyMoveElementWithDirtySimpleProperty() {
init();
transformMoveWithDirtySimpleProperty();
check( true );
delete();
}
@Test
public void testOneToManyReplaceList() {
init();
transformReplace();
check( false );
delete();
}
void init() {
inTransaction(
session -> {
session.setCacheMode( getSessionCacheMode() );
Node node1 = new Node( 1, "node1" );
Node node2 = new Node( 2, "node2" );
Node node3 = new Node( 3, "node3" );
node1.addSubNode( node2 );
node2.addSubNode( node3 );
session.save( node1 );
}
);
}
void transformMove() {
inTransaction(
session -> {
session.setCacheMode( getSessionCacheMode() );
Node node3 = session.load( Node.class, new Integer( 3 ) );
Node node2 = node3.getParentNode();
Node node1 = node2.getParentNode();
node2.removeSubNode( node3 );
node1.addSubNode( node3 );
}
);
}
void transformMoveWithDirtySimpleProperty() {
inTransaction(
session -> {
session.setCacheMode( getSessionCacheMode() );
Node node3 = session.load( Node.class, new Integer( 3 ) );
Node node2 = node3.getParentNode();
Node node1 = node2.getParentNode();
node2.removeSubNode( node3 );
node1.addSubNode( node3 );
node3.setDescription( "node3-updated" );
}
);
}
void transformReplace() {
inTransaction(
session -> {
session.setCacheMode( getSessionCacheMode() );
Node node3 = session.load( Node.class, new Integer( 3 ) );
Node node2 = node3.getParentNode();
Node node1 = node2.getParentNode();
node2.removeSubNode( node3 );
node1.setSubNodes( new ArrayList() );
node1.addSubNode( node2 );
node1.addSubNode( node3 );
}
);
}
void check(boolean simplePropertyUpdated) {
inTransaction(
session -> {
session.setCacheMode( getSessionCacheMode() );
Node node3 = session.get( Node.class, Integer.valueOf( 3 ) );
// fails with 2nd level cache enabled
assertEquals( 1, node3.getParentNode().getId().intValue() );
assertEquals( ( simplePropertyUpdated ? "node3-updated" : "node3" ), node3.getDescription() );
assertTrue( node3.getSubNodes().isEmpty() );
Node node1 = node3.getParentNode();
assertNull( node1.getParentNode() );
assertEquals( 2, node1.getSubNodes().size() );
assertEquals( 2, ( (Node) node1.getSubNodes().get( 0 ) ).getId().intValue() );
assertEquals( "node1", node1.getDescription() );
Node node2 = (Node) node1.getSubNodes().get( 0 );
assertSame( node1, node2.getParentNode() );
assertTrue( node2.getSubNodes().isEmpty() );
assertEquals( "node2", node2.getDescription() );
}
);
}
void delete() {
inTransaction(
session -> {
session.setCacheMode( getSessionCacheMode() );
Node node1 = (Node) session.get( Node.class, Integer.valueOf( 1 ) );
session.delete( node1 );
}
);
}
}

View File

@ -0,0 +1,37 @@
/*
* 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.orm.test.onetomany;
import static org.junit.Assert.assertEquals;
/**
* @author Burkhard Graves
* @author Gail Badner
*/
public abstract class AbstractVersionedRecursiveBidirectionalOneToManyTest
extends AbstractRecursiveBidirectionalOneToManyTest {
@Override
protected String[] getOrmXmlFiles() {
return new String[] { "org/hibernate/orm/test/onetomany/VersionedNode.hbm.xml" };
}
@Override
void check(boolean simplePropertyUpdated) {
super.check( simplePropertyUpdated );
inTransaction(
session -> {
Node node1 = session.get( Node.class, Integer.valueOf( 1 ) );
Node node2 = session.get( Node.class, Integer.valueOf( 2 ) );
Node node3 = session.get( Node.class, Integer.valueOf( 3 ) );
assertEquals( 1, node1.getVersion() );
assertEquals( 1, node2.getVersion() );
assertEquals( 1, node3.getVersion() );
}
);
}
}

View File

@ -7,7 +7,7 @@
--> -->
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <!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.onetomany"> <hibernate-mapping package="org.hibernate.orm.test.onetomany">
<class name="Node" <class name="Node"
table="Node"> table="Node">
<cache usage="read-write" region="Node.entities"/> <cache usage="read-write" region="Node.entities"/>

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * 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>. * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/ */
package org.hibernate.test.onetomany; package org.hibernate.orm.test.onetomany;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;

View File

@ -4,15 +4,11 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * 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>. * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/ */
package org.hibernate.test.onetomany; package org.hibernate.orm.test.onetomany;
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import javax.persistence.CascadeType; import javax.persistence.CascadeType;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
@ -20,25 +16,42 @@ import javax.persistence.Id;
import javax.persistence.ManyToOne; import javax.persistence.ManyToOne;
import javax.persistence.OneToMany; import javax.persistence.OneToMany;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.hibernate.testing.TestForIssue; import org.hibernate.testing.TestForIssue;
import org.junit.Test; import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
import org.hibernate.testing.orm.junit.Jpa;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
public class OneToManyDuplicateInsertionTest extends BaseEntityManagerFunctionalTestCase { import static org.junit.jupiter.api.Assertions.assertEquals;
@Jpa(
annotatedClasses = {
OneToManyDuplicateInsertionTest.Parent.class,
OneToManyDuplicateInsertionTest.Child.class,
OneToManyDuplicateInsertionTest.ParentCascade.class,
OneToManyDuplicateInsertionTest.ChildCascade.class
}
)
public class OneToManyDuplicateInsertionTest {
private int parentId; private int parentId;
@Override @AfterEach
protected Class<?>[] getAnnotatedClasses() { public void tearDown(EntityManagerFactoryScope scope) {
return new Class<?>[]{ Parent.class, Child.class, ParentCascade.class, ChildCascade.class }; scope.inTransaction(
entityManager -> {
entityManager.createQuery( "delete from Child" ).executeUpdate();
entityManager.createQuery( "delete from Parent" ).executeUpdate();
}
);
} }
@Test @Test
@TestForIssue(jiraKey = "HHH-6776") @TestForIssue(jiraKey = "HHH-6776")
public void testDuplicateInsertion() { public void testDuplicateInsertion(EntityManagerFactoryScope scope) {
// persist parent entity in a transaction // persist parent entity in a transaction
doInJPA( this::entityManagerFactory, em -> { scope.inTransaction( em -> {
Parent parent = new Parent(); Parent parent = new Parent();
em.persist( parent ); em.persist( parent );
parentId = parent.getId(); parentId = parent.getId();
@ -46,7 +59,7 @@ public class OneToManyDuplicateInsertionTest extends BaseEntityManagerFunctional
// relate and persist child entity in another transaction // relate and persist child entity in another transaction
doInJPA( this::entityManagerFactory, em -> { scope.inTransaction( em -> {
Parent parent = em.find( Parent.class, parentId ); Parent parent = em.find( Parent.class, parentId );
Child child = new Child(); Child child = new Child();
child.setParent( parent ); child.setParent( parent );
@ -58,7 +71,7 @@ public class OneToManyDuplicateInsertionTest extends BaseEntityManagerFunctional
// get the parent again // get the parent again
doInJPA( this::entityManagerFactory, em -> { scope.inTransaction( em -> {
Parent parent = em.find( Parent.class, parentId ); Parent parent = em.find( Parent.class, parentId );
assertEquals( 1, parent.getChildren().size() ); assertEquals( 1, parent.getChildren().size() );
@ -67,15 +80,15 @@ public class OneToManyDuplicateInsertionTest extends BaseEntityManagerFunctional
@Test @Test
@TestForIssue(jiraKey = "HHH-7404") @TestForIssue(jiraKey = "HHH-7404")
public void testDuplicateInsertionWithCascadeAndMerge() { public void testDuplicateInsertionWithCascadeAndMerge(EntityManagerFactoryScope scope) {
doInJPA( this::entityManagerFactory, em -> { scope.inTransaction( em -> {
ParentCascade p = new ParentCascade(); ParentCascade p = new ParentCascade();
// merge with 0 children // merge with 0 children
p = em.merge( p ); p = em.merge( p );
parentId = p.getId(); parentId = p.getId();
} ); } );
doInJPA( this::entityManagerFactory, em -> { scope.inTransaction( em -> {
ParentCascade p = em.find( ParentCascade.class, parentId ); ParentCascade p = em.find( ParentCascade.class, parentId );
final ChildCascade child = new ChildCascade(); final ChildCascade child = new ChildCascade();
child.setParent( p ); child.setParent( p );
@ -83,7 +96,7 @@ public class OneToManyDuplicateInsertionTest extends BaseEntityManagerFunctional
em.merge( p ); em.merge( p );
} ); } );
doInJPA( this::entityManagerFactory, em -> { scope.inTransaction( em -> {
// again, load the Parent by id // again, load the Parent by id
ParentCascade p = em.find( ParentCascade.class, parentId ); ParentCascade p = em.find( ParentCascade.class, parentId );
@ -99,6 +112,8 @@ public class OneToManyDuplicateInsertionTest extends BaseEntityManagerFunctional
@GeneratedValue @GeneratedValue
private int id; private int id;
private String name;
@OneToMany(mappedBy = "parent") @OneToMany(mappedBy = "parent")
private List<Child> children = new LinkedList<Child>(); private List<Child> children = new LinkedList<Child>();
@ -126,6 +141,8 @@ public class OneToManyDuplicateInsertionTest extends BaseEntityManagerFunctional
@GeneratedValue @GeneratedValue
private int id; private int id;
private String name;
@ManyToOne @ManyToOne
private Parent parent; private Parent parent;

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * 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>. * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/ */
package org.hibernate.test.onetomany; package org.hibernate.orm.test.onetomany;
import org.hibernate.CacheMode; import org.hibernate.CacheMode;
@ -14,6 +14,6 @@ import org.hibernate.CacheMode;
*/ */
public class RecursiveBidirectionalOneToManyCacheTest extends AbstractRecursiveBidirectionalOneToManyTest { public class RecursiveBidirectionalOneToManyCacheTest extends AbstractRecursiveBidirectionalOneToManyTest {
protected CacheMode getSessionCacheMode() { protected CacheMode getSessionCacheMode() {
return CacheMode.NORMAL; return CacheMode.NORMAL;
} }
} }

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * 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>. * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/ */
package org.hibernate.test.onetomany; package org.hibernate.orm.test.onetomany;
import org.hibernate.CacheMode; import org.hibernate.CacheMode;

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * 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>. * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/ */
package org.hibernate.test.onetomany; package org.hibernate.orm.test.onetomany;
import org.hibernate.CacheMode; import org.hibernate.CacheMode;

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * 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>. * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/ */
package org.hibernate.test.onetomany; package org.hibernate.orm.test.onetomany;
import org.hibernate.CacheMode; import org.hibernate.CacheMode;

View File

@ -7,7 +7,7 @@
--> -->
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <!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.onetomany"> <hibernate-mapping package="org.hibernate.orm.test.onetomany">
<class name="Node" <class name="Node"
table="Node"> table="Node">
<cache usage="read-write" region="Node.entities"/> <cache usage="read-write" region="Node.entities"/>

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * 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>. * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/ */
package org.hibernate.test.onetomany.inheritance.joined; package org.hibernate.orm.test.onetomany.inheritance.joined;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.ManyToOne; import javax.persistence.ManyToOne;

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * 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>. * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/ */
package org.hibernate.test.onetomany.inheritance.joined; package org.hibernate.orm.test.onetomany.inheritance.joined;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;

View File

@ -0,0 +1,135 @@
/*
* 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.orm.test.onetomany.inheritance.joined;
import java.util.List;
import java.util.Map.Entry;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@TestForIssue(jiraKey = "HHH-11005")
@DomainModel(
annotatedClasses = {
Book.class,
Library.class,
Product.class
}
)
@SessionFactory
public class MappedSuperclassMapTest {
private static final String SKU001 = "SKU001";
private static final String SKU002 = "SKU002";
private static final String WAR_AND_PEACE = "0140447938";
private static final String ANNA_KARENINA = "0140449175";
@BeforeEach
public void init(SessionFactoryScope scope) {
scope.inTransaction( sess -> {
Book book1 = new Book( SKU001, WAR_AND_PEACE );
Book book2 = new Book( SKU002, ANNA_KARENINA );
sess.persist( book1 );
sess.flush();
sess.persist( book2 );
sess.flush();
Library library = new Library();
library.addBook( book1 );
library.addBook( book2 );
sess.persist( library );
} );
}
@Test
public void lookupEntities(SessionFactoryScope scope) {
scope.inTransaction( sess -> {
List<Library> libraries = sess.createQuery( "FROM Library" ).list();
assertEquals( 1, libraries.size() );
Library library = libraries.get( 0 );
assertNotNull( library );
assertEquals( 2, library.getBooksOnInventory().size() );
Book book = library.getBooksOnInventory().get( SKU001 );
assertNotNull( book );
Library Library = library;
Library.getBooksOnIsbn().get( WAR_AND_PEACE );
assertEquals( WAR_AND_PEACE, book.getIsbn() );
book = library.getBooksOnInventory().get( SKU002 );
assertNotNull( book );
assertEquals( ANNA_KARENINA, book.getIsbn() );
} );
}
@Test
public void lookupEntities_entrySet(SessionFactoryScope scope) {
scope.inTransaction( sess -> {
List<Library> libraries = sess.createQuery( "FROM Library" ).list();
assertEquals( 1, libraries.size() );
Library library = libraries.get( 0 );
assertNotNull( library );
assertEquals( 2, library.getBooksOnInventory().size() );
for ( Entry<String, Book> entry : library.getBooksOnInventory().entrySet() ) {
}
} );
}
@Test
public void breakReferences(SessionFactoryScope scope) {
scope.inTransaction( sess -> {
List<Book> books = sess.createQuery( "FROM Book" ).list();
assertEquals( 2, books.size() );
for ( Book book : books ) {
assertNotNull( book.getLibrary() );
book.getInventoryCode();
}
for ( Book book : books ) {
book.getLibrary().removeBook( book );
}
} );
scope.inTransaction( sess -> {
List<Book> books = sess.createQuery( "FROM Book" ).list();
assertEquals( 2, books.size() );
for ( Book book : books ) {
if ( book.getLibrary() == null ) {
book.getInventoryCode();
}
}
List<Library> libraries = sess.createQuery( "FROM Library" ).list();
assertEquals( 1, libraries.size() );
Library library = libraries.get( 0 );
assertNotNull( library );
assertEquals( 0, library.getBooksOnInventory().size() );
} );
}
@AfterEach
protected void cleanupTestData(SessionFactoryScope scope) throws Exception {
scope.inTransaction( sess -> {
sess.createQuery( "delete from Book" ).executeUpdate();
sess.createQuery( "delete from Library" ).executeUpdate();
} );
}
}

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * 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>. * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/ */
package org.hibernate.test.onetomany.inheritance.joined; package org.hibernate.orm.test.onetomany.inheritance.joined;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * 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>. * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/ */
package org.hibernate.test.onetomany.inheritance.perclass; package org.hibernate.orm.test.onetomany.inheritance.perclass;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.ManyToOne; import javax.persistence.ManyToOne;

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * 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>. * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/ */
package org.hibernate.test.onetomany.inheritance.perclass; package org.hibernate.orm.test.onetomany.inheritance.perclass;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;

View File

@ -0,0 +1,132 @@
/*
* 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.orm.test.onetomany.inheritance.perclass;
import java.util.List;
import java.util.Map.Entry;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@TestForIssue(jiraKey = "HHH-11005")
@DomainModel(
annotatedClasses = {
Book.class,
Library.class,
Product.class
}
)
@SessionFactory
public class MappedSuperclassMapTest {
private static final String SKU001 = "SKU001";
private static final String SKU002 = "SKU002";
private static final String WAR_AND_PEACE = "0140447938";
private static final String ANNA_KARENINA = "0140449175";
@BeforeEach
public void init(SessionFactoryScope scope) {
scope.inTransaction( sess -> {
Library library = new Library();
library.addBook( new Book( SKU001, WAR_AND_PEACE ) );
library.addBook( new Book( SKU002, ANNA_KARENINA ) );
sess.persist( library );
} );
}
@Test
public void lookupEntities(SessionFactoryScope scope) {
scope.inTransaction( sess -> {
List<Library> libraries = sess.createQuery( "FROM Library" ).list();
assertEquals( 1, libraries.size() );
Library library = libraries.get( 0 );
assertNotNull( library );
assertEquals( 2, library.getBooksOnInventory().size() );
Book book = library.getBooksOnInventory().get( SKU001 );
assertNotNull( book );
Library Library = library;
Library.getBooksOnIsbn().get( WAR_AND_PEACE );
assertEquals( WAR_AND_PEACE, book.getIsbn() );
book = library.getBooksOnInventory().get( SKU002 );
assertNotNull( book );
assertEquals( ANNA_KARENINA, book.getIsbn() );
} );
}
@Test
public void lookupEntities_entrySet(SessionFactoryScope scope) {
scope.inTransaction( sess -> {
List<Library> libraries = sess.createQuery( "FROM Library" ).list();
assertEquals( 1, libraries.size() );
Library library = libraries.get( 0 );
assertNotNull( library );
assertEquals( 2, library.getBooksOnInventory().size() );
for ( Entry<String, Book> entry : library.getBooksOnInventory().entrySet() ) {
entry.getKey();
entry.getValue().getIsbn();
}
} );
}
@Test
public void breakReferences(SessionFactoryScope scope) {
scope.inTransaction( sess -> {
List<Book> books = sess.createQuery( "FROM Book" ).list();
assertEquals( 2, books.size() );
for ( Book book : books ) {
assertNotNull( book.getLibrary() );
book.getInventoryCode();
book.getLibrary().getEntid();
}
for ( Book book : books ) {
book.getLibrary().removeBook( book );
}
} );
scope.inTransaction( sess -> {
List<Book> books = sess.createQuery( "FROM Book" ).list();
assertEquals( 2, books.size() );
for ( Book book : books ) {
if ( book.getLibrary() == null ) {
book.getInventoryCode();
}
}
List<Library> libraries = sess.createQuery( "FROM Library" ).list();
assertEquals( 1, libraries.size() );
Library library = libraries.get( 0 );
assertNotNull( library );
assertEquals( 0, library.getBooksOnInventory().size() );
} );
}
@AfterEach
protected void cleanupTestData(SessionFactoryScope scope) throws Exception {
scope.inTransaction( sess -> {
sess.createQuery( "delete from Book" ).executeUpdate();
sess.createQuery( "delete from Library" ).executeUpdate();
} );
}
}

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * 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>. * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/ */
package org.hibernate.test.onetomany.inheritance.perclass; package org.hibernate.orm.test.onetomany.inheritance.perclass;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * 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>. * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/ */
package org.hibernate.test.onetomany.inheritance.single; package org.hibernate.orm.test.onetomany.inheritance.single;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.ManyToOne; import javax.persistence.ManyToOne;

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * 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>. * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/ */
package org.hibernate.test.onetomany.inheritance.single; package org.hibernate.orm.test.onetomany.inheritance.single;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;

View File

@ -0,0 +1,132 @@
/*
* 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.orm.test.onetomany.inheritance.single;
import java.util.List;
import java.util.Map.Entry;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@TestForIssue(jiraKey = "HHH-11005")
@DomainModel(
annotatedClasses = {
Book.class,
Library.class,
Product.class
}
)
@SessionFactory
public class MappedSuperclassMapTest {
private static final String SKU001 = "SKU001";
private static final String SKU002 = "SKU002";
private static final String WAR_AND_PEACE = "0140447938";
private static final String ANNA_KARENINA = "0140449175";
@BeforeEach
public void init(SessionFactoryScope scope) {
scope.inTransaction( sess -> {
Library library = new Library();
library.addBook( new Book( SKU001, WAR_AND_PEACE ) );
library.addBook( new Book( SKU002, ANNA_KARENINA ) );
sess.persist( library );
} );
}
@Test
public void lookupEntities(SessionFactoryScope scope) {
scope.inTransaction( sess -> {
List<Library> libraries = sess.createQuery( "FROM Library" ).list();
assertEquals( 1, libraries.size() );
Library library = libraries.get( 0 );
assertNotNull( library );
assertEquals( 2, library.getBooksOnInventory().size() );
Book book = library.getBooksOnInventory().get( SKU001 );
assertNotNull( book );
Library Library = library;
Library.getBooksOnIsbn().get( WAR_AND_PEACE );
assertEquals( WAR_AND_PEACE, book.getIsbn() );
book = library.getBooksOnInventory().get( SKU002 );
assertNotNull( book );
assertEquals( ANNA_KARENINA, book.getIsbn() );
} );
}
@Test
public void lookupEntities_entrySet(SessionFactoryScope scope) {
scope.inTransaction( sess -> {
List<Library> libraries = sess.createQuery( "FROM Library" ).list();
assertEquals( 1, libraries.size() );
Library library = libraries.get( 0 );
assertNotNull( library );
assertEquals( 2, library.getBooksOnInventory().size() );
for ( Entry<String, Book> entry : library.getBooksOnInventory().entrySet() ) {
entry.getKey();
entry.getValue().getIsbn();
}
} );
}
@Test
public void breakReferences(SessionFactoryScope scope) {
scope.inTransaction( sess -> {
List<Book> books = sess.createQuery( "FROM Book" ).list();
assertEquals( 2, books.size() );
for ( Book book : books ) {
assertNotNull( book.getLibrary() );
book.getInventoryCode();
book.getLibrary().getEntid();
}
for ( Book book : books ) {
book.getLibrary().removeBook( book );
}
} );
scope.inTransaction( sess -> {
List<Book> books = sess.createQuery( "FROM Book" ).list();
assertEquals( 2, books.size() );
for ( Book book : books ) {
if ( book.getLibrary() == null ) {
book.getInventoryCode();
}
}
List<Library> libraries = sess.createQuery( "FROM Library" ).list();
assertEquals( 1, libraries.size() );
Library library = libraries.get( 0 );
assertNotNull( library );
assertEquals( 0, library.getBooksOnInventory().size() );
} );
}
@AfterEach
protected void cleanupTestData(SessionFactoryScope scope) throws Exception {
scope.inTransaction( sess -> {
sess.createQuery( "delete from Book" ).executeUpdate();
sess.createQuery( "delete from Library" ).executeUpdate();
} );
}
}

View File

@ -4,7 +4,7 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * 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>. * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/ */
package org.hibernate.test.onetomany.inheritance.single; package org.hibernate.orm.test.onetomany.inheritance.single;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;

View File

@ -1,174 +0,0 @@
/*
* 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.onetomany;
import java.util.ArrayList;
import org.junit.Test;
import org.hibernate.CacheMode;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
/**
* What is done:
* ___ ___
* | | | |
* -> 1 -> 1
* | -transform-> / \
* 2 2 3
* |
* 3
*
* @author Burkhard Graves
* @author Gail Badner
*/
@SuppressWarnings( {"UnusedDeclaration"})
public abstract class AbstractRecursiveBidirectionalOneToManyTest extends BaseCoreFunctionalTestCase {
@Override
public String[] getMappings() {
return new String[] { "onetomany/Node.hbm.xml" };
}
@Override
public Session openSession() {
Session s = super.openSession();
s.setCacheMode( getSessionCacheMode() );
return s;
}
protected abstract CacheMode getSessionCacheMode();
@Test
public void testOneToManyMoveElement() {
init();
transformMove();
check( false );
delete();
}
@Test
public void testOneToManyMoveElementWithDirtySimpleProperty() {
init();
transformMoveWithDirtySimpleProperty();
check( true );
delete();
}
@Test
public void testOneToManyReplaceList() {
init();
transformReplace();
check( false );
delete();
}
void init() {
Session s = openSession();
Transaction tx = s.beginTransaction();
Node node1 = new Node( 1, "node1" );
Node node2 = new Node( 2, "node2" );
Node node3 = new Node( 3, "node3" );
node1.addSubNode( node2 );
node2.addSubNode( node3 );
s.save(node1);
tx.commit();
s.close();
}
void transformMove() {
Session s = openSession();
Transaction tx = s.beginTransaction();
Node node3 = (Node) s.load(Node.class, new Integer(3));
Node node2 = node3.getParentNode();
Node node1 = node2.getParentNode();
node2.removeSubNode( node3 );
node1.addSubNode( node3 );
tx.commit();
s.close();
}
void transformMoveWithDirtySimpleProperty() {
Session s = openSession();
Transaction tx = s.beginTransaction();
Node node3 = (Node) s.load(Node.class, new Integer(3));
Node node2 = node3.getParentNode();
Node node1 = node2.getParentNode();
node2.removeSubNode( node3 );
node1.addSubNode( node3 );
node3.setDescription( "node3-updated" );
tx.commit();
s.close();
}
void transformReplace() {
Session s = openSession();
Transaction tx = s.beginTransaction();
Node node3 = (Node) s.load(Node.class, new Integer(3));
Node node2 = node3.getParentNode();
Node node1 = node2.getParentNode();
node2.removeSubNode( node3 );
node1.setSubNodes( new ArrayList() );
node1.addSubNode( node2 );
node1.addSubNode( node3 );
tx.commit();
s.close();
}
void check(boolean simplePropertyUpdated) {
Session s = openSession();
Transaction tx = s.beginTransaction();
Node node3 = (Node) s.get( Node.class, Integer.valueOf(3) );
// fails with 2nd level cache enabled
assertEquals( 1, node3.getParentNode().getId().intValue() );
assertEquals( ( simplePropertyUpdated ? "node3-updated" : "node3" ), node3.getDescription() );
assertTrue( node3.getSubNodes().isEmpty() );
Node node1 = node3.getParentNode();
assertNull( node1.getParentNode() );
assertEquals( 2, node1.getSubNodes().size() );
assertEquals( 2, ( ( Node ) node1.getSubNodes().get( 0 ) ).getId().intValue() );
assertEquals( "node1", node1.getDescription() );
Node node2 = ( Node ) node1.getSubNodes().get( 0 );
assertSame( node1, node2.getParentNode() );
assertTrue( node2.getSubNodes().isEmpty() );
assertEquals( "node2", node2.getDescription() );
tx.commit();
s.close();
}
void delete() {
Session s = openSession();
Transaction tx = s.beginTransaction();
Node node1 = ( Node ) s.get( Node.class, Integer.valueOf( 1 ) );
s.delete( node1 );
tx.commit();
s.close();
}
}

View File

@ -1,38 +0,0 @@
/*
* 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.onetomany;
import org.hibernate.Session;
import org.hibernate.Transaction;
import static org.junit.Assert.assertEquals;
/**
* @author Burkhard Graves
* @author Gail Badner
*/
public abstract class AbstractVersionedRecursiveBidirectionalOneToManyTest extends AbstractRecursiveBidirectionalOneToManyTest {
@Override
public String[] getMappings() {
return new String[] { "onetomany/VersionedNode.hbm.xml" };
}
@Override
void check(boolean simplePropertyUpdated) {
super.check( simplePropertyUpdated );
Session s = openSession();
Transaction tx = s.beginTransaction();
Node node1 = ( Node ) s.get( Node.class, Integer.valueOf( 1 ) );
Node node2 = ( Node ) s.get( Node.class, Integer.valueOf( 2 ) );
Node node3 = ( Node ) s.get( Node.class, Integer.valueOf( 3 ) );
assertEquals( 1, node1.getVersion() );
assertEquals( 1, node2.getVersion() );
assertEquals( 1, node3.getVersion() );
tx.commit();
s.close();
}
}

View File

@ -1,146 +0,0 @@
/*
* 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.onetomany.inheritance.joined;
import java.util.List;
import java.util.Map.Entry;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.junit.Before;
import org.junit.Test;
import org.jboss.logging.Logger;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
@TestForIssue(jiraKey = "HHH-11005")
public class MappedSuperclassMapTest extends BaseNonConfigCoreFunctionalTestCase {
private static final String SKU001 = "SKU001";
private static final String SKU002 = "SKU002";
private static final String WAR_AND_PEACE = "0140447938";
private static final String ANNA_KARENINA = "0140449175";
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[]{
Book.class,
Library.class,
Product.class
};
}
@Before
public void init() {
doInHibernate( this::sessionFactory, sess -> {
Book book1 = new Book( SKU001, WAR_AND_PEACE);
Book book2 = new Book( SKU002, ANNA_KARENINA);
sess.persist( book1 );
sess.flush();
sess.persist( book2 );
sess.flush();
Library library = new Library();
library.addBook( book1 );
library.addBook( book2 );
sess.persist(library);
} );
}
@Test
public void lookupEntities() {
doInHibernate( this::sessionFactory, sess -> {
List<Library> libraries = sess.createQuery( "FROM Library").list();
assertEquals(1, libraries.size());
Library library = libraries.get( 0);
assertNotNull(library);
assertEquals(2, library.getBooksOnInventory().size());
Book book = library.getBooksOnInventory().get( SKU001);
assertNotNull(book);
Library Library = library;
Library.getBooksOnIsbn().get( WAR_AND_PEACE );
assertEquals(WAR_AND_PEACE, book.getIsbn());
book = library.getBooksOnInventory().get(SKU002);
assertNotNull(book);
assertEquals(ANNA_KARENINA, book.getIsbn());
} );
}
@Test
public void lookupEntities_entrySet() {
doInHibernate( this::sessionFactory, sess -> {
List<Library> libraries = sess.createQuery( "FROM Library").list();
assertEquals(1, libraries.size());
Library library = libraries.get( 0);
assertNotNull(library);
assertEquals(2, library.getBooksOnInventory().size());
for (Entry<String,Book> entry : library.getBooksOnInventory().entrySet()) {
log.info("Found SKU " + entry.getKey() + " with ISBN " + entry.getValue().getIsbn());
}
} );
}
@Test
public void breakReferences() {
doInHibernate( this::sessionFactory, sess -> {
List<Book> books = sess.createQuery( "FROM Book").list();
assertEquals(2, books.size());
for (Book book : books) {
assertNotNull(book.getLibrary());
log.info("Found SKU " + book.getInventoryCode() + " with library " + book.getLibrary().getEntid());
}
for (Book book : books) {
book.getLibrary().removeBook( book );
}
} );
doInHibernate( this::sessionFactory, sess -> {
List<Book> books = sess.createQuery( "FROM Book").list();
assertEquals(2, books.size());
for (Book book : books) {
if (book.getLibrary() == null ) {
log.info("Found SKU " + book.getInventoryCode() + " with no library");
}
}
List<Library> libraries = sess.createQuery( "FROM Library").list();
assertEquals(1, libraries.size());
Library library = libraries.get( 0);
assertNotNull(library);
assertEquals(0, library.getBooksOnInventory().size());
log.info("Found Library " + library.getEntid() + " with no books");
} );
}
@Override
protected boolean isCleanupTestDataRequired() {
return true;
}
@Override
protected void cleanupTestData() throws Exception {
doInHibernate( this::sessionFactory, sess -> {
sess.createQuery( "delete from Book" ).executeUpdate();
sess.createQuery( "delete from Library" ).executeUpdate();
} );
}
@Override
protected boolean rebuildSessionFactoryOnError() {
return false;
}
}

View File

@ -1,140 +0,0 @@
/*
* 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.onetomany.inheritance.perclass;
import java.util.List;
import java.util.Map.Entry;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.junit.Before;
import org.junit.Test;
import org.jboss.logging.Logger;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
@TestForIssue(jiraKey = "HHH-11005")
public class MappedSuperclassMapTest extends BaseNonConfigCoreFunctionalTestCase {
private static final String SKU001 = "SKU001";
private static final String SKU002 = "SKU002";
private static final String WAR_AND_PEACE = "0140447938";
private static final String ANNA_KARENINA = "0140449175";
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[]{
Book.class,
Library.class,
Product.class
};
}
@Before
public void init() {
doInHibernate( this::sessionFactory, sess -> {
Library library = new Library();
library.addBook( new Book( SKU001, WAR_AND_PEACE) );
library.addBook( new Book( SKU002, ANNA_KARENINA) );
sess.persist(library);
} );
}
@Test
public void lookupEntities() {
doInHibernate( this::sessionFactory, sess -> {
List<Library> libraries = sess.createQuery( "FROM Library").list();
assertEquals(1, libraries.size());
Library library = libraries.get( 0);
assertNotNull(library);
assertEquals(2, library.getBooksOnInventory().size());
Book book = library.getBooksOnInventory().get( SKU001);
assertNotNull(book);
Library Library = library;
Library.getBooksOnIsbn().get( WAR_AND_PEACE );
assertEquals(WAR_AND_PEACE, book.getIsbn());
book = library.getBooksOnInventory().get(SKU002);
assertNotNull(book);
assertEquals(ANNA_KARENINA, book.getIsbn());
} );
}
@Test
public void lookupEntities_entrySet() {
doInHibernate( this::sessionFactory, sess -> {
List<Library> libraries = sess.createQuery( "FROM Library").list();
assertEquals(1, libraries.size());
Library library = libraries.get( 0);
assertNotNull(library);
assertEquals(2, library.getBooksOnInventory().size());
for (Entry<String,Book> entry : library.getBooksOnInventory().entrySet()) {
log.info("Found SKU " + entry.getKey() + " with ISBN " + entry.getValue().getIsbn());
}
} );
}
@Test
public void breakReferences() {
doInHibernate( this::sessionFactory, sess -> {
List<Book> books = sess.createQuery( "FROM Book").list();
assertEquals(2, books.size());
for (Book book : books) {
assertNotNull(book.getLibrary());
log.info("Found SKU " + book.getInventoryCode() + " with library " + book.getLibrary().getEntid());
}
for (Book book : books) {
book.getLibrary().removeBook( book );
}
} );
doInHibernate( this::sessionFactory, sess -> {
List<Book> books = sess.createQuery( "FROM Book").list();
assertEquals(2, books.size());
for (Book book : books) {
if (book.getLibrary() == null ) {
log.info("Found SKU " + book.getInventoryCode() + " with no library");
}
}
List<Library> libraries = sess.createQuery( "FROM Library").list();
assertEquals(1, libraries.size());
Library library = libraries.get( 0);
assertNotNull(library);
assertEquals(0, library.getBooksOnInventory().size());
log.info("Found Library " + library.getEntid() + " with no books");
} );
}
@Override
protected boolean isCleanupTestDataRequired() {
return true;
}
@Override
protected void cleanupTestData() throws Exception {
doInHibernate( this::sessionFactory, sess -> {
sess.createQuery( "delete from Book" ).executeUpdate();
sess.createQuery( "delete from Library" ).executeUpdate();
} );
}
@Override
protected boolean rebuildSessionFactoryOnError() {
return false;
}
}

View File

@ -1,140 +0,0 @@
/*
* 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.onetomany.inheritance.single;
import java.util.List;
import java.util.Map.Entry;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.junit.Before;
import org.junit.Test;
import org.jboss.logging.Logger;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
@TestForIssue(jiraKey = "HHH-11005")
public class MappedSuperclassMapTest extends BaseNonConfigCoreFunctionalTestCase {
private static final String SKU001 = "SKU001";
private static final String SKU002 = "SKU002";
private static final String WAR_AND_PEACE = "0140447938";
private static final String ANNA_KARENINA = "0140449175";
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[]{
Book.class,
Library.class,
Product.class
};
}
@Before
public void init() {
doInHibernate( this::sessionFactory, sess -> {
Library library = new Library();
library.addBook( new Book( SKU001, WAR_AND_PEACE) );
library.addBook( new Book( SKU002, ANNA_KARENINA) );
sess.persist(library);
} );
}
@Test
public void lookupEntities() {
doInHibernate( this::sessionFactory, sess -> {
List<Library> libraries = sess.createQuery( "FROM Library").list();
assertEquals(1, libraries.size());
Library library = libraries.get( 0);
assertNotNull(library);
assertEquals(2, library.getBooksOnInventory().size());
Book book = library.getBooksOnInventory().get( SKU001);
assertNotNull(book);
Library Library = library;
Library.getBooksOnIsbn().get( WAR_AND_PEACE );
assertEquals(WAR_AND_PEACE, book.getIsbn());
book = library.getBooksOnInventory().get(SKU002);
assertNotNull(book);
assertEquals(ANNA_KARENINA, book.getIsbn());
} );
}
@Test
public void lookupEntities_entrySet() {
doInHibernate( this::sessionFactory, sess -> {
List<Library> libraries = sess.createQuery( "FROM Library").list();
assertEquals(1, libraries.size());
Library library = libraries.get( 0);
assertNotNull(library);
assertEquals(2, library.getBooksOnInventory().size());
for (Entry<String,Book> entry : library.getBooksOnInventory().entrySet()) {
log.info("Found SKU " + entry.getKey() + " with ISBN " + entry.getValue().getIsbn());
}
} );
}
@Test
public void breakReferences() {
doInHibernate( this::sessionFactory, sess -> {
List<Book> books = sess.createQuery( "FROM Book").list();
assertEquals(2, books.size());
for (Book book : books) {
assertNotNull(book.getLibrary());
log.info("Found SKU " + book.getInventoryCode() + " with library " + book.getLibrary().getEntid());
}
for (Book book : books) {
book.getLibrary().removeBook( book );
}
} );
doInHibernate( this::sessionFactory, sess -> {
List<Book> books = sess.createQuery( "FROM Book").list();
assertEquals(2, books.size());
for (Book book : books) {
if (book.getLibrary() == null ) {
log.info("Found SKU " + book.getInventoryCode() + " with no library");
}
}
List<Library> libraries = sess.createQuery( "FROM Library").list();
assertEquals(1, libraries.size());
Library library = libraries.get( 0);
assertNotNull(library);
assertEquals(0, library.getBooksOnInventory().size());
log.info("Found Library " + library.getEntid() + " with no books");
} );
}
@Override
protected boolean isCleanupTestDataRequired() {
return true;
}
@Override
protected void cleanupTestData() throws Exception {
doInHibernate( this::sessionFactory, sess -> {
sess.createQuery( "delete from Book" ).executeUpdate();
sess.createQuery( "delete from Library" ).executeUpdate();
} );
}
@Override
protected boolean rebuildSessionFactoryOnError() {
return false;
}
}