[JIRA] (HHH-16254) Session load by pk returns proxy in association when using "batch fetch size", "cache", "fetch mode select", "single table inheritance"
by Jones (JIRA)
Jones ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%... ) *updated* an issue
Hibernate ORM ( https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiMDJlZDNmNjFh... ) / Bug ( https://hibernate.atlassian.net/browse/HHH-16254?atlOrigin=eyJpIjoiMDJlZD... ) HHH-16254 ( https://hibernate.atlassian.net/browse/HHH-16254?atlOrigin=eyJpIjoiMDJlZD... ) Session load by pk returns proxy in association when using "batch fetch size", "cache", "fetch mode select", "single table inheritance" ( https://hibernate.atlassian.net/browse/HHH-16254?atlOrigin=eyJpIjoiMDJlZD... )
Change By: Jones ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%... )
{code:java}@Entity
@Table(name = "MY_TABLE")
@DiscriminatorColumn(name = "DISC_COL", discriminatorType = DiscriminatorType.INTEGER)
@DiscriminatorValue("0")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class ParentEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID")
Integer id;
@JoinColumn(name = "PARENT", foreignKey = @ForeignKey(name = "FK_PARENT_01"))
@ManyToOne
@Fetch(FetchMode.SELECT)
ParentEntity parent;
}{code}
{code:java}@Entity
@DiscriminatorValue("1")
public class ChildEntity extends ParentEntity {}{code}
{code:java}public class ORMUnitTestCase extends BaseCoreFunctionalTestCase {
// Add your entities here.
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] { ParentEntity.class, ChildEntity.class };
}
// Add in any settings that are specific to your test. See
// resources/hibernate.properties for the defaults.
@Override
protected void configure(Configuration configuration) {
super.configure(configuration);
configuration.setProperty(AvailableSettings.SHOW_SQL, Boolean.TRUE.toString());
configuration.setProperty(AvailableSettings.FORMAT_SQL, Boolean.TRUE.toString());
configuration.setProperty(AvailableSettings.DEFAULT_BATCH_FETCH_SIZE, "10");
}
// Add your tests, using standard JUnit.
@Test
public void hhh0000Test() throws Exception {
// BaseCoreFunctionalTestCase automatically creates the SessionFactory and
// provides the Session.
ChildEntity child1 = new ChildEntity();
ChildEntity child2 = new ChildEntity();
try (Session s = openSession()) {
Transaction tx = s.beginTransaction();
child1.parent = child2;
s.persist(child1);
s.persist(child2);
tx.commit();
// To simulate that all data was already persisted before the application starts
s.getSessionFactory().getCache().evictAllRegions();
}
try (Session s = openSession()) {
Transaction tx = s.beginTransaction();
ParentEntity entity = s.byId(ParentEntity.class).load(child1.id);
assertThat(entity.parent).isInstanceOf(ChildEntity.class);
tx.commit();
}
}
}{code}
{noformat}java.lang.AssertionError:
Expecting:
<org.hibernate.bugs.ChildEntity@713afa0e>
to be an instance of:
<org.hibernate.bugs.ChildEntity>
but was instance of:
<org.hibernate.bugs.ParentEntity$HibernateProxy$1AjGQHjF>
at org.hibernate.bugs.ORMUnitTestCase.hhh0000Test(ORMUnitTestCase.java:79)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.hibernate.testing.junit4.ExtendedFrameworkMethod.invokeExplosively(ExtendedFrameworkMethod.java:45)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:299)
at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:293)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.lang.Thread.run(Thread.java:833){noformat}
The exact scenario works fine in 6.1.7.Final and 5.6.15
The issue only happens when all the conditions are met: "batch fetch size", "cache", "fetch mode select"
Reproducible test case attached and also available at [https://github.com/ratoaq2/HHH-16254|https://github.com/ratoaq2/HHH-16254...]
Additional Info:
On 6.1.7 the parent is loaded using LoadEventListener.INTERNAL_LOAD_NULLABLE which doesn’t allow the proxy creation
On 6.2.0.RC3 the parent is loaded using LoadEventListener.INTERNAL_LOAD_LAZY which allows proxy creation.
( https://hibernate.atlassian.net/browse/HHH-16254#add-comment?atlOrigin=ey... ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-16254#add-comment?atlOrigin=ey... )
Get Jira notifications on your phone! Download the Jira Cloud app for Android ( https://play.google.com/store/apps/details?id=com.atlassian.android.jira.... ) or iOS ( https://itunes.apple.com/app/apple-store/id1006972087?pt=696495&ct=EmailN... ) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100217- sha1:65be9f2 )
1 year, 10 months
[JIRA] (HHH-16254) Session load by pk returns proxy in association when using "batch fetch size", "cache", "fetch mode select", "single table inheritance"
by Jones (JIRA)
Jones ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%... ) *updated* an issue
Hibernate ORM ( https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiMjIzNDFmMWY0... ) / Bug ( https://hibernate.atlassian.net/browse/HHH-16254?atlOrigin=eyJpIjoiMjIzND... ) HHH-16254 ( https://hibernate.atlassian.net/browse/HHH-16254?atlOrigin=eyJpIjoiMjIzND... ) Session load by pk returns proxy in association when using "batch fetch size", "cache", "fetch mode select", "single table inheritance" ( https://hibernate.atlassian.net/browse/HHH-16254?atlOrigin=eyJpIjoiMjIzND... )
Change By: Jones ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%... )
{code:java}@Entity
@Table(name = "MY_TABLE")
@DiscriminatorColumn(name = "DISC_COL", discriminatorType = DiscriminatorType.INTEGER)
@DiscriminatorValue("0")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class ParentEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID")
Integer id;
@JoinColumn(name = "PARENT", foreignKey = @ForeignKey(name = "FK_PARENT_01"))
@ManyToOne
@Fetch(FetchMode.SELECT)
ParentEntity parent;
}{code}
{code:java}@Entity
@DiscriminatorValue("1")
public class ChildEntity extends ParentEntity {}{code}
{code:java}public class ORMUnitTestCase extends BaseCoreFunctionalTestCase {
// Add your entities here.
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] { ParentEntity.class, ChildEntity.class };
}
// Add in any settings that are specific to your test. See
// resources/hibernate.properties for the defaults.
@Override
protected void configure(Configuration configuration) {
super.configure(configuration);
configuration.setProperty(AvailableSettings.SHOW_SQL, Boolean.TRUE.toString());
configuration.setProperty(AvailableSettings.FORMAT_SQL, Boolean.TRUE.toString());
configuration.setProperty(AvailableSettings.DEFAULT_BATCH_FETCH_SIZE, "10");
}
// Add your tests, using standard JUnit.
@Test
public void hhh0000Test() throws Exception {
// BaseCoreFunctionalTestCase automatically creates the SessionFactory and
// provides the Session.
ChildEntity child1 = new ChildEntity();
ChildEntity child2 = new ChildEntity();
try (Session s = openSession()) {
Transaction tx = s.beginTransaction();
child1.parent = child2;
s.persist(child1);
s.persist(child2);
tx.commit();
// To simulate that all data was already persisted before the application starts
s.getSessionFactory().getCache().evictAllRegions();
}
try (Session s = openSession()) {
Transaction tx = s.beginTransaction();
ParentEntity entity = s.byId(ParentEntity.class).load(child1.id);
assertThat(entity.parent).isInstanceOf(ChildEntity.class);
tx.commit();
}
}
}{code}
{noformat}java.lang.AssertionError:
Expecting:
<org.hibernate.bugs.ChildEntity@713afa0e>
to be an instance of:
<org.hibernate.bugs.ChildEntity>
but was instance of:
<org.hibernate.bugs.ParentEntity$HibernateProxy$1AjGQHjF>
at org.hibernate.bugs.ORMUnitTestCase.hhh0000Test(ORMUnitTestCase.java:79)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.hibernate.testing.junit4.ExtendedFrameworkMethod.invokeExplosively(ExtendedFrameworkMethod.java:45)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:299)
at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:293)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.lang.Thread.run(Thread.java:833){noformat}
The exact scenario works fine in 6.1.7.Final and 5.6.15
The issue only happens when all the conditions are met: "batch fetch size", "cache", "fetch mode select"
Reproducible test case to be uploaded attached and also available at [https://github.com/ratoaq2/HHH-16254|https://github.com/ratoaq2/HHH-16254...]
( https://hibernate.atlassian.net/browse/HHH-16254#add-comment?atlOrigin=ey... ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-16254#add-comment?atlOrigin=ey... )
Get Jira notifications on your phone! Download the Jira Cloud app for Android ( https://play.google.com/store/apps/details?id=com.atlassian.android.jira.... ) or iOS ( https://itunes.apple.com/app/apple-store/id1006972087?pt=696495&ct=EmailN... ) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100217- sha1:65be9f2 )
1 year, 10 months
[JIRA] (HHH-16254) Session load by pk returns proxy in association when using "batch fetch size", "cache", "fetch mode select", "single table inheritance"
by Jones (JIRA)
Jones ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%... ) *created* an issue
Hibernate ORM ( https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiOTU1Y2FmMWRi... ) / Bug ( https://hibernate.atlassian.net/browse/HHH-16254?atlOrigin=eyJpIjoiOTU1Y2... ) HHH-16254 ( https://hibernate.atlassian.net/browse/HHH-16254?atlOrigin=eyJpIjoiOTU1Y2... ) Session load by pk returns proxy in association when using "batch fetch size", "cache", "fetch mode select", "single table inheritance" ( https://hibernate.atlassian.net/browse/HHH-16254?atlOrigin=eyJpIjoiOTU1Y2... )
Issue Type: Bug Affects Versions: 6.2.0.CR3 Assignee: Unassigned Components: hibernate-core Created: 05/Mar/2023 22:01 PM Priority: Critical Reporter: Jones ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%... )
@Entity
@Table(name = "MY_TABLE" )
@DiscriminatorColumn(name = "DISC_COL" , discriminatorType = DiscriminatorType.INTEGER)
@DiscriminatorValue( "0" )
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class ParentEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID" )
Integer id;
@JoinColumn(name = "PARENT" , foreignKey = @ForeignKey(name = "FK_PARENT_01" ))
@ManyToOne
@Fetch(FetchMode.SELECT)
ParentEntity parent;
}
@Entity
@DiscriminatorValue( "1" )
public class ChildEntity extends ParentEntity {}
public class ORMUnitTestCase extends BaseCoreFunctionalTestCase {
// Add your entities here.
@Override
protected Class <?>[] getAnnotatedClasses() {
return new Class <?>[] { ParentEntity.class, ChildEntity.class };
}
// Add in any settings that are specific to your test. See
// resources/hibernate.properties for the defaults.
@Override
protected void configure(Configuration configuration) {
super.configure(configuration);
configuration.setProperty(AvailableSettings.SHOW_SQL, Boolean.TRUE.toString());
configuration.setProperty(AvailableSettings.FORMAT_SQL, Boolean.TRUE.toString());
configuration.setProperty(AvailableSettings.DEFAULT_BATCH_FETCH_SIZE, "10" );
}
// Add your tests, using standard JUnit.
@Test
public void hhh0000Test() throws Exception {
// BaseCoreFunctionalTestCase automatically creates the SessionFactory and
// provides the Session.
ChildEntity child1 = new ChildEntity();
ChildEntity child2 = new ChildEntity();
try (Session s = openSession()) {
Transaction tx = s.beginTransaction();
child1.parent = child2;
s.persist(child1);
s.persist(child2);
tx.commit();
// To simulate that all data was already persisted before the application starts
s.getSessionFactory().getCache().evictAllRegions();
}
try (Session s = openSession()) {
Transaction tx = s.beginTransaction();
ParentEntity entity = s.byId(ParentEntity.class).load(child1.id);
assertThat(entity.parent).isInstanceOf(ChildEntity.class);
tx.commit();
}
}
}
java.lang.AssertionError:
Expecting:
<org.hibernate.bugs.ChildEntity@713afa0e>
to be an instance of:
<org.hibernate.bugs.ChildEntity>
but was instance of:
<org.hibernate.bugs.ParentEntity$HibernateProxy$1AjGQHjF>
at org.hibernate.bugs.ORMUnitTestCase.hhh0000Test(ORMUnitTestCase.java:79)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.hibernate.testing.junit4.ExtendedFrameworkMethod.invokeExplosively(ExtendedFrameworkMethod.java:45)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:299)
at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:293)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.lang.Thread.run(Thread.java:833)
The exact scenario works fine in 6.1.7.Final and 5.6.15
The issue only happens when all the conditions are met: "batch fetch size", "cache", "fetch mode select"
Reproducible test case to be uploaded
( https://hibernate.atlassian.net/browse/HHH-16254#add-comment?atlOrigin=ey... ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-16254#add-comment?atlOrigin=ey... )
Get Jira notifications on your phone! Download the Jira Cloud app for Android ( https://play.google.com/store/apps/details?id=com.atlassian.android.jira.... ) or iOS ( https://itunes.apple.com/app/apple-store/id1006972087?pt=696495&ct=EmailN... ) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100217- sha1:65be9f2 )
1 year, 10 months