Roberts Z (
https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=63bc113...
) *updated* an issue
Hibernate ORM (
https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiNjE0ZjI2ZTQ4...
) / Bug (
https://hibernate.atlassian.net/browse/HHH-16004?atlOrigin=eyJpIjoiNjE0Zj...
) HHH-16004 (
https://hibernate.atlassian.net/browse/HHH-16004?atlOrigin=eyJpIjoiNjE0Zj...
) Downcasting with CriteriaBuilder.treat causes ClassCastException (
https://hibernate.atlassian.net/browse/HHH-16004?atlOrigin=eyJpIjoiNjE0Zj...
)
Change By: Roberts Z (
https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=63bc113...
)
When building the a query with Criteria API, downcasting to access a class specific
attribute, leads to ClassCastException for the internal Hibernate classes. Please see the
attachment for a minimal Spring project to reproduce this issue.
{code:java}@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@Getter
@Setter
public abstract class Term extends BaseEntity {
@Column(nullable = false)
private String title;
@OneToMany(mappedBy = "term")
private Collection<Linkage> linkagesToPublications = new ArrayList<>();
}
////////
@Table
@Entity(name = "LocalTermTitle")
@Getter
@Setter
@NoArgsConstructor
public class LocalTerm extends Term {
@ManyToOne(cascade = CascadeType.MERGE)
@JoinColumn(name = "IdLanguage", nullable = false)
private Language language;
}
////////
@Table
@Entity(name = "PublicationLinkageToTerm")
@Getter
@Setter
@NoArgsConstructor
public class Linkage extends BaseEntity {
@ManyToOne
@JoinColumn(name = "IdTermTitleBase", nullable = false)
private Term term;
}
////////
public static Specification<Linkage> byTermLanguage(final Language language) {
return (root, query, cb) -> {
final var asLocalTerm = cb.treat(root.get("term"), LocalTerm.class);
return cb.equal(asLocalTerm.get("language"), language);
};
}
{code}
_Caused by: java.lang.ClassCastException: class
org.hibernate.metamodel.model.domain.internal.EntitySqmPathSource cannot be cast to class
org.hibernate.metamodel.model.domain.EntityDomainType
(org.hibernate.metamodel.model.domain.internal.EntitySqmPathSource and
org.hibernate.metamodel.model.domain.EntityDomainType are in unnamed module of loader
'app')_
_at
org.hibernate.query.sqm.tree.domain.SqmTreatedSimplePath.<init>(SqmTreatedSimplePath.java:51)
~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]_
_at
org.hibernate.query.sqm.tree.domain.SqmEntityValuedSimplePath.treatAs(SqmEntityValuedSimplePath.java:78)
~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]_
_at
org.hibernate.query.sqm.tree.domain.SqmEntityValuedSimplePath.treatAs(SqmEntityValuedSimplePath.java:73)
~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]_
_at
org.hibernate.query.sqm.tree.domain.SqmEntityValuedSimplePath.treatAs(SqmEntityValuedSimplePath.java:21)
~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]_
_at
org.hibernate.query.sqm.internal.SqmCriteriaNodeBuilder.treat(SqmCriteriaNodeBuilder.java:378)
~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]_
_at
org.hibernate.query.sqm.internal.SqmCriteriaNodeBuilder.treat(SqmCriteriaNodeBuilder.java:153)
~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]_
_at
com.example.demo.repo.LinkageRepo$Specifications.lambda$byTermLanguage$67bfe078$1(LinkageRepo.java:19)
~[classes/:na]_
_at
org.springframework.data.jpa.repository.support.SimpleJpaRepository.applySpecificationToCriteria(SimpleJpaRepository.java:807)
~[spring-data-jpa-3.0.0.jar:3.0.0]_
_at
org.springframework.data.jpa.repository.support.SimpleJpaRepository.getQuery(SimpleJpaRepository.java:741)
~[spring-data-jpa-3.0.0.jar:3.0.0]_
_at
org.springframework.data.jpa.repository.support.SimpleJpaRepository.getQuery(SimpleJpaRepository.java:726)
~[spring-data-jpa-3.0.0.jar:3.0.0]_
_at
org.springframework.data.jpa.repository.support.SimpleJpaRepository.findAll(SimpleJpaRepository.java:463)
~[spring-data-jpa-3.0.0.jar:3.0.0]_
_at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
~[na:na]_
_at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
~[na:na]_
_at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
~[na:na]_
_at java.base/java.lang.reflect.Method.invoke(Unknown Source) ~[na:na]_
_at
org.springframework.data.repository.core.support.RepositoryMethodInvoker$RepositoryFragmentMethodInvoker.lambda$new$0(RepositoryMethodInvoker.java:288)
~[spring-data-commons-3.0.0.jar:3.0.0]_
_at
org.springframework.data.repository.core.support.RepositoryMethodInvoker.doInvoke(RepositoryMethodInvoker.java:136)
~[spring-data-commons-3.0.0.jar:3.0.0]_
_at
org.springframework.data.repository.core.support.RepositoryMethodInvoker.invoke(RepositoryMethodInvoker.java:120)
~[spring-data-commons-3.0.0.jar:3.0.0]_
_at
org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:516)
~[spring-data-commons-3.0.0.jar:3.0.0]_
_…_
----
(
https://hibernate.atlassian.net/browse/HHH-16004#add-comment?atlOrigin=ey...
) Add Comment (
https://hibernate.atlassian.net/browse/HHH-16004#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#100213- sha1:1fa7b87 )