Re-enabled additional tests

This commit is contained in:
Andrea Boriero 2021-08-16 22:31:37 +02:00 committed by Christian Beikov
parent 4563c73abb
commit 94c4bcf66f
13 changed files with 399 additions and 399 deletions

View File

@ -4,7 +4,7 @@
* 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.propertyref;
package org.hibernate.orm.test.propertyref;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
@ -12,14 +12,12 @@ import javax.persistence.ElementCollection;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OrderColumn;
import javax.persistence.Table;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.annotations.IndexColumn;
import org.hibernate.annotations.ListIndexBase;
/**

View File

@ -4,7 +4,7 @@
* 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.propertyref;
package org.hibernate.orm.test.propertyref;
import javax.persistence.Column;
import javax.persistence.Embeddable;

View File

@ -0,0 +1,67 @@
/*
* 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.propertyref;
import java.util.List;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.dialect.H2Dialect;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.RequiresDialect;
import org.hibernate.testing.orm.junit.ServiceRegistry;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.hibernate.testing.orm.junit.Setting;
import org.junit.jupiter.api.Test;
/**
* @author Steve Ebersole
*/
@RequiresDialect(H2Dialect.class)
@DomainModel(
annotatedClasses = DoesNotWork.class
)
@SessionFactory
@ServiceRegistry(
settings = {
@Setting(name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "false"),
@Setting(name = AvailableSettings.HBM2DDL_IMPORT_FILES, value = "/org/hibernate/orm/test/propertyref/import.sql")
}
)
public class DoesNotWorkTest {
@Test
public void testIt(SessionFactoryScope scope) {
DoesNotWorkPk pk = new DoesNotWorkPk();
pk.setId1( "ZZZ" );
pk.setId2( "00" );
// {
// Session session = openSession();
// session.beginTransaction();
// DoesNotWork entity = new DoesNotWork( pk );
// entity.setGlobalNotes( Arrays.asList( "My first note!" ) );
// session.save( entity );
// session.getTransaction().commit();
// session.close();
// }
scope.inTransaction(
session -> {
DoesNotWork entity = (DoesNotWork) session.get( DoesNotWork.class, pk );
List<String> notes = entity.getGlobalNotes();
if ( notes != null && notes.size() > 0 ) {
for ( String s : notes ) {
System.out.println( s );
}
}
session.delete( entity );
}
);
}
}

View File

@ -0,0 +1,71 @@
/*
* 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.propertyref;
import java.util.List;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.dialect.H2Dialect;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.RequiresDialect;
import org.hibernate.testing.orm.junit.ServiceRegistry;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.hibernate.testing.orm.junit.Setting;
import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
/**
* @author Steve Ebersole
*/
@RequiresDialect(H2Dialect.class)
@DomainModel(
xmlMappings = "org/hibernate/orm/test/propertyref/Mapping.hbm.xml"
)
@SessionFactory
@ServiceRegistry(
settings = {
@Setting(name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "false"),
@Setting(name = AvailableSettings.HBM2DDL_IMPORT_FILES, value = "/org/hibernate/orm/test/propertyref/import.sql")
}
)
public class DoesNotWorkWithHbmTest {
@Test
public void testIt(SessionFactoryScope scope) {
DoesNotWorkPk pk = new DoesNotWorkPk();
pk.setId1( "ZZZ" );
pk.setId2( "00" );
// {
// Session session = openSession();
// session.beginTransaction();
// DoesNotWork entity = new DoesNotWork( pk );
// entity.setGlobalNotes( Arrays.asList( "My first note!" ) );
// session.save( entity );
// session.getTransaction().commit();
// session.close();
// }
scope.inTransaction(
session -> {
DoesNotWork entity = (DoesNotWork) session.get( DoesNotWork.class, pk );
assertNotNull( entity );
List<String> notes = entity.getGlobalNotes();
assertNotNull( notes );
assertEquals( 2, notes.size() );
for ( String s : notes ) {
System.out.println( s );
}
session.delete( entity );
}
);
}
}

View File

@ -9,7 +9,7 @@
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.hibernate.test.propertyref">
<hibernate-mapping package="org.hibernate.orm.test.propertyref">
<class name="DoesNotWork" table="vgras007_v031">
<composite-id name="doesNotWorkPk" class="DoesNotWorkPk">
<key-property name="id1" column="track_no"/>

View File

@ -0,0 +1,53 @@
/*
* 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.propertyref.basic;
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.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
/**
* @author Brett Meyer
*/
@DomainModel(
xmlMappings = "org/hibernate/orm/test/propertyref/basic/EntityClass.hbm.xml"
)
@SessionFactory
public class BasicPropertiesTest {
/**
* Really simple regression test for HHH-8689.
*/
@Test
@TestForIssue(jiraKey = "HHH-8689")
public void testProperties(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
EntityClass ec = new EntityClass();
ec.setKey( 1l );
ec.setField1( "foo1" );
ec.setField2( "foo2" );
session.persist( ec );
}
);
EntityClass ec = scope.fromTransaction(
session ->
session.get( EntityClass.class, 1l )
);
assertNotNull( ec );
assertEquals( ec.getField1(), "foo1" );
assertEquals( ec.getField2(), "foo2" );
}
}

View File

@ -11,7 +11,7 @@
<hibernate-mapping>
<class
name="org.hibernate.test.propertyref.basic.EntityClass"
name="org.hibernate.orm.test.propertyref.basic.EntityClass"
table="table1" lazy="false">
<id
name="key"

View File

@ -4,7 +4,7 @@
* 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.propertyref.basic;
package org.hibernate.orm.test.propertyref.basic;
public class EntityClass {

View File

@ -1,68 +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.propertyref;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration;
import org.hibernate.dialect.H2Dialect;
import org.hibernate.testing.RequiresDialect;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
/**
* @author Steve Ebersole
*/
@RequiresDialect(H2Dialect.class)
public class DoesNotWorkTest extends BaseCoreFunctionalTestCase {
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[] {DoesNotWork.class};
}
@Override
protected void configure(Configuration configuration) {
super.configure( configuration );
configuration.setProperty( AvailableSettings.USE_SECOND_LEVEL_CACHE, "false" );
configuration.setProperty( AvailableSettings.HBM2DDL_IMPORT_FILES, "/org/hibernate/test/propertyref/import.sql" );
}
@Test
public void testIt() {
DoesNotWorkPk pk = new DoesNotWorkPk();
pk.setId1( "ZZZ" );
pk.setId2( "00" );
// {
// Session session = openSession();
// session.beginTransaction();
// DoesNotWork entity = new DoesNotWork( pk );
// entity.setGlobalNotes( Arrays.asList( "My first note!" ) );
// session.save( entity );
// session.getTransaction().commit();
// session.close();
// }
{
Session session = openSession();
session.beginTransaction();
DoesNotWork entity = (DoesNotWork) session.get( DoesNotWork.class, pk );
List<String> notes = entity.getGlobalNotes();
if ( notes != null && notes.size() > 0 ) {
for ( String s : notes ) {
System.out.println( s );
}
}
session.delete( entity );
session.getTransaction().commit();
session.close();
}
}
}

View File

@ -1,72 +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.propertyref;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration;
import org.hibernate.dialect.H2Dialect;
import org.hibernate.testing.RequiresDialect;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
/**
* @author Steve Ebersole
*/
@RequiresDialect(H2Dialect.class)
public class DoesNotWorkWithHbmTest extends BaseCoreFunctionalTestCase {
@Override
protected String[] getMappings() {
return new String[] { "propertyref/Mapping.hbm.xml" };
}
@Override
protected void configure(Configuration configuration) {
super.configure( configuration );
configuration.setProperty( AvailableSettings.USE_SECOND_LEVEL_CACHE, "false" );
configuration.setProperty( AvailableSettings.HBM2DDL_IMPORT_FILES, "/org/hibernate/test/propertyref/import.sql" );
}
@Test
public void testIt() {
DoesNotWorkPk pk = new DoesNotWorkPk();
pk.setId1( "ZZZ" );
pk.setId2( "00" );
// {
// Session session = openSession();
// session.beginTransaction();
// DoesNotWork entity = new DoesNotWork( pk );
// entity.setGlobalNotes( Arrays.asList( "My first note!" ) );
// session.save( entity );
// session.getTransaction().commit();
// session.close();
// }
{
Session session = openSession();
session.beginTransaction();
DoesNotWork entity = (DoesNotWork) session.get( DoesNotWork.class, pk );
assertNotNull( entity );
List<String> notes = entity.getGlobalNotes();
assertNotNull( notes );
assertEquals( 2, notes.size() );
for ( String s : notes ) {
System.out.println( s );
}
session.delete( entity );
session.getTransaction().commit();
session.close();
}
}
}

View File

@ -1,54 +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.propertyref.basic;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
/**
* @author Brett Meyer
*/
public class BasicPropertiesTest extends BaseCoreFunctionalTestCase {
@Override
public String[] getMappings() {
return new String[] { "propertyref/basic/EntityClass.hbm.xml" };
}
/**
* Really simple regression test for HHH-8689.
*/
@Test
@TestForIssue(jiraKey = "HHH-8689")
public void testProperties() {
Session s = openSession();
Transaction t = s.beginTransaction();
EntityClass ec = new EntityClass();
ec.setKey( 1l );
ec.setField1( "foo1" );
ec.setField2( "foo2" );
s.persist( ec );
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
ec = (EntityClass) s.get( EntityClass.class, 1l );
t.commit();
s.close();
assertNotNull( ec );
assertEquals( ec.getField1(), "foo1" );
assertEquals( ec.getField2(), "foo2" );
}
}

View File

@ -8,236 +8,241 @@ package org.hibernate.test.propertyref.basic;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import org.hibernate.FetchMode;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.mapping.Column;
import org.hibernate.mapping.ForeignKey;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.junit.Test;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.ServiceRegistry;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.hibernate.testing.orm.junit.Setting;
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.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
/**
* @author Gavin King
*/
public class PropertyRefTest extends BaseNonConfigCoreFunctionalTestCase {
@Override
public String[] getMappings() {
return new String[] { "propertyref/basic/Person.hbm.xml" };
}
@DomainModel(
xmlMappings = "org/hibernate/test/propertyref/basic/Person.hbm.xml"
@Override
protected void addSettings(Map settings) {
settings.put( AvailableSettings.DEFAULT_BATCH_FETCH_SIZE, "1" );
settings.put( AvailableSettings.GENERATE_STATISTICS, "true");
}
@Override
public String getCacheConcurrencyStrategy() {
return null;
}
@Test
public void testNonLazyBagKeyPropertyRef() {
Session s = openSession();
Transaction t = s.beginTransaction();
Person p = new Person();
p.setName( "Steve" );
p.setUserId( "steve" );
p.getSystems().add( "QA" );
p.getSystems().add( "R&D" );
s.persist( p );
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
s.createQuery( "from Person" ).list();
s.clear();
s.createNativeQuery( "select {p.*} from PROPREF_PERS {p}" )
.addEntity( "p", Person.class.getName() )
.list();
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
List results = s.createQuery( "from Person" ).list();
Iterator itr = results.iterator();
while ( itr.hasNext() ) {
s.delete( itr.next() );
)
@SessionFactory(
generateStatistics = true
)
@ServiceRegistry(
settings = {
@Setting(name = AvailableSettings.DEFAULT_BATCH_FETCH_SIZE, value = "1")
}
t.commit();
s.close();
)
public class PropertyRefTest {
@Test
public void testNonLazyBagKeyPropertyRef(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
Person p = new Person();
p.setName( "Steve" );
p.setUserId( "steve" );
p.getSystems().add( "QA" );
p.getSystems().add( "R&D" );
session.persist( p );
}
);
scope.inTransaction(
session -> {
session.createQuery( "from Person" ).list();
session.clear();
session.createNativeQuery( "select {p.*} from PROPREF_PERS {p}" )
.addEntity( "p", Person.class.getName() )
.list();
}
);
scope.inTransaction(
session -> {
List results = session.createQuery( "from Person" ).list();
Iterator itr = results.iterator();
while ( itr.hasNext() ) {
session.delete( itr.next() );
}
}
);
}
@Test
public void testManyToManyPropertyRef() {
public void testManyToManyPropertyRef(SessionFactoryScope scope) {
// prepare some test data relating to the Group->Person many-to-many association
Session s = openSession();
Transaction t = s.beginTransaction();
Person p = new Person();
p.setName( "Steve" );
p.setUserId( "steve" );
s.persist( p );
Group g = new Group();
g.setName( "Admins" );
g.getUsers().add( p );
s.persist( g );
// force a flush and detachment here to test reattachment handling of the property-ref (HHH-1531)
t.commit();
s.close();
scope.inTransaction(
session -> {
Person p = new Person();
p.setName( "Steve" );
p.setUserId( "steve" );
session.persist( p );
g.setName( "Admins" );
g.getUsers().add( p );
session.persist( g );
// force a flush and detachment here to test reattachment handling of the property-ref (HHH-1531)
}
);
Person p2 = new Person();
p2.setName( "Max" );
p2.setUserId( "max" );
g.getUsers().add( p2 );
s = openSession();
t = s.beginTransaction();
s.update( g );
t.commit();
s.close();
scope.inTransaction(
session ->
session.update( g )
);
// test retrieval of the group
s = openSession();
t = s.beginTransaction();
g = ( Group ) s.createQuery( "from Group g left join fetch g.users" ).uniqueResult();
assertTrue( Hibernate.isInitialized( g.getUsers() ) );
assertEquals( 2, g.getUsers().size() );
s.delete( g );
s.createQuery( "delete Person" ).executeUpdate();
t.commit();
s.close();
}
@Test
public void testOneToOnePropertyRef() {
Session s = openSession();
Transaction t = s.beginTransaction();
Person p = new Person();
p.setName("Steve");
p.setUserId("steve");
Address a = new Address();
a.setAddress("Texas");
a.setCountry("USA");
p.setAddress(a);
a.setPerson(p);
s.save(p);
Person p2 = new Person();
p2.setName("Max");
p2.setUserId("max");
s.save(p2);
Account act = new Account();
act.setType('c');
act.setUser(p2);
p2.getAccounts().add(act);
s.save(act);
s.flush();
s.clear();
p = (Person) s.get( Person.class, p.getId() ); //get address reference by outer join
p2 = (Person) s.get( Person.class, p2.getId() ); //get null address reference by outer join
assertNull( p2.getAddress() );
assertNotNull( p.getAddress() );
List l = s.createQuery("from Person").list(); //pull address references for cache
assertEquals( l.size(), 2 );
assertTrue( l.contains(p) && l.contains(p2) );
s.clear();
l = s.createQuery("from Person p order by p.name").list(); //get address references by sequential selects
assertEquals( l.size(), 2 );
assertNull( ( (Person) l.get(0) ).getAddress() );
assertNotNull( ( (Person) l.get(1) ).getAddress() );
s.clear();
l = s.createQuery("from Person p left join fetch p.address a order by a.country").list(); //get em by outer join
assertEquals( l.size(), 2 );
if ( ( (Person) l.get(0) ).getName().equals("Max") ) {
assertNull( ( (Person) l.get(0) ).getAddress() );
assertNotNull( ( (Person) l.get(1) ).getAddress() );
}
else {
assertNull( ( (Person) l.get(1) ).getAddress() );
assertNotNull( ( (Person) l.get(0) ).getAddress() );
}
s.clear();
l = s.createQuery("from Person p left join p.accounts a").list();
for ( int i=0; i<2; i++ ) {
Object[] row = (Object[]) l.get(i);
Person px = (Person) row[0];
assertFalse( Hibernate.isInitialized( px.getAccounts() ) );
assertTrue( px.getAccounts().size()>0 || row[1]==null );
}
s.clear();
l = s.createQuery("from Person p left join fetch p.accounts a order by p.name").list();
Person p0 = (Person) l.get(0);
assertTrue( Hibernate.isInitialized( p0.getAccounts() ) );
assertEquals( p0.getAccounts().size(), 1 );
assertSame( ( (Account) p0.getAccounts().iterator().next() ).getUser(), p0 );
Person p1 = (Person) l.get(1);
assertTrue( Hibernate.isInitialized( p1.getAccounts() ) );
assertEquals( p1.getAccounts().size(), 0 );
s.clear();
Account acc = (Account) s.createQuery("from Account a left join fetch a.user").uniqueResult();
assertTrue( Hibernate.isInitialized(acc.getUser()) );
assertNotNull(acc.getUser());
assertTrue( acc.getUser().getAccounts().contains(acc) );
s.createQuery("delete from Address").executeUpdate();
s.createQuery("delete from Account").executeUpdate(); // to not break constraint violation between Person and Account
s.createQuery("delete from Person").executeUpdate();
t.commit();
s.close();
scope.inTransaction(
session -> {
Group group = (Group) session.createQuery( "from Group g left join fetch g.users" ).uniqueResult();
assertTrue( Hibernate.isInitialized( group.getUsers() ) );
assertEquals( 2, group.getUsers().size() );
session.delete( group );
session.createQuery( "delete Person" ).executeUpdate();
}
);
}
@Test
public void testJoinFetchPropertyRef() {
inTransaction(
public void testOneToOnePropertyRef(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
Person p = new Person();
p.setName( "Steve" );
p.setUserId( "steve" );
Address a = new Address();
a.setAddress( "Texas" );
a.setCountry( "USA" );
p.setAddress( a );
a.setPerson( p );
session.save( p );
Person p2 = new Person();
p2.setName( "Max" );
p2.setUserId( "max" );
session.save( p2 );
Account act = new Account();
act.setType( 'c' );
act.setUser( p2 );
p2.getAccounts().add( act );
session.save( act );
session.flush();
session.clear();
p = session.get( Person.class, p.getId() ); //get address reference by outer join
p2 = session.get( Person.class, p2.getId() ); //get null address reference by outer join
assertNull( p2.getAddress() );
assertNotNull( p.getAddress() );
List l = session.createQuery( "from Person" ).list(); //pull address references for cache
assertEquals( 2, l.size() );
assertTrue( l.contains( p ) && l.contains( p2 ) );
session.clear();
l = session.createQuery( "from Person p order by p.name" )
.list(); //get address references by sequential selects
assertEquals( 2, l.size() );
assertNull( ( (Person) l.get( 0 ) ).getAddress() );
assertNotNull( ( (Person) l.get( 1 ) ).getAddress() );
session.clear();
l = session.createQuery( "from Person p left join fetch p.address a order by a.country" )
.list(); //get em by outer join
assertEquals( 2, l.size() );
if ( ( (Person) l.get( 0 ) ).getName().equals( "Max" ) ) {
assertNull( ( (Person) l.get( 0 ) ).getAddress() );
assertNotNull( ( (Person) l.get( 1 ) ).getAddress() );
}
else {
assertNull( ( (Person) l.get( 1 ) ).getAddress() );
assertNotNull( ( (Person) l.get( 0 ) ).getAddress() );
}
session.clear();
l = session.createQuery( "from Person p left join p.accounts a" ).list();
for ( int i = 0; i < 2; i++ ) {
Person px = (Person) l.get( i );
assertFalse( Hibernate.isInitialized( px.getAccounts() ) );
if ( px.getName().equals( "Max" ) ) {
assertEquals( 1, px.getAccounts().size() );
}
else {
assertEquals( 0, px.getAccounts().size() );
}
}
session.clear();
l = session.createQuery( "from Person p left join fetch p.accounts a order by p.name" ).list();
Person p0 = (Person) l.get( 0 );
assertTrue( Hibernate.isInitialized( p0.getAccounts() ) );
assertEquals( 1, p0.getAccounts().size() );
assertSame( p0, ( (Account) p0.getAccounts().iterator().next() ).getUser() );
Person p1 = (Person) l.get( 1 );
assertTrue( Hibernate.isInitialized( p1.getAccounts() ) );
assertEquals( 0, p1.getAccounts().size() );
session.clear();
Account acc = (Account) session.createQuery( "from Account a left join fetch a.user" ).uniqueResult();
assertTrue( Hibernate.isInitialized( acc.getUser() ) );
assertNotNull( acc.getUser() );
assertTrue( acc.getUser().getAccounts().contains( acc ) );
session.createQuery( "delete from Address" ).executeUpdate();
session.createQuery( "delete from Account" )
.executeUpdate(); // to not break constraint violation between Person and Account
session.createQuery( "delete from Person" ).executeUpdate();
}
);
}
@Test
public void testJoinFetchPropertyRef(SessionFactoryScope scope) {
scope.inTransaction(
s -> {
Person p = new Person();
p.setName("Steve");
p.setUserId("steve");
p.setName( "Steve" );
p.setUserId( "steve" );
Address a = new Address();
a.setAddress("Texas");
a.setCountry("USA");
p.setAddress(a);
a.setPerson(p);
s.save(p);
a.setAddress( "Texas" );
a.setCountry( "USA" );
p.setAddress( a );
a.setPerson( p );
s.save( p );
s.flush();
s.clear();
sessionFactory().getStatistics().clear();
final SessionFactoryImplementor sessionFactory = scope.getSessionFactory();
sessionFactory.getStatistics().clear();
p = s.get( Person.class, p.getId() ); //get address reference by outer join
assertTrue( Hibernate.isInitialized( p.getAddress() ) );
assertNotNull( p.getAddress() );
assertEquals( sessionFactory().getStatistics().getPrepareStatementCount(), 1 );
assertEquals( sessionFactory().getStatistics().getEntityFetchCount(), 0 );
assertEquals( 1, sessionFactory.getStatistics().getPrepareStatementCount() );
assertEquals( 0, sessionFactory.getStatistics().getEntityFetchCount() );
s.clear();
sessionFactory().getStatistics().clear();
sessionFactory.getStatistics().clear();
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
CriteriaQuery<Person> criteria = criteriaBuilder.createQuery( Person.class );
@ -250,37 +255,37 @@ public class PropertyRefTest extends BaseNonConfigCoreFunctionalTestCase {
assertTrue( Hibernate.isInitialized( p.getAddress() ) );
assertNotNull( p.getAddress() );
assertEquals( sessionFactory().getStatistics().getPrepareStatementCount(), 2 );
assertEquals( sessionFactory().getStatistics().getEntityFetchCount(), 0 );
assertEquals( 2, sessionFactory.getStatistics().getPrepareStatementCount() );
assertEquals( 0, sessionFactory.getStatistics().getEntityFetchCount() );
s.createQuery("delete from Address").executeUpdate();
s.createQuery("delete from Person").executeUpdate();
s.createQuery( "delete from Address" ).executeUpdate();
s.createQuery( "delete from Person" ).executeUpdate();
}
);
}
@Test
public void testForeignKeyCreation() {
PersistentClass classMapping = metadata().getEntityBinding( Account.class.getName() );
public void testForeignKeyCreation(SessionFactoryScope scope) {
PersistentClass classMapping = scope.getMetadataImplementor().getEntityBinding( Account.class.getName() );
Iterator foreignKeyIterator = classMapping.getTable().getForeignKeyIterator();
boolean found = false;
while ( foreignKeyIterator.hasNext() ) {
ForeignKey element = (ForeignKey) foreignKeyIterator.next();
if(element.getReferencedEntityName().equals(Person.class.getName() ) ) {
if(!element.isReferenceToPrimaryKey() ) {
if ( element.getReferencedEntityName().equals( Person.class.getName() ) ) {
if ( !element.isReferenceToPrimaryKey() ) {
List referencedColumns = element.getReferencedColumns();
Column column = (Column) referencedColumns.get(0);
if(column.getName().equals("person_userid") ) {
Column column = (Column) referencedColumns.get( 0 );
if ( column.getName().equals( "person_userid" ) ) {
found = true; // extend test to include the columns
}
}
}
}
}
assertTrue("Property ref foreign key not found",found);
assertTrue( found, "Property ref foreign key not found" );
}
}