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]_
_…_
---- |
|