[JIRA] (HHH-17064) Fetch mode select causes StackOverflowError if used together with fetch type lazy
by Ruslan Gryn (JIRA)
Ruslan Gryn ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%... ) *updated* an issue
Hibernate ORM ( https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiYTRkYWM3ZDIx... ) / Bug ( https://hibernate.atlassian.net/browse/HHH-17064?atlOrigin=eyJpIjoiYTRkYW... ) HHH-17064 ( https://hibernate.atlassian.net/browse/HHH-17064?atlOrigin=eyJpIjoiYTRkYW... ) Fetch mode select causes StackOverflowError if used together with fetch type lazy ( https://hibernate.atlassian.net/browse/HHH-17064?atlOrigin=eyJpIjoiYTRkYW... )
Change By: Ruslan Gryn ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%... )
I discovered this issue during migration to Spring Boot 3 and Hibernate 6.2
So if we have the following entity
{noformat}@Entity
public class Main {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToOne
private Connector connector;
@ManyToOne(fetch = FetchType.LAZY)
private Sub sub;
}{noformat}
This entity has a reference to {{Sub}} entity which is lazy
{{Connector}} entity has a reference on {{Sub}} as well and FetchMode is a SELECT
{noformat}@Entity
public class Connector {
@Id
private Long id;
@ManyToOne
@Fetch(FetchMode.SELECT)
private Sub sub;
}{noformat}
when executing the following query {{select m from Main m where m.connector.id = 1}} you will get the following exception
{noformat}ava.lang.StackOverflowError
at org.hibernate.sql.results.graph.entity.EntityInitializer.findFirstEntityInitializer(EntityInitializer.java:34)
at org.hibernate.sql.results.graph.entity.internal.EntitySelectFetchInitializerBuilder.createInitializer(EntitySelectFetchInitializerBuilder.java:45)
at org.hibernate.sql.results.graph.entity.internal.EntityFetchSelectImpl.buildEntitySelectFetchInitializer(EntityFetchSelectImpl.java:92)
at org.hibernate.sql.results.graph.entity.internal.EntityFetchSelectImpl.lambda$createAssembler$0(EntityFetchSelectImpl.java:70)
at org.hibernate.sql.results.internal.ResultsHelper$1.resolveInitializer(ResultsHelper.java:117)
at org.hibernate.sql.results.graph.entity.internal.EntityFetchSelectImpl.createAssembler(EntityFetchSelectImpl.java:67)
at org.hibernate.sql.results.graph.entity.AbstractEntityInitializer.<init>(AbstractEntityInitializer.java:172)
at org.hibernate.sql.results.graph.entity.internal.EntityJoinedFetchInitializer.<init>(EntityJoinedFetchInitializer.java:48)
at org.hibernate.sql.results.graph.entity.internal.EntityFetchJoinedImpl.buildEntityJoinedFetchInitializer(EntityFetchJoinedImpl.java:127)
at org.hibernate.sql.results.graph.entity.internal.EntityFetchJoinedImpl.lambda$getEntityInitializer$0(EntityFetchJoinedImpl.java:100)
at org.hibernate.sql.results.internal.ResultsHelper$1.resolveInitializer(ResultsHelper.java:117)
at org.hibernate.sql.results.graph.entity.internal.EntityFetchJoinedImpl.getEntityInitializer(EntityFetchJoinedImpl.java:97)
at org.hibernate.sql.results.graph.entity.AbstractNonLazyEntityFetch.createAssembler(AbstractNonLazyEntityFetch.java:68)
at org.hibernate.sql.results.graph.entity.AbstractEntityInitializer.<init>(AbstractEntityInitializer.java:172)
at org.hibernate.sql.results.graph.entity.internal.EntityResultInitializer.<init>(EntityResultInitializer.java:35)
at org.hibernate.sql.results.graph.entity.internal.EntityResultImpl.lambda$createResultAssembler$0(EntityResultImpl.java:91)
at org.hibernate.sql.results.internal.ResultsHelper$1.resolveInitializer(ResultsHelper.java:117)
at org.hibernate.sql.results.graph.entity.internal.EntityResultImpl.createResultAssembler(EntityResultImpl.java:82)
at org.hibernate.sql.results.jdbc.internal.StandardJdbcValuesMapping.resolveAssemblers(StandardJdbcValuesMapping.java:53){noformat}
I’ve attached the reproducer. Run {{ORMStandaloneTestCase}} class to reproduce this issue
Also, it works fine on Hibernate 5
if use {{(a)Fetch(FetchMode.JOIN)}} then this case is working fine on Hibernate 6
( https://hibernate.atlassian.net/browse/HHH-17064#add-comment?atlOrigin=ey... ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-17064#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#100234- sha1:90f5128 )
1 year, 1 month
[JIRA] (HHH-17065) Column ordering leads to wrong column order in composite primary keys
by Benedikt Waldvogel (JIRA)
Benedikt Waldvogel ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%... ) *updated* an issue
Hibernate ORM ( https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiYzRkZjc0OWUz... ) / Bug ( https://hibernate.atlassian.net/browse/HHH-17065?atlOrigin=eyJpIjoiYzRkZj... ) HHH-17065 ( https://hibernate.atlassian.net/browse/HHH-17065?atlOrigin=eyJpIjoiYzRkZj... ) Column ordering leads to wrong column order in composite primary keys ( https://hibernate.atlassian.net/browse/HHH-17065?atlOrigin=eyJpIjoiYzRkZj... )
Change By: Benedikt Waldvogel ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%... )
Note: This issue is very similar to [https://hibernate.atlassian.net/browse/HHH-16943|https://hibernate.atlass...] but the same problem happens for *composite primary keys*.
h2. Given
{code:java}@Entity
@IdClass( CompositePrimaryKey.class )
static class TestEntity {
@Id private String b;
@Id private String a;
}
private static class CompositePrimaryKey {
private String b;
private String a;
// getters, setters, hashCode, equals
}{code}
Note that we define the composite primary key on the columns *(b, a)* (in that very order). The column order could be important since the underlying primary key index should be used to speed-up queries that select all entities for a given value of “b”.
h2. Actual vs. Expected
When the schema gets created in the context of a JPA EntityManagerFactory, the columns are (re-)ordered during construction of the [SessionFactoryImpl, Line 252: bootMetamodel.orderColumns( false )|https://github.com/hibernate/hibernate-orm/blob/58aff00957721074a4a3937256edd64a067bc943/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java#L252]
This leads to the following DDL:
{code:sql}create table "TestEntity" (
a varchar(255) not null,
b varchar(255) not null,
primary key (a, b)
){code}
Note that the primary key is created for {color:#bf2600}*(a, b)*{color} instead of the expected column order *(b, a)*.
h2. Workaround
One can restore the behavior of Hibernate 6.1 by switching to the legacy column ordering strategy via {{hibernate.column_ordering_strategy=legacy}}.
h2. Test Case
[https://github.com/hibernate/hibernate-orm/pull/7160|https://github.com/h...]
( https://hibernate.atlassian.net/browse/HHH-17065#add-comment?atlOrigin=ey... ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-17065#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#100234- sha1:90f5128 )
1 year, 1 month
[JIRA] (HHH-17065) Column ordering leads to wrong column order in composite primary keys
by Benedikt Waldvogel (JIRA)
Benedikt Waldvogel ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%... ) *updated* an issue
Hibernate ORM ( https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiODk3YWVjYjRh... ) / Bug ( https://hibernate.atlassian.net/browse/HHH-17065?atlOrigin=eyJpIjoiODk3YW... ) HHH-17065 ( https://hibernate.atlassian.net/browse/HHH-17065?atlOrigin=eyJpIjoiODk3YW... ) Column ordering leads to wrong column order in composite primary keys ( https://hibernate.atlassian.net/browse/HHH-17065?atlOrigin=eyJpIjoiODk3YW... )
Change By: Benedikt Waldvogel ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%... )
Note: This issue is very similar to [https://hibernate.atlassian.net/browse/HHH-16943|https://hibernate.atlass...] but the same problem happens for *composite primary keys*.
h2. Given
{code:java}@Entity
@IdClass( CompositePrimaryKey.class )
static class TestEntity {
@Id private String b;
@Id private String a;
}
private static class CompositePrimaryKey {
private String b;
private String a;
// getters, setters, hashCode, equals
}{code}
Note that we define the composite primary key on the columns *(b, a)* (in that very order). The column order could be important since the underlying primary key index should be used to speed-up queries that select all entities for a given value of “b”.
h2. Actual vs. Expected
When the schema gets created in the context of a JPA EntityManagerFactory, the columns are (re-)ordered during construction of the [SessionFactoryImpl, Line 252: bootMetamodel.orderColumns( false )|https://github.com/hibernate/hibernate-orm/blob/58aff00957721074a4a3937256edd64a067bc943/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java#L252]
This leads to the following DDL:
{code:sql}create table "TestEntity" (
a varchar(255) not null,
b varchar(255) not null,
primary key (a, b)
){code}
Note that the primary key is created for {color:#bf2600}*(a, b)*{color} instead of the expected column order *(b, a)*.
h2. Workaround
One can restore the behavior of Hibernate 6.1 by switching to the legacy column ordering strategy via {{hibernate.column_ordering_strategy=legacy}}.
h2. Test Case
_Upcoming…_ [https://github.com/hibernate/hibernate-orm/pull/7160|https://github.com/h...]
( https://hibernate.atlassian.net/browse/HHH-17065#add-comment?atlOrigin=ey... ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-17065#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#100234- sha1:90f5128 )
1 year, 1 month