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

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.propertyref; package org.hibernate.orm.test.propertyref;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Embeddable; 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" "-//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.propertyref"> <hibernate-mapping package="org.hibernate.orm.test.propertyref">
<class name="DoesNotWork" table="vgras007_v031"> <class name="DoesNotWork" table="vgras007_v031">
<composite-id name="doesNotWorkPk" class="DoesNotWorkPk"> <composite-id name="doesNotWorkPk" class="DoesNotWorkPk">
<key-property name="id1" column="track_no"/> <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> <hibernate-mapping>
<class <class
name="org.hibernate.test.propertyref.basic.EntityClass" name="org.hibernate.orm.test.propertyref.basic.EntityClass"
table="table1" lazy="false"> table="table1" lazy="false">
<id <id
name="key" name="key"

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