[JIRA] (HHH-16123) Invalid generated MySQL query when accesing join from a treated path
by Roberts Z (JIRA)
Roberts Z ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=63bc113... ) *updated* an issue
Hibernate ORM ( https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiZGNkMDkzNTYy... ) / Bug ( https://hibernate.atlassian.net/browse/HHH-16123?atlOrigin=eyJpIjoiZGNkMD... ) HHH-16123 ( https://hibernate.atlassian.net/browse/HHH-16123?atlOrigin=eyJpIjoiZGNkMD... ) Invalid generated MySQL query when accesing join from a treated path ( https://hibernate.atlassian.net/browse/HHH-16123?atlOrigin=eyJpIjoiZGNkMD... )
Change By: Roberts Z ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=63bc113... )
When having the following model structure: (see the attachment for a minimal Spring project as a full reproducer)
{code:java}@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Publication extends BaseEntity {
@Column(name = "title")
private String title;
}
@Entity
public abstract class Book extends Publication {
@OneToMany(mappedBy = "book", cascade = CascadeType.REMOVE)
private List<AuthorParticipation> participations = new ArrayList<>();
}
@Entity @Table public class LongBook extends Book {}
@Entity @Table public class ShortBook extends Book {}
@Entity
@Table
public class Author extends BaseEntity {
@Column(name = "name")
private String name;
}
@Entity
@Table
public class AuthorParticipation extends BaseEntity {
@ManyToOne
@JoinColumn(name = "id_author", nullable = false)
private Author author;
@ManyToOne
@JoinColumn(name = "id_book", nullable = false)
private Book book;
}{code}
And when querying entities by the parent class (in this example: {{Publication}}) with the Criteria API and treating the root path to another abstract class, when accessing a join, for example:
{code:java}publicationRepository.findAll(
(root, query, criteriaBuilder) -> {
Root<Book> asBook = criteriaBuilder.treat(root, Book.class);
Join<Book, AuthorParticipation> joinAuthors = asBook.join("participations");
return criteriaBuilder.equal(
joinAuthors
.get("author")
.get("name"),
"John Doe");
});{code}
An invalid SQL statement is generated:
{code:sql}/* <criteria> */ select
p1_0.id,
p1_0.clazz_,
p1_0.title
from
( ) p1_0
join
`demo_application$author_participation` p2_0
on p1_0.id=p2_0.id_book
join
`demo_application$author` a1_0
on a1_0.id=p2_0.id_author
where
a1_0.name=?{code}
Traceback:
{noformat}org.hibernate.exception.SQLGrammarException: JDBC exception executing SQL [/* <criteria> */ select p1_0.id,p1_0.clazz_,p1_0.title from ( ) p1_0 join `demo_application$author_participation` p2_0 on p1_0.id=p2_0.id_book join `demo_application$author` a1_0 on a1_0.id=p2_0.id_author where a1_0.name=?]
at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:64) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:56) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:109) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:95) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.hibernate.sql.results.jdbc.internal.DeferredResultSetAccess.executeQuery(DeferredResultSetAccess.java:253) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.hibernate.sql.results.jdbc.internal.DeferredResultSetAccess.getResultSet(DeferredResultSetAccess.java:146) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.hibernate.sql.results.jdbc.internal.JdbcValuesResultSetImpl.advanceNext(JdbcValuesResultSetImpl.java:205) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.hibernate.sql.results.jdbc.internal.JdbcValuesResultSetImpl.processNext(JdbcValuesResultSetImpl.java:85) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.hibernate.sql.results.jdbc.internal.AbstractJdbcValues.next(AbstractJdbcValues.java:29) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.hibernate.sql.results.internal.RowProcessingStateStandardImpl.next(RowProcessingStateStandardImpl.java:88) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.hibernate.sql.results.spi.ListResultsConsumer.consume(ListResultsConsumer.java:177) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.hibernate.sql.results.spi.ListResultsConsumer.consume(ListResultsConsumer.java:33) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.hibernate.sql.exec.internal.JdbcSelectExecutorStandardImpl.doExecuteQuery(JdbcSelectExecutorStandardImpl.java:443) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.hibernate.sql.exec.internal.JdbcSelectExecutorStandardImpl.executeQuery(JdbcSelectExecutorStandardImpl.java:166) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.hibernate.sql.exec.internal.JdbcSelectExecutorStandardImpl.list(JdbcSelectExecutorStandardImpl.java:91) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.hibernate.sql.exec.spi.JdbcSelectExecutor.list(JdbcSelectExecutor.java:31) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.hibernate.query.sqm.internal.ConcreteSqmSelectQueryPlan.lambda$new$0(ConcreteSqmSelectQueryPlan.java:113) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.hibernate.query.sqm.internal.ConcreteSqmSelectQueryPlan.withCacheableSqmInterpretation(ConcreteSqmSelectQueryPlan.java:335) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.hibernate.query.sqm.internal.ConcreteSqmSelectQueryPlan.performList(ConcreteSqmSelectQueryPlan.java:276) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.hibernate.query.sqm.internal.QuerySqmImpl.doList(QuerySqmImpl.java:571) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.hibernate.query.spi.AbstractSelectionQuery.list(AbstractSelectionQuery.java:363) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.hibernate.query.sqm.internal.QuerySqmImpl.list(QuerySqmImpl.java:1073) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.hibernate.query.Query.getResultList(Query.java:94) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.findAll(SimpleJpaRepository.java:463) ~[spring-data-jpa-3.0.1.jar:3.0.1]{noformat}
( https://hibernate.atlassian.net/browse/HHH-16123#add-comment?atlOrigin=ey... ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-16123#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#100214- sha1:9509471 )
1 year, 8 months
[JIRA] (HHH-16123) Invalid generated MySQL query when accesing join from a treated path
by Roberts Z (JIRA)
Roberts Z ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=63bc113... ) *created* an issue
Hibernate ORM ( https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiZWQ4ZmJhYmI5... ) / Bug ( https://hibernate.atlassian.net/browse/HHH-16123?atlOrigin=eyJpIjoiZWQ4Zm... ) HHH-16123 ( https://hibernate.atlassian.net/browse/HHH-16123?atlOrigin=eyJpIjoiZWQ4Zm... ) Invalid generated MySQL query when accesing join from a treated path ( https://hibernate.atlassian.net/browse/HHH-16123?atlOrigin=eyJpIjoiZWQ4Zm... )
Issue Type: Bug Affects Versions: 6.2.0.CR2, 6.1.6 Assignee: Unassigned Components: query-criteria, query-sql Created: 31/Jan/2023 07:49 AM Environment: openjdk 17.0.5 2022-10-18;
Kali GNU/Linux 2022.4;
8.0.31 MySQL Community Server - GPL;
Priority: Major Reporter: Roberts Z ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=63bc113... )
When having the following model structure: (see the attachment for a minimal Spring project as a full reproducer)
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Publication extends BaseEntity {
@Column(name = "title" )
private String title;
}
@Entity
public abstract class Book extends Publication {
@OneToMany(mappedBy = "book" , cascade = CascadeType.REMOVE)
private List<AuthorParticipation> participations = new ArrayList<>();
}
@Entity @Table public class LongBook extends Book {}
@Entity @Table public class ShortBook extends Book {}
@Entity
@Table
public class Author extends BaseEntity {
@Column(name = "name" )
private String name;
}
@Entity
@Table
public class AuthorParticipation extends BaseEntity {
@ManyToOne
@JoinColumn(name = "id_author" , nullable = false )
private Author author;
@ManyToOne
@JoinColumn(name = "id_book" , nullable = false )
private Book book;
}
And when querying entities by the parent class (in this example: Publication ) with the Criteria API and treating the root path to another abstract class, when accessing a join, for example:
publicationRepository.findAll(
(root, query, criteriaBuilder) -> {
Root<Book> = criteriaBuilder.treat(root, Book.class);
Join<Book, AuthorParticipation> joinAuthors = asBook.join( "participations" );
return criteriaBuilder.equal(
joinAuthors
.get( "author" )
.get( "name" ),
"John Doe" );
});
An invalid SQL is generated:
/* <criteria> */ select
p1_0.id,
p1_0.clazz_,
p1_0.title
from
( ) p1_0
join
`demo_application$author_participation` p2_0
on p1_0.id=p2_0.id_book
join
`demo_application$author` a1_0
on a1_0.id=p2_0.id_author
where
a1_0. name =?
Traceback:
org.hibernate.exception.SQLGrammarException: JDBC exception executing SQL [/* <criteria> */ select p1_0.id,p1_0.clazz_,p1_0.title from ( ) p1_0 join `demo_application$author_participation` p2_0 on p1_0.id=p2_0.id_book join `demo_application$author` a1_0 on a1_0.id=p2_0.id_author where a1_0.name=?]
at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:64) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:56) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:109) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:95) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.hibernate.sql.results.jdbc.internal.DeferredResultSetAccess.executeQuery(DeferredResultSetAccess.java:253) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.hibernate.sql.results.jdbc.internal.DeferredResultSetAccess.getResultSet(DeferredResultSetAccess.java:146) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.hibernate.sql.results.jdbc.internal.JdbcValuesResultSetImpl.advanceNext(JdbcValuesResultSetImpl.java:205) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.hibernate.sql.results.jdbc.internal.JdbcValuesResultSetImpl.processNext(JdbcValuesResultSetImpl.java:85) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.hibernate.sql.results.jdbc.internal.AbstractJdbcValues.next(AbstractJdbcValues.java:29) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.hibernate.sql.results.internal.RowProcessingStateStandardImpl.next(RowProcessingStateStandardImpl.java:88) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.hibernate.sql.results.spi.ListResultsConsumer.consume(ListResultsConsumer.java:177) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.hibernate.sql.results.spi.ListResultsConsumer.consume(ListResultsConsumer.java:33) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.hibernate.sql.exec.internal.JdbcSelectExecutorStandardImpl.doExecuteQuery(JdbcSelectExecutorStandardImpl.java:443) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.hibernate.sql.exec.internal.JdbcSelectExecutorStandardImpl.executeQuery(JdbcSelectExecutorStandardImpl.java:166) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.hibernate.sql.exec.internal.JdbcSelectExecutorStandardImpl.list(JdbcSelectExecutorStandardImpl.java:91) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.hibernate.sql.exec.spi.JdbcSelectExecutor.list(JdbcSelectExecutor.java:31) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.hibernate.query.sqm.internal.ConcreteSqmSelectQueryPlan.lambda$new$0(ConcreteSqmSelectQueryPlan.java:113) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.hibernate.query.sqm.internal.ConcreteSqmSelectQueryPlan.withCacheableSqmInterpretation(ConcreteSqmSelectQueryPlan.java:335) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.hibernate.query.sqm.internal.ConcreteSqmSelectQueryPlan.performList(ConcreteSqmSelectQueryPlan.java:276) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.hibernate.query.sqm.internal.QuerySqmImpl.doList(QuerySqmImpl.java:571) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.hibernate.query.spi.AbstractSelectionQuery.list(AbstractSelectionQuery.java:363) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.hibernate.query.sqm.internal.QuerySqmImpl.list(QuerySqmImpl.java:1073) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.hibernate.query.Query.getResultList(Query.java:94) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.findAll(SimpleJpaRepository.java:463) ~[spring-data-jpa-3.0.1.jar:3.0.1]
( https://hibernate.atlassian.net/browse/HHH-16123#add-comment?atlOrigin=ey... ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-16123#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#100214- sha1:9509471 )
1 year, 8 months