Re-enable additional tests and fix hql queries not detecting circularity
This commit is contained in:
parent
92c70ecb08
commit
9e343ac0d7
|
@ -157,6 +157,11 @@ public class LoaderSqlAstCreationState
|
||||||
return visitedAssociationKeys.contains( associationKey );
|
return visitedAssociationKeys.contains( associationKey );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isRegisteringVisitedAssociationKeys(){
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ModelPart resolveModelPart(NavigablePath navigablePath) {
|
public ModelPart resolveModelPart(NavigablePath navigablePath) {
|
||||||
// for now, let's assume that the navigable-path refers to TableGroup
|
// for now, let's assume that the navigable-path refers to TableGroup
|
||||||
|
|
|
@ -358,7 +358,8 @@ public class ToOneAttributeMapping
|
||||||
DomainResultCreationState creationState) {
|
DomainResultCreationState creationState) {
|
||||||
final AssociationKey associationKey = foreignKeyDescriptor.getAssociationKey();
|
final AssociationKey associationKey = foreignKeyDescriptor.getAssociationKey();
|
||||||
|
|
||||||
if ( creationState.isAssociationKeyVisited( associationKey ) ) {
|
if ( creationState.isAssociationKeyVisited( associationKey )
|
||||||
|
|| bidirectionalAttributeName != null && !creationState.isRegisteringVisitedAssociationKeys() ) {
|
||||||
NavigablePath parentNavigablePath = fetchablePath.getParent();
|
NavigablePath parentNavigablePath = fetchablePath.getParent();
|
||||||
assert parentNavigablePath.equals( fetchParent.getNavigablePath() );
|
assert parentNavigablePath.equals( fetchParent.getNavigablePath() );
|
||||||
// The parent navigable path is {fk} if we are creating the domain result for the foreign key for a circular fetch
|
// The parent navigable path is {fk} if we are creating the domain result for the foreign key for a circular fetch
|
||||||
|
|
|
@ -38,6 +38,10 @@ public interface DomainResultCreationState {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
default boolean isRegisteringVisitedAssociationKeys(){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Resolve the ModelPart associated with a given NavigablePath. More specific ModelParts should be preferred - e.g.
|
* Resolve the ModelPart associated with a given NavigablePath. More specific ModelParts should be preferred - e.g.
|
||||||
* the SingularAssociationAttributeMapping rather than just the EntityTypeMapping for the associated type
|
* the SingularAssociationAttributeMapping rather than just the EntityTypeMapping for the associated type
|
||||||
|
|
|
@ -21,8 +21,9 @@
|
||||||
* 51 Franklin Street, Fifth Floor
|
* 51 Franklin Street, Fifth Floor
|
||||||
* Boston, MA 02110-1301 USA
|
* Boston, MA 02110-1301 USA
|
||||||
*/
|
*/
|
||||||
package org.hibernate.test.onetoone.bidirectional;
|
package org.hibernate.orm.test.onetoone.bidirectional;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
import javax.persistence.CascadeType;
|
import javax.persistence.CascadeType;
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
|
@ -41,6 +42,7 @@ import org.junit.jupiter.api.AfterEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertSame;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test cases for fetch joining a bi-directional one-to-one mapping.
|
* Test cases for fetch joining a bi-directional one-to-one mapping.
|
||||||
|
@ -116,10 +118,14 @@ public class BiDirectionalOneToOneFetchTest {
|
||||||
}
|
}
|
||||||
} );
|
} );
|
||||||
|
|
||||||
session.createQuery(
|
List<EntityA> list = session.createQuery(
|
||||||
"from EntityA a join fetch a.b"
|
"from EntityA a join fetch a.b"
|
||||||
).list();
|
).list();
|
||||||
|
|
||||||
|
EntityA entityA = list.get( 0 );
|
||||||
|
assertSame( entityA, entityA.getB().getA() );
|
||||||
|
|
||||||
|
|
||||||
assertEquals(
|
assertEquals(
|
||||||
"Join fetching inverse one-to-one didn't use the object already present in the result set!",
|
"Join fetching inverse one-to-one didn't use the object already present in the result set!",
|
||||||
1,
|
1,
|
||||||
|
@ -178,6 +184,14 @@ public class BiDirectionalOneToOneFetchTest {
|
||||||
this.b = b;
|
this.b = b;
|
||||||
this.b.a = this;
|
this.b.a = this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public EntityB getB() {
|
||||||
|
return b;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setB(EntityB b) {
|
||||||
|
this.b = b;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Entity(name = "EntityB")
|
@Entity(name = "EntityB")
|
||||||
|
@ -195,6 +209,10 @@ public class BiDirectionalOneToOneFetchTest {
|
||||||
public EntityB(Long id) {
|
public EntityB(Long id) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public EntityA getA() {
|
||||||
|
return a;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -8,143 +8,167 @@ package org.hibernate.test.onetoone.singletable;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import org.hibernate.Session;
|
|
||||||
import org.hibernate.Transaction;
|
|
||||||
import org.hibernate.cfg.Configuration;
|
|
||||||
import org.hibernate.cfg.Environment;
|
import org.hibernate.cfg.Environment;
|
||||||
|
import org.hibernate.engine.spi.SessionFactoryImplementor;
|
||||||
import org.hibernate.stat.EntityStatistics;
|
import org.hibernate.stat.EntityStatistics;
|
||||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import org.hibernate.testing.orm.junit.DomainModel;
|
||||||
import static org.junit.Assert.assertNotNull;
|
import org.hibernate.testing.orm.junit.ServiceRegistry;
|
||||||
import static org.junit.Assert.assertNull;
|
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.AfterEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Gavin King
|
* @author Gavin King
|
||||||
*/
|
*/
|
||||||
public class DiscrimSubclassOneToOneTest extends BaseCoreFunctionalTestCase {
|
@DomainModel(
|
||||||
@Override
|
xmlMappings = "org/hibernate/test/onetoone/singletable/Person.hbm.xml"
|
||||||
public String[] getMappings() {
|
)
|
||||||
return new String[] { "onetoone/singletable/Person.hbm.xml" };
|
@SessionFactory(
|
||||||
}
|
generateStatistics = true
|
||||||
|
)
|
||||||
|
@ServiceRegistry(
|
||||||
|
settings = @Setting(name = Environment.USE_SECOND_LEVEL_CACHE, value = "false")
|
||||||
|
)
|
||||||
|
public class DiscrimSubclassOneToOneTest {
|
||||||
|
|
||||||
@Override
|
@AfterEach
|
||||||
public void configure(Configuration cfg) {
|
public void teardDown(SessionFactoryScope scope) {
|
||||||
cfg.setProperty(Environment.USE_SECOND_LEVEL_CACHE, "false");
|
scope.inTransaction(
|
||||||
cfg.setProperty(Environment.GENERATE_STATISTICS, "true");
|
session -> {
|
||||||
|
session.createQuery( "delete from Address" ).executeUpdate();
|
||||||
|
session.createQuery( "delete from MailingAddress" ).executeUpdate();
|
||||||
|
session.createQuery( "delete from Person" ).executeUpdate();
|
||||||
|
session.createQuery( "delete from Org" ).executeUpdate();
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testOneToOneOnSubclass() {
|
public void testOneToOneOnSubclass(SessionFactoryScope scope) {
|
||||||
Person p = new Person();
|
|
||||||
p.name = "Gavin";
|
|
||||||
Address a = new Address();
|
|
||||||
a.entityName = "Gavin";
|
|
||||||
a.zip = "3181";
|
|
||||||
a.state = "VIC";
|
|
||||||
a.street = "Karbarook Ave";
|
|
||||||
p.address = a;
|
|
||||||
|
|
||||||
Session s = openSession();
|
|
||||||
Transaction t = s.beginTransaction();
|
|
||||||
s.persist(p);
|
|
||||||
t.commit();
|
|
||||||
s.close();
|
|
||||||
|
|
||||||
s = openSession();
|
|
||||||
t = s.beginTransaction();
|
|
||||||
|
|
||||||
EntityStatistics addressStats = sessionFactory().getStatistics().getEntityStatistics( Address.class.getName() );
|
|
||||||
EntityStatistics mailingAddressStats = sessionFactory().getStatistics().getEntityStatistics("MailingAddress");
|
|
||||||
|
|
||||||
p = (Person) s.createQuery("from Person p join fetch p.address left join fetch p.mailingAddress").uniqueResult();
|
scope.inTransaction(
|
||||||
assertNotNull(p.address); assertNull(p.mailingAddress);
|
session -> {
|
||||||
s.clear();
|
Person p = new Person();
|
||||||
|
p.name = "Gavin";
|
||||||
|
Address a = new Address();
|
||||||
|
a.entityName = "Gavin";
|
||||||
|
a.zip = "3181";
|
||||||
|
a.state = "VIC";
|
||||||
|
a.street = "Karbarook Ave";
|
||||||
|
p.address = a;
|
||||||
|
session.persist( p );
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
assertEquals( addressStats.getFetchCount(), 0 );
|
scope.inTransaction(
|
||||||
assertEquals( mailingAddressStats.getFetchCount(), 0 );
|
session -> {
|
||||||
|
SessionFactoryImplementor sessionFactory = scope.getSessionFactory();
|
||||||
p = (Person) s.createQuery("from Person p join fetch p.address").uniqueResult();
|
EntityStatistics addressStats = sessionFactory.getStatistics()
|
||||||
assertNotNull(p.address); assertNull(p.mailingAddress);
|
.getEntityStatistics( Address.class.getName() );
|
||||||
s.clear();
|
EntityStatistics mailingAddressStats = sessionFactory.getStatistics().getEntityStatistics(
|
||||||
|
"MailingAddress" );
|
||||||
assertEquals( addressStats.getFetchCount(), 0 );
|
|
||||||
assertEquals( mailingAddressStats.getFetchCount(), 1 );
|
|
||||||
|
|
||||||
p = (Person) s.createQuery("from Person").uniqueResult();
|
Person p = (Person) session.createQuery(
|
||||||
assertNotNull(p.address); assertNull(p.mailingAddress);
|
"from Person p join fetch p.address left join fetch p.mailingAddress" ).uniqueResult();
|
||||||
s.clear();
|
assertNotNull( p.address );
|
||||||
|
assertNull( p.mailingAddress );
|
||||||
assertEquals( addressStats.getFetchCount(), 1 );
|
session.clear();
|
||||||
assertEquals( mailingAddressStats.getFetchCount(), 2 );
|
|
||||||
|
|
||||||
p = (Person) s.createQuery("from Entity").uniqueResult();
|
assertEquals( 0, addressStats.getFetchCount() );
|
||||||
assertNotNull(p.address); assertNull(p.mailingAddress);
|
assertEquals( 0, mailingAddressStats.getFetchCount() );
|
||||||
s.clear();
|
|
||||||
|
|
||||||
assertEquals( addressStats.getFetchCount(), 2 );
|
|
||||||
assertEquals( mailingAddressStats.getFetchCount(), 3 );
|
|
||||||
|
|
||||||
//note that in here join fetch is used for the nullable
|
p = (Person) session.createQuery( "from Person p join fetch p.address" ).uniqueResult();
|
||||||
//one-to-one, due to a very special case of default
|
assertNotNull( p.address );
|
||||||
p = (Person) s.get(Person.class, "Gavin");
|
assertNull( p.mailingAddress );
|
||||||
assertNotNull(p.address); assertNull(p.mailingAddress);
|
session.clear();
|
||||||
s.clear();
|
|
||||||
|
|
||||||
assertEquals( addressStats.getFetchCount(), 2 );
|
|
||||||
assertEquals( mailingAddressStats.getFetchCount(), 3 );
|
|
||||||
|
|
||||||
p = (Person) s.get(Entity.class, "Gavin");
|
assertEquals( 0, addressStats.getFetchCount() );
|
||||||
assertNotNull(p.address); assertNull(p.mailingAddress);
|
assertEquals( 1, mailingAddressStats.getFetchCount() );
|
||||||
s.clear();
|
|
||||||
|
p = (Person) session.createQuery( "from Person" ).uniqueResult();
|
||||||
assertEquals( addressStats.getFetchCount(), 2 );
|
assertNotNull( p.address );
|
||||||
assertEquals( mailingAddressStats.getFetchCount(), 3 );
|
assertNull( p.mailingAddress );
|
||||||
|
session.clear();
|
||||||
t.commit();
|
|
||||||
s.close();
|
assertEquals( 1, addressStats.getFetchCount() );
|
||||||
|
assertEquals( 2, mailingAddressStats.getFetchCount() );
|
||||||
s = openSession();
|
|
||||||
t = s.beginTransaction();
|
p = (Person) session.createQuery( "from Entity" ).uniqueResult();
|
||||||
Org org = new Org();
|
assertNotNull( p.address );
|
||||||
org.name = "IFA";
|
assertNull( p.mailingAddress );
|
||||||
Address a2 = new Address();
|
session.clear();
|
||||||
a2.entityName = "IFA";
|
|
||||||
a2.zip = "3181";
|
assertEquals( 2, addressStats.getFetchCount() );
|
||||||
a2.state = "VIC";
|
assertEquals( 3, mailingAddressStats.getFetchCount() );
|
||||||
a2.street = "Orrong Rd";
|
|
||||||
org.addresses.add(a2);
|
//note that in here join fetch is used for the nullable
|
||||||
s.persist(org);
|
//one-to-one, due to a very special case of default
|
||||||
t.commit();
|
p = session.get( Person.class, "Gavin" );
|
||||||
s.close();
|
assertNotNull( p.address );
|
||||||
|
assertNull( p.mailingAddress );
|
||||||
s = openSession();
|
session.clear();
|
||||||
t = s.beginTransaction();
|
|
||||||
org = (Org) s.get(Entity.class, "IFA");
|
assertEquals( 2, addressStats.getFetchCount() );
|
||||||
s.clear();
|
assertEquals( 3, mailingAddressStats.getFetchCount() );
|
||||||
|
|
||||||
List list = s.createQuery("from Entity e order by e.name").list();
|
p = (Person) session.get( Entity.class, "Gavin" );
|
||||||
p = (Person) list.get(0);
|
assertNotNull( p.address );
|
||||||
assertNotNull(p.address); assertNull(p.mailingAddress);
|
assertNull( p.mailingAddress );
|
||||||
org = (Org) list.get(1);
|
session.clear();
|
||||||
assertEquals( org.addresses.size(), 1 );
|
|
||||||
s.clear();
|
assertEquals( 2, addressStats.getFetchCount() );
|
||||||
|
assertEquals( 3, mailingAddressStats.getFetchCount() );
|
||||||
list = s.createQuery("from Entity e left join fetch e.address left join fetch e.mailingAddress order by e.name").list();
|
|
||||||
p = (Person) list.get(0);
|
}
|
||||||
org = (Org) list.get(1);
|
);
|
||||||
assertNotNull(p.address); assertNull(p.mailingAddress);
|
|
||||||
assertEquals( org.addresses.size(), 1 );
|
scope.inTransaction(
|
||||||
|
session -> {
|
||||||
s.delete(p);
|
Org org = new Org();
|
||||||
s.delete(org);
|
org.name = "IFA";
|
||||||
|
Address a2 = new Address();
|
||||||
t.commit();
|
a2.entityName = "IFA";
|
||||||
s.close();
|
a2.zip = "3181";
|
||||||
|
a2.state = "VIC";
|
||||||
|
a2.street = "Orrong Rd";
|
||||||
|
org.addresses.add( a2 );
|
||||||
|
session.persist( org );
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
scope.inTransaction(
|
||||||
|
session -> {
|
||||||
|
Org org = (Org) session.get( Entity.class, "IFA" );
|
||||||
|
session.clear();
|
||||||
|
|
||||||
|
List list = session.createQuery( "from Entity e order by e.name" ).list();
|
||||||
|
Person p = (Person) list.get( 0 );
|
||||||
|
assertNotNull( p.address );
|
||||||
|
assertNull( p.mailingAddress );
|
||||||
|
org = (Org) list.get( 1 );
|
||||||
|
assertEquals( org.addresses.size(), 1 );
|
||||||
|
session.clear();
|
||||||
|
|
||||||
|
list = session.createQuery(
|
||||||
|
"from Entity e left join fetch e.address left join fetch e.mailingAddress order by e.name" )
|
||||||
|
.list();
|
||||||
|
p = (Person) list.get( 0 );
|
||||||
|
org = (Org) list.get( 1 );
|
||||||
|
assertNotNull( p.address );
|
||||||
|
assertNull( p.mailingAddress );
|
||||||
|
assertEquals( org.addresses.size(), 1 );
|
||||||
|
|
||||||
|
session.delete( p );
|
||||||
|
session.delete( org );
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue