Re-enable additional tests and fix hql queries not detecting circularity

This commit is contained in:
Andrea Boriero 2021-05-13 13:25:41 +02:00 committed by Andrea Boriero
parent 92c70ecb08
commit 9e343ac0d7
5 changed files with 175 additions and 123 deletions

View File

@ -157,6 +157,11 @@ public class LoaderSqlAstCreationState
return visitedAssociationKeys.contains( associationKey );
}
@Override
public boolean isRegisteringVisitedAssociationKeys(){
return true;
}
@Override
public ModelPart resolveModelPart(NavigablePath navigablePath) {
// for now, let's assume that the navigable-path refers to TableGroup

View File

@ -358,7 +358,8 @@ public class ToOneAttributeMapping
DomainResultCreationState creationState) {
final AssociationKey associationKey = foreignKeyDescriptor.getAssociationKey();
if ( creationState.isAssociationKeyVisited( associationKey ) ) {
if ( creationState.isAssociationKeyVisited( associationKey )
|| bidirectionalAttributeName != null && !creationState.isRegisteringVisitedAssociationKeys() ) {
NavigablePath parentNavigablePath = fetchablePath.getParent();
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

View File

@ -38,6 +38,10 @@ public interface DomainResultCreationState {
return false;
}
default boolean isRegisteringVisitedAssociationKeys(){
return false;
}
/**
* 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

View File

@ -21,8 +21,9 @@
* 51 Franklin Street, Fifth Floor
* 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 javax.persistence.CascadeType;
import javax.persistence.Entity;
@ -41,6 +42,7 @@ import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
/**
* 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"
).list();
EntityA entityA = list.get( 0 );
assertSame( entityA, entityA.getB().getA() );
assertEquals(
"Join fetching inverse one-to-one didn't use the object already present in the result set!",
1,
@ -178,6 +184,14 @@ public class BiDirectionalOneToOneFetchTest {
this.b = b;
this.b.a = this;
}
public EntityB getB() {
return b;
}
public void setB(EntityB b) {
this.b = b;
}
}
@Entity(name = "EntityB")
@ -195,6 +209,10 @@ public class BiDirectionalOneToOneFetchTest {
public EntityB(Long id) {
this.id = id;
}
public EntityA getA() {
return a;
}
}
}

View File

@ -8,143 +8,167 @@ package org.hibernate.test.onetoone.singletable;
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.engine.spi.SessionFactoryImplementor;
import org.hibernate.stat.EntityStatistics;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
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.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
*/
public class DiscrimSubclassOneToOneTest extends BaseCoreFunctionalTestCase {
@Override
public String[] getMappings() {
return new String[] { "onetoone/singletable/Person.hbm.xml" };
}
@DomainModel(
xmlMappings = "org/hibernate/test/onetoone/singletable/Person.hbm.xml"
)
@SessionFactory(
generateStatistics = true
)
@ServiceRegistry(
settings = @Setting(name = Environment.USE_SECOND_LEVEL_CACHE, value = "false")
)
public class DiscrimSubclassOneToOneTest {
@Override
public void configure(Configuration cfg) {
cfg.setProperty(Environment.USE_SECOND_LEVEL_CACHE, "false");
cfg.setProperty(Environment.GENERATE_STATISTICS, "true");
@AfterEach
public void teardDown(SessionFactoryScope scope) {
scope.inTransaction(
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
public void testOneToOneOnSubclass() {
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;
public void testOneToOneOnSubclass(SessionFactoryScope scope) {
Session s = openSession();
Transaction t = s.beginTransaction();
s.persist(p);
t.commit();
s.close();
scope.inTransaction(
session -> {
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 );
}
);
s = openSession();
t = s.beginTransaction();
scope.inTransaction(
session -> {
SessionFactoryImplementor sessionFactory = scope.getSessionFactory();
EntityStatistics addressStats = sessionFactory.getStatistics()
.getEntityStatistics( Address.class.getName() );
EntityStatistics mailingAddressStats = sessionFactory.getStatistics().getEntityStatistics(
"MailingAddress" );
EntityStatistics addressStats = sessionFactory().getStatistics().getEntityStatistics( Address.class.getName() );
EntityStatistics mailingAddressStats = sessionFactory().getStatistics().getEntityStatistics("MailingAddress");
Person p = (Person) session.createQuery(
"from Person p join fetch p.address left join fetch p.mailingAddress" ).uniqueResult();
assertNotNull( p.address );
assertNull( p.mailingAddress );
session.clear();
p = (Person) s.createQuery("from Person p join fetch p.address left join fetch p.mailingAddress").uniqueResult();
assertNotNull(p.address); assertNull(p.mailingAddress);
s.clear();
assertEquals( 0, addressStats.getFetchCount() );
assertEquals( 0, mailingAddressStats.getFetchCount() );
assertEquals( addressStats.getFetchCount(), 0 );
assertEquals( mailingAddressStats.getFetchCount(), 0 );
p = (Person) session.createQuery( "from Person p join fetch p.address" ).uniqueResult();
assertNotNull( p.address );
assertNull( p.mailingAddress );
session.clear();
p = (Person) s.createQuery("from Person p join fetch p.address").uniqueResult();
assertNotNull(p.address); assertNull(p.mailingAddress);
s.clear();
assertEquals( 0, addressStats.getFetchCount() );
assertEquals( 1, mailingAddressStats.getFetchCount() );
assertEquals( addressStats.getFetchCount(), 0 );
assertEquals( mailingAddressStats.getFetchCount(), 1 );
p = (Person) session.createQuery( "from Person" ).uniqueResult();
assertNotNull( p.address );
assertNull( p.mailingAddress );
session.clear();
p = (Person) s.createQuery("from Person").uniqueResult();
assertNotNull(p.address); assertNull(p.mailingAddress);
s.clear();
assertEquals( 1, addressStats.getFetchCount() );
assertEquals( 2, mailingAddressStats.getFetchCount() );
assertEquals( addressStats.getFetchCount(), 1 );
assertEquals( mailingAddressStats.getFetchCount(), 2 );
p = (Person) session.createQuery( "from Entity" ).uniqueResult();
assertNotNull( p.address );
assertNull( p.mailingAddress );
session.clear();
p = (Person) s.createQuery("from Entity").uniqueResult();
assertNotNull(p.address); assertNull(p.mailingAddress);
s.clear();
assertEquals( 2, addressStats.getFetchCount() );
assertEquals( 3, mailingAddressStats.getFetchCount() );
assertEquals( addressStats.getFetchCount(), 2 );
assertEquals( mailingAddressStats.getFetchCount(), 3 );
//note that in here join fetch is used for the nullable
//one-to-one, due to a very special case of default
p = session.get( Person.class, "Gavin" );
assertNotNull( p.address );
assertNull( p.mailingAddress );
session.clear();
//note that in here join fetch is used for the nullable
//one-to-one, due to a very special case of default
p = (Person) s.get(Person.class, "Gavin");
assertNotNull(p.address); assertNull(p.mailingAddress);
s.clear();
assertEquals( 2, addressStats.getFetchCount() );
assertEquals( 3, mailingAddressStats.getFetchCount() );
assertEquals( addressStats.getFetchCount(), 2 );
assertEquals( mailingAddressStats.getFetchCount(), 3 );
p = (Person) session.get( Entity.class, "Gavin" );
assertNotNull( p.address );
assertNull( p.mailingAddress );
session.clear();
p = (Person) s.get(Entity.class, "Gavin");
assertNotNull(p.address); assertNull(p.mailingAddress);
s.clear();
assertEquals( 2, addressStats.getFetchCount() );
assertEquals( 3, mailingAddressStats.getFetchCount() );
assertEquals( addressStats.getFetchCount(), 2 );
assertEquals( mailingAddressStats.getFetchCount(), 3 );
}
);
t.commit();
s.close();
scope.inTransaction(
session -> {
Org org = new Org();
org.name = "IFA";
Address a2 = new Address();
a2.entityName = "IFA";
a2.zip = "3181";
a2.state = "VIC";
a2.street = "Orrong Rd";
org.addresses.add( a2 );
session.persist( org );
}
);
s = openSession();
t = s.beginTransaction();
Org org = new Org();
org.name = "IFA";
Address a2 = new Address();
a2.entityName = "IFA";
a2.zip = "3181";
a2.state = "VIC";
a2.street = "Orrong Rd";
org.addresses.add(a2);
s.persist(org);
t.commit();
s.close();
scope.inTransaction(
session -> {
Org org = (Org) session.get( Entity.class, "IFA" );
session.clear();
s = openSession();
t = s.beginTransaction();
org = (Org) s.get(Entity.class, "IFA");
s.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 list = s.createQuery("from Entity e order by e.name").list();
p = (Person) list.get(0);
assertNotNull(p.address); assertNull(p.mailingAddress);
org = (Org) list.get(1);
assertEquals( org.addresses.size(), 1 );
s.clear();
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 );
s.delete(p);
s.delete(org);
t.commit();
s.close();
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 );
}
);
}
}