Re-enable additional tests

This commit is contained in:
Andrea Boriero 2020-08-20 18:07:04 +01:00
parent 5f2f225cc7
commit 5dfa67bd6f
17 changed files with 614 additions and 558 deletions

View File

@ -18,7 +18,7 @@
--> -->
<hibernate-mapping <hibernate-mapping
package="org.hibernate.test.array"> package="org.hibernate.orm.test.array">
<class name="A" lazy="true" table="aaa"> <class name="A" lazy="true" table="aaa">

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.array; package org.hibernate.orm.test.array;
/** /**

View File

@ -0,0 +1,54 @@
/*
* 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.array;
import org.hibernate.dialect.AbstractHANADialect;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.hibernate.testing.orm.junit.SkipForDialect;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
/**
* @author Emmanuel Bernard
*/
@DomainModel(
xmlMappings = "org/hibernate/orm/test/array/A.hbm.xml"
)
@SessionFactory
public class ArrayTest {
@Test
@SkipForDialect(dialectClass = AbstractHANADialect.class, reason = " HANA doesn't support tables consisting of only a single auto-generated column")
public void testArrayJoinFetch(SessionFactoryScope scope) {
A a = new A();
scope.inTransaction(
session -> {
B b = new B();
a.setBs( new B[] { b } );
session.persist( a );
}
);
scope.inTransaction(
session -> {
A retrieved = session.get( A.class, a.getId() );
assertNotNull( retrieved );
assertNotNull( retrieved.getBs() );
assertEquals( 1, retrieved.getBs().length );
assertNotNull( retrieved.getBs()[0] );
session.delete( retrieved );
session.delete( retrieved.getBs()[0] );
}
);
}
}

View File

@ -6,7 +6,7 @@
*/ */
//$Id: B.java 6527 2005-04-26 16:58:52Z oneovthafew $ //$Id: B.java 6527 2005-04-26 16:58:52Z oneovthafew $
package org.hibernate.test.array; package org.hibernate.orm.test.array;
/** /**

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.collection.set; package org.hibernate.orm.test.collection.set;
/** /**

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.collection.set; package org.hibernate.orm.test.collection.set;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;

View File

@ -9,7 +9,7 @@
"-//Hibernate/Hibernate Mapping DTD 3.0//EN" "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.hibernate.test.collection.set"> <hibernate-mapping package="org.hibernate.orm.test.collection.set">
<class name="Parent"> <class name="Parent">
<id name="name" column="NAME" type="string" /> <id name="name" column="NAME" type="string" />

View File

@ -9,7 +9,7 @@
"-//Hibernate/Hibernate Mapping DTD 3.0//EN" "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.hibernate.test.collection.set"> <hibernate-mapping package="org.hibernate.orm.test.collection.set">
<class name="Parent"> <class name="Parent">
<id name="name" column="NAME" type="string" /> <id name="name" column="NAME" type="string" />

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.collection.set; package org.hibernate.orm.test.collection.set;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;

View File

@ -0,0 +1,35 @@
/*
* 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.collection.set;
/**
* @author Gail Badner
*/
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.FailureExpected;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.Test;
@DomainModel(
xmlMappings = "org/hibernate/orm/test/collection/set/Mappings.hbm.xml",
concurrencyStrategy = "nonstrict-read-write"
)
@SessionFactory(generateStatistics = true)
public class PersistentSetNonLazyTest extends PersistentSetTest {
@Test
// @FailureExpected(
// jiraKey = "HHH-3799",
// reason = "known to fail with non-lazy collection using query cache"
// )
public void testLoadChildCheckParentContainsChildCache(SessionFactoryScope scope) {
super.testLoadChildCheckParentContainsChildCache( scope );
}
}

View File

@ -0,0 +1,430 @@
/*
* 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.collection.set;
import java.util.HashSet;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import org.hibernate.CacheMode;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.collection.internal.PersistentSet;
import org.hibernate.query.Query;
import org.hibernate.stat.CollectionStatistics;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.FailureExpected;
import org.hibernate.testing.orm.junit.ServiceRegistry;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* @author Steve Ebersole
*/
@DomainModel(
xmlMappings = "org/hibernate/orm/test/collection/set/Mappings.hbm.xml"
)
@SessionFactory(generateStatistics = true)
@ServiceRegistry(
settings = {
@ServiceRegistry.Setting(
name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "true"
),
@ServiceRegistry.Setting(
name = AvailableSettings.USE_QUERY_CACHE, value = "true"
)
}
)
public class PersistentSetTest {
@Test
public void testWriteMethodDirtying(SessionFactoryScope scope) {
Parent parent = new Parent( "p1" );
Child child = new Child( "c1" );
parent.getChildren().add( child );
child.setParent( parent );
Child otherChild = new Child( "c2" );
scope.inTransaction(
session -> {
session.save( parent );
session.flush();
// at this point, the set on parent has now been replaced with a PersistentSet...
PersistentSet children = (PersistentSet) parent.getChildren();
assertFalse( children.add( child ) );
assertFalse( children.isDirty() );
assertFalse( children.remove( otherChild ) );
assertFalse( children.isDirty() );
HashSet otherSet = new HashSet();
otherSet.add( child );
assertFalse( children.addAll( otherSet ) );
assertFalse( children.isDirty() );
assertFalse( children.retainAll( otherSet ) );
assertFalse( children.isDirty() );
otherSet = new HashSet();
otherSet.add( otherChild );
assertFalse( children.removeAll( otherSet ) );
assertFalse( children.isDirty() );
assertTrue( children.retainAll( otherSet ) );
assertTrue( children.isDirty() );
assertTrue( children.isEmpty() );
children.clear();
session.delete( child );
assertTrue( children.isDirty() );
session.flush();
children.clear();
assertFalse( children.isDirty() );
session.delete( parent );
}
);
}
@Test
public void testCollectionMerging(SessionFactoryScope scope) {
Parent p = new Parent( "p1" );
scope.inTransaction(
session -> {
Child child = new Child( "c1" );
p.getChildren().add( child );
child.setParent( p );
session.save( p );
}
);
CollectionStatistics stats = scope.getSessionFactory().getStatistics()
.getCollectionStatistics( Parent.class.getName() + ".children" );
long recreateCount = stats.getRecreateCount();
long updateCount = stats.getUpdateCount();
Parent merged = (Parent) scope.fromTransaction(
session ->
session.merge( p )
);
assertEquals( 1, merged.getChildren().size() );
assertEquals( recreateCount, stats.getRecreateCount() );
assertEquals( updateCount, stats.getUpdateCount() );
scope.inTransaction(
session -> {
Parent parent = session.get( Parent.class, "p1" );
assertEquals( 1, parent.getChildren().size() );
session.delete( parent );
}
);
}
@Test
public void testCollectiondirtyChecking(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
Parent parent = new Parent( "p1" );
Child child = new Child( "c1" );
parent.getChildren().add( child );
child.setParent( parent );
session.save( parent );
}
);
CollectionStatistics stats = scope.getSessionFactory().getStatistics()
.getCollectionStatistics( Parent.class.getName() + ".children" );
long recreateCount = stats.getRecreateCount();
long updateCount = stats.getUpdateCount();
Parent parent = scope.fromTransaction(
session -> {
Parent p = session.get( Parent.class, "p1" );
assertEquals( 1, p.getChildren().size() );
return p;
}
);
assertEquals( 1, parent.getChildren().size() );
assertEquals( recreateCount, stats.getRecreateCount() );
assertEquals( updateCount, stats.getUpdateCount() );
scope.inTransaction(
session -> {
assertEquals( 1, parent.getChildren().size() );
session.delete( parent );
}
);
}
@Test
public void testCompositeElementWriteMethodDirtying(SessionFactoryScope scope) {
Container container = new Container( "p1" );
Container.Content c1 = new Container.Content( "c1" );
container.getContents().add( c1 );
Container.Content c2 = new Container.Content( "c2" );
scope.inTransaction(
session -> {
session.save( container );
session.flush();
// at this point, the set on container has now been replaced with a PersistentSet...
PersistentSet children = (PersistentSet) container.getContents();
assertFalse( children.add( c1 ) );
assertFalse( children.isDirty() );
assertFalse( children.remove( c2 ) );
assertFalse( children.isDirty() );
HashSet otherSet = new HashSet();
otherSet.add( c1 );
assertFalse( children.addAll( otherSet ) );
assertFalse( children.isDirty() );
assertFalse( children.retainAll( otherSet ) );
assertFalse( children.isDirty() );
otherSet = new HashSet();
otherSet.add( c2 );
assertFalse( children.removeAll( otherSet ) );
assertFalse( children.isDirty() );
assertTrue( children.retainAll( otherSet ) );
assertTrue( children.isDirty() );
assertTrue( children.isEmpty() );
children.clear();
assertTrue( children.isDirty() );
session.flush();
children.clear();
assertFalse( children.isDirty() );
session.delete( container );
}
);
}
@Test
@FailureExpected(jiraKey = "HHH-2485")
public void testCompositeElementMerging(SessionFactoryScope scope) {
Container container = new Container( "p1" );
scope.inTransaction(
session -> {
Container.Content c1 = new Container.Content( "c1" );
container.getContents().add( c1 );
session.save( container );
}
);
CollectionStatistics stats = scope.getSessionFactory().getStatistics()
.getCollectionStatistics( Container.class.getName() + ".contents" );
long recreateCount = stats.getRecreateCount();
long updateCount = stats.getUpdateCount();
container.setName( "another name" );
scope.inTransaction(
session -> {
session.merge( container );
}
);
assertEquals( 1, container.getContents().size() );
assertEquals( recreateCount, stats.getRecreateCount() );
assertEquals( updateCount, stats.getUpdateCount() );
scope.inTransaction(
session -> {
Container c = session.get( Container.class, container.getId() );
assertEquals( 1, container.getContents().size() );
session.delete( container );
}
);
}
@Test
@FailureExpected(jiraKey = "HHH-2485")
public void testCompositeElementCollectionDirtyChecking(SessionFactoryScope scope) {
Container c = new Container( "p1" );
scope.inTransaction(
session -> {
Container.Content c1 = new Container.Content( "c1" );
c.getContents().add( c1 );
session.save( c );
}
);
CollectionStatistics stats = scope.getSessionFactory().getStatistics()
.getCollectionStatistics( Container.class.getName() + ".contents" );
long recreateCount = stats.getRecreateCount();
long updateCount = stats.getUpdateCount();
scope.inTransaction(
session -> {
Container c1 = session.get( Container.class, c.getId() );
assertEquals( 1, c1.getContents().size() );
}
);
assertEquals( 1, c.getContents().size() );
assertEquals( recreateCount, stats.getRecreateCount() );
assertEquals( updateCount, stats.getUpdateCount() );
scope.inTransaction(
session -> {
Container c1 = session.get( Container.class, c.getId() );
assertEquals( 1, c1.getContents().size() );
session.delete( c1 );
}
);
}
@Test
public void testLoadChildCheckParentContainsChildCache(SessionFactoryScope scope) {
Parent p1 = new Parent( "p1" );
Child c1 = new Child( "c1" );
c1.setDescription( "desc1" );
p1.getChildren().add( c1 );
c1.setParent( p1 );
Child otherChild = new Child( "c2" );
otherChild.setDescription( "desc2" );
p1.getChildren().add( otherChild );
otherChild.setParent( p1 );
scope.inTransaction(
session -> session.save( p1 )
);
scope.inTransaction(
session -> {
Parent parent = session.get( Parent.class, p1.getName() );
assertTrue( parent.getChildren().contains( c1 ) );
assertTrue( parent.getChildren().contains( otherChild ) );
}
);
scope.inTransaction(
session -> {
Child child = session.get( Child.class, c1.getName() );
assertTrue( child.getParent().getChildren().contains( child ) );
session.clear();
CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
CriteriaQuery<Child> criteria = criteriaBuilder.createQuery( Child.class );
Root<Child> root = criteria.from( Child.class );
criteria.where( criteriaBuilder.equal( root.get( "name" ), "c1" ) );
child = session.createQuery( criteria ).uniqueResult();
// child = ( Child ) session.createCriteria( Child.class, child.getName() )
// .setCacheable( true )
// .add( Restrictions.idEq( "c1" ) )
// .uniqueResult();
assertTrue( child.getParent().getChildren().contains( child ) );
assertTrue( child.getParent().getChildren().contains( otherChild ) );
session.clear();
CriteriaQuery<Child> criteriaQuery = criteriaBuilder.createQuery( Child.class );
Root<Child> childRoot = criteriaQuery.from( Child.class );
criteriaQuery.where( criteriaBuilder.equal( childRoot.get( "name" ), "c1" ) );
Query<Child> query = session.createQuery( criteriaQuery );
query.setCacheable( true );
child = query.uniqueResult();
// child = ( Child ) session.createCriteria( Child.class, child.getName() )
// .setCacheable( true )
// .add( Restrictions.idEq( "c1" ) )
// .uniqueResult();
assertTrue( child.getParent().getChildren().contains( child ) );
assertTrue( child.getParent().getChildren().contains( otherChild ) );
session.clear();
child = (Child) session.createQuery( "from Child where name = 'c1'" )
.setCacheable( true )
.uniqueResult();
assertTrue( child.getParent().getChildren().contains( child ) );
child = (Child) session.createQuery( "from Child where name = 'c1'" )
.setCacheable( true )
.uniqueResult();
assertTrue( child.getParent().getChildren().contains( child ) );
session.delete( child.getParent() );
}
);
}
@Test
public void testLoadChildCheckParentContainsChildNoCache(SessionFactoryScope scope) {
Parent p = new Parent( "p1" );
Child c1 = new Child( "c1" );
p.getChildren().add( c1 );
c1.setParent( p );
Child otherChild = new Child( "c2" );
p.getChildren().add( otherChild );
otherChild.setParent( p );
scope.inTransaction(
session -> session.save( p )
);
scope.inTransaction(
session -> {
session.setCacheMode( CacheMode.IGNORE );
Parent parent = session.get( Parent.class, p.getName() );
assertTrue( parent.getChildren().contains( c1 ) );
assertTrue( parent.getChildren().contains( otherChild ) );
}
);
scope.inTransaction(
session -> {
session.setCacheMode( CacheMode.IGNORE );
Child child = session.get( Child.class, c1.getName() );
assertTrue( child.getParent().getChildren().contains( child ) );
session.clear();
CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
CriteriaQuery<Child> criteria = criteriaBuilder.createQuery( Child.class );
Root<Child> root = criteria.from( Child.class );
criteria.where( criteriaBuilder.equal( root.get( "name" ), "c1" ) );
child = session.createQuery( criteria ).uniqueResult();
// child = ( Child ) session.createCriteria( Child.class, child.getName() )
// .add( Restrictions.idEq( "c1" ) )
// .uniqueResult();
assertTrue( child.getParent().getChildren().contains( child ) );
assertTrue( child.getParent().getChildren().contains( otherChild ) );
session.clear();
child = (Child) session.createQuery( "from Child where name = 'c1'" ).uniqueResult();
assertTrue( child.getParent().getChildren().contains( child ) );
session.delete( child.getParent() );
}
);
}
}

View File

@ -4,23 +4,19 @@
* 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.collection.set; package org.hibernate.orm.test.collection.set;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import javax.persistence.CollectionTable; import javax.persistence.CollectionTable;
import javax.persistence.ColumnResult;
import javax.persistence.ElementCollection; import javax.persistence.ElementCollection;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.JoinColumn; import javax.persistence.JoinColumn;
import javax.persistence.SqlResultSetMapping;
import javax.persistence.Table; import javax.persistence.Table;
import org.hibernate.query.NativeQuery;
import org.hibernate.testing.TestForIssue; import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test; import org.junit.Test;
@ -121,43 +117,8 @@ public class SetElementNullBasicTest extends BaseCoreFunctionalTestCase {
} }
); );
} }
//
// @SqlResultSetMapping(
// columns = {
// @ColumnResult( name = "a_id", type = long.class ),
// @ColumnResult( name = "a_name" )
// }
// )
private List<?> getCollectionElementRows(int id) { private List<?> getCollectionElementRows(int id) {
doInHibernate(
this::sessionFactory,
session -> {
final String qry = "SELECT a.id as a_id, a.name as a_name FROM AnEntity a where a.id = " + id;
final NativeQuery nativeQuery = session.createNativeQuery( qry );
nativeQuery.list();
nativeQuery.addRoot( "a", AnEntity.class )
.addIdColumnAliases( "a_id" )
.addProperty( "name", "a_name" );
nativeQuery.addFetch( "c", "a", "aCollection" );
}
);
doInHibernate(
this::sessionFactory,
session -> {
final String qry = "SELECT a.id as a_id, a.name as a_name, c.aCollection FROM AnEntity a join AnEntity_aCollection c on a.id = c.id and a.id = " + id;
final NativeQuery nativeQuery = session.createNativeQuery( qry );
nativeQuery.addRoot( "a", AnEntity.class )
.addIdColumnAliases( "a_id" )
.addProperty( "name", "a_name" );
nativeQuery.addFetch( "c", "a", "aCollection" );
nativeQuery.list();
}
);
return doInHibernate( return doInHibernate(
this::sessionFactory, session -> { this::sessionFactory, session -> {
return session.createNativeQuery( return session.createNativeQuery(

View File

@ -1,55 +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.array;
import org.junit.Test;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.dialect.AbstractHANADialect;
import org.hibernate.testing.SkipForDialect;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
/**
* @author Emmanuel Bernard
*/
public class ArrayTest extends BaseCoreFunctionalTestCase {
public String[] getMappings() {
return new String[] { "array/A.hbm.xml" };
}
@Test
@SkipForDialect(value = AbstractHANADialect.class, comment = " HANA doesn't support tables consisting of only a single auto-generated column")
public void testArrayJoinFetch() throws Exception {
Session s;
Transaction tx;
s = openSession();
tx = s.beginTransaction();
A a = new A();
B b = new B();
a.setBs( new B[] {b} );
s.persist( a );
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
a = (A) s.get( A.class, a.getId() );
assertNotNull( a );
assertNotNull( a.getBs() );
assertEquals( 1, a.getBs().length );
assertNotNull( a.getBs()[0] );
s.delete(a);
s.delete(a.getBs()[0]);
tx.commit();
s.close();
}
}

View File

@ -1,34 +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.collection.set;
import org.hibernate.testing.FailureExpected;
import org.junit.Test;
/**
* @author Gail Badner
*/
public class PersistentSetNonLazyTest extends PersistentSetTest {
public String[] getMappings() {
return new String[] { "collection/set/MappingsNonLazy.hbm.xml" };
}
@Override
protected String getCacheConcurrencyStrategy() {
return "nonstrict-read-write";
}
@Test
@Override
@FailureExpected(
jiraKey = "HHH-3799",
message = "known to fail with non-lazy collection using query cache"
)
public void testLoadChildCheckParentContainsChildCache() {
super.testLoadChildCheckParentContainsChildCache();
}
}

View File

@ -1,418 +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.collection.set;
import java.util.HashSet;
import java.util.Map;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import org.hibernate.CacheMode;
import org.hibernate.Session;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.collection.internal.PersistentSet;
import org.hibernate.query.Query;
import org.hibernate.stat.CollectionStatistics;
import org.hibernate.testing.FailureExpected;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/**
* @author Steve Ebersole
*/
public class PersistentSetTest extends BaseNonConfigCoreFunctionalTestCase {
@Override
public String[] getMappings() {
return new String[] { "collection/set/Mappings.hbm.xml" };
}
@Override
protected void addSettings(Map settings) {
super.addSettings( settings );
settings.put( AvailableSettings.GENERATE_STATISTICS, "true" );
settings.put( AvailableSettings.USE_SECOND_LEVEL_CACHE, "true" );
settings.put( AvailableSettings.USE_QUERY_CACHE, "true" );
}
@Test
public void testWriteMethodDirtying() {
Parent parent = new Parent( "p1" );
Child child = new Child( "c1" );
parent.getChildren().add( child );
child.setParent( parent );
Child otherChild = new Child( "c2" );
Session session = openSession();
session.beginTransaction();
session.save( parent );
session.flush();
// at this point, the set on parent has now been replaced with a PersistentSet...
PersistentSet children = ( PersistentSet ) parent.getChildren();
assertFalse( children.add( child ) );
assertFalse( children.isDirty() );
assertFalse( children.remove( otherChild ) );
assertFalse( children.isDirty() );
HashSet otherSet = new HashSet();
otherSet.add( child );
assertFalse( children.addAll( otherSet ) );
assertFalse( children.isDirty() );
assertFalse( children.retainAll( otherSet ) );
assertFalse( children.isDirty() );
otherSet = new HashSet();
otherSet.add( otherChild );
assertFalse( children.removeAll( otherSet ) );
assertFalse( children.isDirty() );
assertTrue( children.retainAll( otherSet ));
assertTrue( children.isDirty() );
assertTrue( children.isEmpty() );
children.clear();
session.delete( child );
assertTrue( children.isDirty() );
session.flush();
children.clear();
assertFalse( children.isDirty() );
session.delete( parent );
session.getTransaction().commit();
session.close();
}
@Test
public void testCollectionMerging() {
Session session = openSession();
session.beginTransaction();
Parent parent = new Parent( "p1" );
Child child = new Child( "c1" );
parent.getChildren().add( child );
child.setParent( parent );
session.save( parent );
session.getTransaction().commit();
session.close();
CollectionStatistics stats = sessionFactory().getStatistics().getCollectionStatistics( Parent.class.getName() + ".children" );
long recreateCount = stats.getRecreateCount();
long updateCount = stats.getUpdateCount();
session = openSession();
session.beginTransaction();
parent = ( Parent ) session.merge( parent );
session.getTransaction().commit();
session.close();
assertEquals( 1, parent.getChildren().size() );
assertEquals( recreateCount, stats.getRecreateCount() );
assertEquals( updateCount, stats.getUpdateCount() );
session = openSession();
session.beginTransaction();
parent = ( Parent ) session.get( Parent.class, "p1" );
assertEquals( 1, parent.getChildren().size() );
session.delete( parent );
session.getTransaction().commit();
session.close();
}
@Test
public void testCollectiondirtyChecking() {
Session session = openSession();
session.beginTransaction();
Parent parent = new Parent( "p1" );
Child child = new Child( "c1" );
parent.getChildren().add( child );
child.setParent( parent );
session.save( parent );
session.getTransaction().commit();
session.close();
CollectionStatistics stats = sessionFactory().getStatistics().getCollectionStatistics( Parent.class.getName() + ".children" );
long recreateCount = stats.getRecreateCount();
long updateCount = stats.getUpdateCount();
session = openSession();
session.beginTransaction();
parent = session.get( Parent.class, "p1" );
assertEquals( 1, parent.getChildren().size() );
session.getTransaction().commit();
session.close();
assertEquals( 1, parent.getChildren().size() );
assertEquals( recreateCount, stats.getRecreateCount() );
assertEquals( updateCount, stats.getUpdateCount() );
session = openSession();
session.beginTransaction();
assertEquals( 1, parent.getChildren().size() );
session.delete( parent );
session.getTransaction().commit();
session.close();
}
@Test
public void testCompositeElementWriteMethodDirtying() {
Container container = new Container( "p1" );
Container.Content c1 = new Container.Content( "c1" );
container.getContents().add( c1 );
Container.Content c2 = new Container.Content( "c2" );
Session session = openSession();
session.beginTransaction();
session.save( container );
session.flush();
// at this point, the set on container has now been replaced with a PersistentSet...
PersistentSet children = (PersistentSet) container.getContents();
assertFalse( children.add( c1 ) );
assertFalse( children.isDirty() );
assertFalse( children.remove( c2 ) );
assertFalse( children.isDirty() );
HashSet otherSet = new HashSet();
otherSet.add( c1 );
assertFalse( children.addAll( otherSet ) );
assertFalse( children.isDirty() );
assertFalse( children.retainAll( otherSet ) );
assertFalse( children.isDirty() );
otherSet = new HashSet();
otherSet.add( c2 );
assertFalse( children.removeAll( otherSet ) );
assertFalse( children.isDirty() );
assertTrue( children.retainAll( otherSet ));
assertTrue( children.isDirty() );
assertTrue( children.isEmpty() );
children.clear();
assertTrue( children.isDirty() );
session.flush();
children.clear();
assertFalse( children.isDirty() );
session.delete( container );
session.getTransaction().commit();
session.close();
}
@Test
@FailureExpected( jiraKey = "HHH-2485" )
public void testCompositeElementMerging() {
Session session = openSession();
session.beginTransaction();
Container container = new Container( "p1" );
Container.Content c1 = new Container.Content( "c1" );
container.getContents().add( c1 );
session.save( container );
session.getTransaction().commit();
session.close();
CollectionStatistics stats = sessionFactory().getStatistics().getCollectionStatistics( Container.class.getName() + ".contents" );
long recreateCount = stats.getRecreateCount();
long updateCount = stats.getUpdateCount();
container.setName( "another name" );
session = openSession();
session.beginTransaction();
container = ( Container ) session.merge( container );
session.getTransaction().commit();
session.close();
assertEquals( 1, container.getContents().size() );
assertEquals( recreateCount, stats.getRecreateCount() );
assertEquals( updateCount, stats.getUpdateCount() );
session = openSession();
session.beginTransaction();
container = session.get( Container.class, container.getId() );
assertEquals( 1, container.getContents().size() );
session.delete( container );
session.getTransaction().commit();
session.close();
}
@Test
@FailureExpected( jiraKey = "HHH-2485" )
public void testCompositeElementCollectionDirtyChecking() {
Session session = openSession();
session.beginTransaction();
Container container = new Container( "p1" );
Container.Content c1 = new Container.Content( "c1" );
container.getContents().add( c1 );
session.save( container );
session.getTransaction().commit();
session.close();
CollectionStatistics stats = sessionFactory().getStatistics().getCollectionStatistics( Container.class.getName() + ".contents" );
long recreateCount = stats.getRecreateCount();
long updateCount = stats.getUpdateCount();
session = openSession();
session.beginTransaction();
container = session.get( Container.class, container.getId() );
assertEquals( 1, container.getContents().size() );
session.getTransaction().commit();
session.close();
assertEquals( 1, container.getContents().size() );
assertEquals( recreateCount, stats.getRecreateCount() );
assertEquals( updateCount, stats.getUpdateCount() );
session = openSession();
session.beginTransaction();
container = session.get( Container.class, container.getId() );
assertEquals( 1, container.getContents().size() );
session.delete( container );
session.getTransaction().commit();
session.close();
}
@Test
public void testLoadChildCheckParentContainsChildCache() {
Parent parent = new Parent( "p1" );
Child child = new Child( "c1" );
child.setDescription( "desc1" );
parent.getChildren().add( child );
child.setParent( parent );
Child otherChild = new Child( "c2" );
otherChild.setDescription( "desc2" );
parent.getChildren().add( otherChild );
otherChild.setParent( parent );
Session session = openSession();
session.beginTransaction();
session.save( parent );
session.getTransaction().commit();
session = openSession();
session.beginTransaction();
parent = session.get( Parent.class, parent.getName() );
assertTrue( parent.getChildren().contains( child ) );
assertTrue( parent.getChildren().contains( otherChild ) );
session.getTransaction().commit();
session = openSession();
session.beginTransaction();
child = session.get( Child.class, child.getName() );
assertTrue( child.getParent().getChildren().contains( child ) );
session.clear();
CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
CriteriaQuery<Child> criteria = criteriaBuilder.createQuery( Child.class );
Root<Child> root = criteria.from( Child.class );
criteria.where( criteriaBuilder.equal( root.get( "name" ), "c1" ));
child = session.createQuery( criteria ).uniqueResult();
// child = ( Child ) session.createCriteria( Child.class, child.getName() )
// .setCacheable( true )
// .add( Restrictions.idEq( "c1" ) )
// .uniqueResult();
assertTrue( child.getParent().getChildren().contains( child ) );
assertTrue( child.getParent().getChildren().contains( otherChild ) );
session.clear();
CriteriaQuery<Child> criteriaQuery = criteriaBuilder.createQuery( Child.class );
Root<Child> childRoot = criteriaQuery.from( Child.class );
criteriaQuery.where( criteriaBuilder.equal( childRoot.get( "name" ), "c1" ));
Query<Child> query = session.createQuery( criteriaQuery );
query.setCacheable( true );
child = query.uniqueResult();
// child = ( Child ) session.createCriteria( Child.class, child.getName() )
// .setCacheable( true )
// .add( Restrictions.idEq( "c1" ) )
// .uniqueResult();
assertTrue( child.getParent().getChildren().contains( child ) );
assertTrue( child.getParent().getChildren().contains( otherChild ) );
session.clear();
child = ( Child ) session.createQuery( "from Child where name = 'c1'" )
.setCacheable( true )
.uniqueResult();
assertTrue( child.getParent().getChildren().contains( child ) );
child = ( Child ) session.createQuery( "from Child where name = 'c1'" )
.setCacheable( true )
.uniqueResult();
assertTrue( child.getParent().getChildren().contains( child ) );
session.delete( child.getParent() );
session.getTransaction().commit();
session.close();
}
@Test
public void testLoadChildCheckParentContainsChildNoCache() {
Parent parent = new Parent( "p1" );
Child child = new Child( "c1" );
parent.getChildren().add( child );
child.setParent( parent );
Child otherChild = new Child( "c2" );
parent.getChildren().add( otherChild );
otherChild.setParent( parent );
Session session = openSession();
session.beginTransaction();
session.save( parent );
session.getTransaction().commit();
session = openSession();
session.beginTransaction();
session.setCacheMode( CacheMode.IGNORE );
parent = session.get( Parent.class, parent.getName() );
assertTrue( parent.getChildren().contains( child ) );
assertTrue( parent.getChildren().contains( otherChild ) );
session.getTransaction().commit();
session = openSession();
session.beginTransaction();
session.setCacheMode( CacheMode.IGNORE );
child = session.get( Child.class, child.getName() );
assertTrue( child.getParent().getChildren().contains( child ) );
session.clear();
CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
CriteriaQuery<Child> criteria = criteriaBuilder.createQuery( Child.class );
Root<Child> root = criteria.from( Child.class );
criteria.where( criteriaBuilder.equal( root.get( "name" ), "c1" ) );
child = session.createQuery( criteria ).uniqueResult();
// child = ( Child ) session.createCriteria( Child.class, child.getName() )
// .add( Restrictions.idEq( "c1" ) )
// .uniqueResult();
assertTrue( child.getParent().getChildren().contains( child ) );
assertTrue( child.getParent().getChildren().contains( otherChild ) );
session.clear();
child = ( Child ) session.createQuery( "from Child where name = 'c1'" ).uniqueResult();
assertTrue( child.getParent().getChildren().contains( child ) );
session.delete( child.getParent() );
session.getTransaction().commit();
session.close();
}
}

View File

@ -102,6 +102,9 @@ public @interface DomainModel {
SharedCacheMode sharedCacheMode() default SharedCacheMode.ENABLE_SELECTIVE; SharedCacheMode sharedCacheMode() default SharedCacheMode.ENABLE_SELECTIVE;
boolean overrideCacheStrategy() default true;
String concurrencyStrategy() default "";
AccessType accessType() default AccessType.READ_WRITE; AccessType accessType() default AccessType.READ_WRITE;
@interface ExtraQueryImport { @interface ExtraQueryImport {

View File

@ -6,11 +6,24 @@
*/ */
package org.hibernate.testing.orm.junit; package org.hibernate.testing.orm.junit;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.NClob;
import java.util.Iterator;
import java.util.Optional; import java.util.Optional;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources; import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.spi.MetadataImplementor; import org.hibernate.boot.spi.MetadataImplementor;
import org.hibernate.mapping.Collection;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.Property;
import org.hibernate.mapping.RootClass;
import org.hibernate.mapping.SimpleValue;
import org.hibernate.type.BlobType;
import org.hibernate.type.ClobType;
import org.hibernate.type.NClobType;
import org.hibernate.testing.orm.domain.DomainModelDescriptor; import org.hibernate.testing.orm.domain.DomainModelDescriptor;
import org.hibernate.testing.orm.domain.StandardDomainModel; import org.hibernate.testing.orm.domain.StandardDomainModel;
@ -109,7 +122,13 @@ public class DomainModelExtension
metadataSources.addQueryImport( importedClass.getSimpleName(), importedClass ); metadataSources.addQueryImport( importedClass.getSimpleName(), importedClass );
} }
return (MetadataImplementor) metadataSources.buildMetadata(); MetadataImplementor metadataImplementor = (MetadataImplementor) metadataSources.buildMetadata();
applyCacheSettings(
metadataImplementor,
domainModelAnnotation.overrideCacheStrategy(),
domainModelAnnotation.concurrencyStrategy()
);
return metadataImplementor;
}; };
} }
@ -124,6 +143,64 @@ public class DomainModelExtension
return scope; return scope;
} }
protected static final void applyCacheSettings(Metadata metadata, boolean overrideCacheStrategy, String cacheConcurrencyStrategy) {
if ( !overrideCacheStrategy ) {
return;
}
if ( cacheConcurrencyStrategy.equals( "" ) ) {
return;
}
for ( PersistentClass entityBinding : metadata.getEntityBindings() ) {
if ( entityBinding.isInherited() ) {
continue;
}
boolean hasLob = false;
final Iterator props = entityBinding.getPropertyClosureIterator();
while ( props.hasNext() ) {
final Property prop = (Property) props.next();
if ( prop.getValue().isSimpleValue() ) {
if ( isLob( ( (SimpleValue) prop.getValue() ).getTypeName() ) ) {
hasLob = true;
break;
}
}
}
if ( !hasLob ) {
( (RootClass) entityBinding ).setCacheConcurrencyStrategy( cacheConcurrencyStrategy );
entityBinding.setCached( true );
}
}
for ( Collection collectionBinding : metadata.getCollectionBindings() ) {
boolean isLob = false;
if ( collectionBinding.getElement().isSimpleValue() ) {
isLob = isLob( ( (SimpleValue) collectionBinding.getElement() ).getTypeName() );
}
if ( !isLob ) {
collectionBinding.setCacheConcurrencyStrategy( cacheConcurrencyStrategy );
}
}
}
private static boolean isLob(String typeName) {
return "blob".equals( typeName )
|| "clob".equals( typeName )
|| "nclob".equals( typeName )
|| Blob.class.getName().equals( typeName )
|| Clob.class.getName().equals( typeName )
|| NClob.class.getName().equals( typeName )
|| BlobType.class.getName().equals( typeName )
|| ClobType.class.getName().equals( typeName )
|| NClobType.class.getName().equals( typeName );
}
@Override @Override
public void postProcessTestInstance(Object testInstance, ExtensionContext context) { public void postProcessTestInstance(Object testInstance, ExtensionContext context) {
findDomainModelScope( testInstance, context ); findDomainModelScope( testInstance, context );
@ -172,7 +249,6 @@ public class DomainModelExtension
final StandardServiceRegistry registry = serviceRegistryScope.getRegistry(); final StandardServiceRegistry registry = serviceRegistryScope.getRegistry();
model = producer.produceModel( registry ); model = producer.produceModel( registry );
return model; return model;
} }
@ -203,4 +279,8 @@ public class DomainModelExtension
model = null; model = null;
} }
} }
protected void afterMetadataBuilt(Metadata metadata) {
}
} }