Yasin Koyuncu (
https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=70121%3...
) *created* an issue
Hibernate ORM (
https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiNzhhYWIxNGJh...
) / Bug (
https://hibernate.atlassian.net/browse/HHH-16693?atlOrigin=eyJpIjoiNzhhYW...
) HHH-16693 (
https://hibernate.atlassian.net/browse/HHH-16693?atlOrigin=eyJpIjoiNzhhYW...
) MappedSuperClass with generic collections not mapped correctly (
https://hibernate.atlassian.net/browse/HHH-16693?atlOrigin=eyJpIjoiNzhhYW...
)
Issue Type: Bug Affects Versions: 6.2.2, 6.2.3 Assignee: Unassigned Components:
hibernate-core Created: 25/May/2023 02:55 AM Priority: Blocker Reporter: Yasin Koyuncu (
https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=70121%3...
)
We have noticed a critical issue with loading of entities with MapperSuperClasses having
generic collections. The entities are not loaded correctly, where these generic collection
fields are configured with other entities extending the same MapperSuperClass. Let me
explain the situation with an example:
Let’s say we have a MappedSuperClass A with a generic collection field:
@MappedSuperclass
public abstract class Configuration<T> {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID" , nullable = false )
private Long id;
@OneToMany(mappedBy = "configuration" , cascade = ALL, orphanRemoval = true
)
private Set<T> items;
}
and subtypes:
@Entity
@Entity
@Table(name = "A_CONFIGURATION")
public class AConfiguration extends Configuration<AItem> {
}
@Entity
@Table(name = "A_ITEM")
public class AItem {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID", updatable = false, nullable = false)
private Long id;
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "CONFIGURATION_ID")
private AConfiguration configuration;
}
@Entity
@Table(name = "B_CONFIGURATION")
public class BConfiguration extends Configuration<BItem> {
}
@Entity
@Table(name = "B_ITEM")
public class BItem {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID", updatable = false, nullable = false)
private Long id;
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "CONFIGURATION_ID")
private BConfiguration configuration;
@OneToMany(mappedBy = "item", cascade = CascadeType.ALL, orphanRemoval =
true)
private Set<BItemChild> children = new LinkedHashSet<>();
}
@Entity
@Table(name = "B_ITEM_CHILD")
public class BItemChild {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private Long id;
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "CONFIGURATION_ID")
private BItem item;
}
The following jpql query throws an error:
select config from BConfiguration config
left join treat (config.items as BItem) item
left join item.children child
left join child.id
Stacktrace:
...
Caused by: org.hibernate.query.sqm.InterpretationException: Error interpreting query
[select config from BConfiguration config
left join treat(config.items as BItem) item
left join item.children child
left join child.id
]; this may indicate a semantic (user query) problem or a bug in the parser [select config
from BConfiguration config
left join treat(config.items as BItem) item
left join item.children child
left join child.id
]
at
org.hibernate.query.hql.internal.StandardHqlTranslator.translate(StandardHqlTranslator.java:97)
~[hibernate-core-6.2.3.Final.jar:6.2.3.Final]
at
org.hibernate.internal.AbstractSharedSessionContract.lambda$interpretHql$2(AbstractSharedSessionContract.java:744)
~[hibernate-core-6.2.3.Final.jar:6.2.3.Final]
at
org.hibernate.query.internal.QueryInterpretationCacheStandardImpl.createHqlInterpretation(QueryInterpretationCacheStandardImpl.java:141)
~[hibernate-core-6.2.3.Final.jar:6.2.3.Final]
at
org.hibernate.query.internal.QueryInterpretationCacheStandardImpl.resolveHqlInterpretation(QueryInterpretationCacheStandardImpl.java:128)
~[hibernate-core-6.2.3.Final.jar:6.2.3.Final]
at
org.hibernate.internal.AbstractSharedSessionContract.interpretHql(AbstractSharedSessionContract.java:741)
~[hibernate-core-6.2.3.Final.jar:6.2.3.Final]
at
org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:786)
~[hibernate-core-6.2.3.Final.jar:6.2.3.Final]
... 43 common frames omitted
Caused by: java.lang.IllegalArgumentException: org.hibernate.query.SemanticException:
Could not resolve attribute 'children' of
'com.example.demo.entities.AItem'
at org.hibernate.query.sqm.SqmPathSource.getSubPathSource(SqmPathSource.java:61)
~[hibernate-core-6.2.3.Final.jar:6.2.3.Final]
at
org.hibernate.query.hql.internal.QualifiedJoinPathConsumer.createJoin(QualifiedJoinPathConsumer.java:182)
~[hibernate-core-6.2.3.Final.jar:6.2.3.Final]
at
org.hibernate.query.hql.internal.QualifiedJoinPathConsumer$AttributeJoinDelegate.consumeIdentifier(QualifiedJoinPathConsumer.java:236)
~[hibernate-core-6.2.3.Final.jar:6.2.3.Final]
at
org.hibernate.query.hql.internal.QualifiedJoinPathConsumer.consumeIdentifier(QualifiedJoinPathConsumer.java:103)
~[hibernate-core-6.2.3.Final.jar:6.2.3.Final]
at
org.hibernate.query.hql.internal.SemanticQueryBuilder.visitSimplePath(SemanticQueryBuilder.java:5208)
~[hibernate-core-6.2.3.Final.jar:6.2.3.Final]
at
org.hibernate.query.hql.internal.SemanticQueryBuilder.visitIndexedPathAccessFragment(SemanticQueryBuilder.java:5155)
~[hibernate-core-6.2.3.Final.jar:6.2.3.Final]
at
org.hibernate.query.hql.internal.SemanticQueryBuilder.visitGeneralPathFragment(SemanticQueryBuilder.java:5124)
~[hibernate-core-6.2.3.Final.jar:6.2.3.Final]
at
org.hibernate.query.hql.internal.SemanticQueryBuilder.visitGeneralPathFragment(SemanticQueryBuilder.java:253)
~[hibernate-core-6.2.3.Final.jar:6.2.3.Final]
at
org.hibernate.grammars.hql.HqlParser$GeneralPathFragmentContext.accept(HqlParser.java:4513)
~[hibernate-core-6.2.3.Final.jar:6.2.3.Final]
at
org.hibernate.query.hql.internal.SemanticQueryBuilder.visitPath(SemanticQueryBuilder.java:5116)
~[hibernate-core-6.2.3.Final.jar:6.2.3.Final]
at
org.hibernate.query.hql.internal.SemanticQueryBuilder.visitPath(SemanticQueryBuilder.java:253)
~[hibernate-core-6.2.3.Final.jar:6.2.3.Final]
at org.hibernate.grammars.hql.HqlParser$PathContext.accept(HqlParser.java:4311)
~[hibernate-core-6.2.3.Final.jar:6.2.3.Final]
at
org.hibernate.query.hql.internal.SemanticQueryBuilder.consumeJoin(SemanticQueryBuilder.java:2193)
~[hibernate-core-6.2.3.Final.jar:6.2.3.Final]
at
org.hibernate.query.hql.internal.SemanticQueryBuilder.visitEntityWithJoins(SemanticQueryBuilder.java:1887)
~[hibernate-core-6.2.3.Final.jar:6.2.3.Final]
at
org.hibernate.query.hql.internal.SemanticQueryBuilder.visitFromClause(SemanticQueryBuilder.java:1865)
~[hibernate-core-6.2.3.Final.jar:6.2.3.Final]
at
org.hibernate.query.hql.internal.SemanticQueryBuilder.visitQuery(SemanticQueryBuilder.java:1141)
~[hibernate-core-6.2.3.Final.jar:6.2.3.Final]
at
org.hibernate.query.hql.internal.SemanticQueryBuilder.visitQuerySpecExpression(SemanticQueryBuilder.java:937)
~[hibernate-core-6.2.3.Final.jar:6.2.3.Final]
at
org.hibernate.query.hql.internal.SemanticQueryBuilder.visitQuerySpecExpression(SemanticQueryBuilder.java:253)
~[hibernate-core-6.2.3.Final.jar:6.2.3.Final]
at
org.hibernate.grammars.hql.HqlParser$QuerySpecExpressionContext.accept(HqlParser.java:1818)
~[hibernate-core-6.2.3.Final.jar:6.2.3.Final]
at
org.hibernate.query.hql.internal.SemanticQueryBuilder.visitSimpleQueryGroup(SemanticQueryBuilder.java:931)
~[hibernate-core-6.2.3.Final.jar:6.2.3.Final]
at
org.hibernate.query.hql.internal.SemanticQueryBuilder.visitSimpleQueryGroup(SemanticQueryBuilder.java:253)
~[hibernate-core-6.2.3.Final.jar:6.2.3.Final]
at
org.hibernate.grammars.hql.HqlParser$SimpleQueryGroupContext.accept(HqlParser.java:1711)
~[hibernate-core-6.2.3.Final.jar:6.2.3.Final]
at
org.hibernate.query.hql.internal.SemanticQueryBuilder.visitSelectStatement(SemanticQueryBuilder.java:418)
~[hibernate-core-6.2.3.Final.jar:6.2.3.Final]
at
org.hibernate.query.hql.internal.SemanticQueryBuilder.visitStatement(SemanticQueryBuilder.java:377)
~[hibernate-core-6.2.3.Final.jar:6.2.3.Final]
at
org.hibernate.query.hql.internal.SemanticQueryBuilder.buildSemanticModel(SemanticQueryBuilder.java:295)
~[hibernate-core-6.2.3.Final.jar:6.2.3.Final]
at
org.hibernate.query.hql.internal.StandardHqlTranslator.translate(StandardHqlTranslator.java:81)
~[hibernate-core-6.2.3.Final.jar:6.2.3.Final]
... 48 common frames omitted
Caused by: org.hibernate.query.SemanticException: Could not resolve attribute
'children' of 'com.example.demo.entities.AItem'
... 74 common frames omitted
So from the error it seems hibernate thinks ‘children' field belongs to the
'com.example.demo.entities.AItem' which doesn’t have this field.
Also I noticed that hibernate searches for a suitable type for the generic collection and
it assigns it the first subtype it encounters such as AConfiguration instead of
BConfiguration. If we had renamed Aconfiguration to CConfiguration then it doesn’t
complain anymore.
This is a blocker for us and prevents us from using generic collections in the
MappedSuperClasses.
(
https://hibernate.atlassian.net/browse/HHH-16693#add-comment?atlOrigin=ey...
) Add Comment (
https://hibernate.atlassian.net/browse/HHH-16693#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=Em...
) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100225- sha1:0428b75 )