[JIRA] (HHH-16070) Exception when find by association id that is a generic @EmbeddedId with @MappedSuperclass
by Francisco Sousa (JIRA)
Francisco Sousa ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=5e949f3... ) *updated* an issue
Hibernate ORM ( https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiNGQ5OWY3ZTky... ) / Bug ( https://hibernate.atlassian.net/browse/HHH-16070?atlOrigin=eyJpIjoiNGQ5OW... ) HHH-16070 ( https://hibernate.atlassian.net/browse/HHH-16070?atlOrigin=eyJpIjoiNGQ5OW... ) Exception when find by association id that is a generic @EmbeddedId with @MappedSuperclass ( https://hibernate.atlassian.net/browse/HHH-16070?atlOrigin=eyJpIjoiNGQ5OW... )
Change By: Francisco Sousa ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=5e949f3... )
I have the following abstracts base classes:
{code:java}@MappedSuperclass
public abstract class DomainEntityId implements Serializable {
private final Long value;
protected DomainEntityId() {
this.value = RandomGenerator.getDefault().nextLong();
}
public Long getValue() {
return value;
}
}{code}
{code:java}@MappedSuperclass
public abstract class DomainEntityModel<ID extends DomainEntityId> {
@EmbeddedId
@AttributeOverride(name = "value", column = @Column(name = "id"))
private final ID id;
protected DomainEntityModel(ID id) {
this.id = id;
}
@NonNull
public ID getId() {
return id;
}
}{code}
and the following concrete entities:
{code:java}@Embeddable
public class CustomerId extends DomainEntityId {}{code}
{noformat}@Entity
public class Customer extends DomainEntityModel<CustomerId> {
private Integer code;
private String name;
public Customer() {
super(new CustomerId());
}
// getters, setters, etc
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}{ noformat}
{noformat}@Embeddable
public class InvoiceId extends DomainEntityId {}{noformat}
{noformat}@Entity
public class Invoice extends DomainEntityModel<InvoiceId> {
private Integer number;
@ManyToOne
private Customer customer;
public Invoice() {
super(new InvoiceId());
}
public Integer getNumber() { // getters, setters, etc
return number;
}
public void setNumber(Integer number) {
this.number = number;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
}{ noformat}
When I try to find invoices by customer id, I get the following exception:
{noformat}java.lang.IllegalArgumentException: Argument [org.hibernate.model.CustomerId@311a0b3e] of type [org.hibernate.model.CustomerId] did not match parameter type [org.hibernate.model.InvoiceId (n/a)]
at org.hibernate.query.spi.QueryParameterBindingValidator.validate(QueryParameterBindingValidator.java:84)
at org.hibernate.query.spi.QueryParameterBindingValidator.validate(QueryParameterBindingValidator.java:31)
at org.hibernate.query.internal.QueryParameterBindingImpl.validate(QueryParameterBindingImpl.java:339)
at org.hibernate.query.internal.QueryParameterBindingImpl.setBindValue(QueryParameterBindingImpl.java:129)
at org.hibernate.query.spi.AbstractCommonQueryContract.setParameter(AbstractCommonQueryContract.java:860)
at org.hibernate.query.spi.AbstractSelectionQuery.setParameter(AbstractSelectionQuery.java:708)
at org.hibernate.query.sqm.internal.QuerySqmImpl.setParameter(QuerySqmImpl.java:1263)
at org.hibernate.query.sqm.internal.QuerySqmImpl.setParameter(QuerySqmImpl.java:131)
at org.hibernate.bugs.JPAUnitTestCase.findByAssociationIdTest(JPAUnitTestCase.java:47)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54){noformat}
I’ve debug as I could and AFAICS, as both concrete entities share the same superclass, there is only one [MappedSuperclass|https://docs.jboss.org/hibernate/orm/6.1/javadocs/org/hi...] instance and the {{DeclaredIdentifierProperty}} is being replaced with the id of the last entity. After, this id is used to validate the query parameter value and they are incompatibles.
There is a similar problem [https://hibernate.atlassian.net/browse/HHH-11955|https://hibernate.atlass...], although I’m not sure that is exactly the same.
Here is a test case demonstrating this issue in Hibernate 6 [https://github.com/franciscosousabr/test-case-template-hibernate-orm6|htt... here is a test case demonstrating the same code working in Hibernate 5 [https://github.com/franciscosousabr/test-case-template-hibernate-orm5|htt...]
( https://hibernate.atlassian.net/browse/HHH-16070#add-comment?atlOrigin=ey... ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-16070#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:fa7bc5f )
1 year, 8 months
[JIRA] (HHH-16070) Exception when find by association id that is a generic @EmbeddedId with @MappedSuperclass
by Francisco Sousa (JIRA)
Francisco Sousa ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=5e949f3... ) *updated* an issue
Hibernate ORM ( https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiNjZkOGM2MjU5... ) / Bug ( https://hibernate.atlassian.net/browse/HHH-16070?atlOrigin=eyJpIjoiNjZkOG... ) HHH-16070 ( https://hibernate.atlassian.net/browse/HHH-16070?atlOrigin=eyJpIjoiNjZkOG... ) Exception when find by association id that is a generic @EmbeddedId with @MappedSuperclass ( https://hibernate.atlassian.net/browse/HHH-16070?atlOrigin=eyJpIjoiNjZkOG... )
Change By: Francisco Sousa ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=5e949f3... )
I have the following abstracts base classes:
{code:java}@MappedSuperclass
public abstract class DomainEntityId implements Serializable {
private final Long value;
protected DomainEntityId() {
this.value = RandomGenerator.getDefault().nextLong();
}
public Long getValue() {
return value;
}
}{code}
{code:java}@MappedSuperclass
public abstract class DomainEntityModel<ID extends DomainEntityId> {
@EmbeddedId
@AttributeOverride(name = "value", column = @Column(name = "id"))
private final ID id;
protected DomainEntityModel(ID id) {
this.id = id;
}
@NonNull
public ID getId() {
return id;
}
}{code}
and the following concrete entities:
{code:java}@Embeddable
public class CustomerId extends DomainEntityId {}{code}
{noformat}@Entity
public class Customer extends DomainEntityModel<CustomerId> {
private Integer code;
private String name;
public Customer() {
super(new CustomerId());
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}{noformat}
{noformat}@Embeddable
public class InvoiceId extends DomainEntityId {}{noformat}
{noformat}@Entity
public class Invoice extends DomainEntityModel<InvoiceId> {
private Integer number;
@ManyToOne
private Customer customer;
public Invoice() {
super(new InvoiceId());
}
public Integer getNumber() {
return number;
}
public void setNumber(Integer number) {
this.number = number;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
}{noformat}
When I try to find invoices by customer id, I get the following exception:
{noformat}java.lang.IllegalArgumentException: Argument [ br org. com hibernate. leverinfo.mappedsuperclassissue. model.CustomerId@ 3c2c9b59 311a0b3e ] of type [ br org. com hibernate. leverinfo.mappedsuperclassissue. model.CustomerId] did not match parameter type [ br org. com hibernate. leverinfo.mappedsuperclassissue. model.InvoiceId (n/a)]
at org.hibernate.query.spi.QueryParameterBindingValidator.validate(QueryParameterBindingValidator.java:84) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.hibernate.query.spi.QueryParameterBindingValidator.validate(QueryParameterBindingValidator.java:31) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.hibernate.query.internal.QueryParameterBindingImpl.validate(QueryParameterBindingImpl.java:339) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.hibernate.query.internal.QueryParameterBindingImpl.setBindValue(QueryParameterBindingImpl.java:129) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.hibernate.query.spi.AbstractCommonQueryContract.setParameter(AbstractCommonQueryContract.java: 1066 860 ) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.hibernate.query.spi.AbstractSelectionQuery.setParameter(AbstractSelectionQuery.java: 774 708 ) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.hibernate.query.sqm.internal.QuerySqmImpl.setParameter(QuerySqmImpl.java: 1329 1263 ) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org.hibernate.query.sqm.internal.QuerySqmImpl.setParameter(QuerySqmImpl.java:131) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
at org. springframework hibernate. data bugs. jpa JPAUnitTestCase. repository.query.QueryParameterSetter$BindableQuery.setParameter findByAssociationIdTest ( QueryParameterSetter JPAUnitTestCase.java: 314 47 ) ~[spring-data-jpa-3
at java. 0 base/jdk. 0 internal. jar:3 reflect. 0 NativeMethodAccessorImpl. 0] invoke0(Native Method)
at org java. springframework base/jdk. data internal. jpa reflect. repository NativeMethodAccessorImpl. query.QueryParameterSetter$NamedOrIndexedQueryParameterSetter.lambda$setParameter$3 invoke ( QueryParameterSetter NativeMethodAccessorImpl.java: 111 62 ) ~[spring-data-jpa-3.0.0.jar:3.0.0]
at org java. springframework base/jdk. data internal. jpa reflect. repository DelegatingMethodAccessorImpl. query.QueryParameterSetter$ErrorHandling$1.execute invoke ( QueryParameterSetter DelegatingMethodAccessorImpl.java: 136 43 ) ~[spring-data-jpa-3.0.0.jar:3.0.0]
at org java. springframework base/java. data lang. jpa reflect. repository Method. query.QueryParameterSetter$NamedOrIndexedQueryParameterSetter.setParameter invoke ( QueryParameterSetter Method.java: 111 566 ) ~[spring-data-jpa-3.0.0.jar:3.0.0]
at org. springframework junit. data runners. jpa model. repository FrameworkMethod$1. query.ParameterBinder.bind runReflectiveCall ( ParameterBinder FrameworkMethod.java: 83 59 ) ~[spring-data-jpa-3.0.0.jar:3.0.0]
at org. springframework junit. data internal. jpa runners. repository model. query ReflectiveCallable. ParameterBinder.bind run ( ParameterBinder ReflectiveCallable.java: 75 12 ) ~[spring-data-jpa-3.0.0.jar:3.0.0]
at org. springframework junit. data runners. jpa model. repository FrameworkMethod. query.ParameterBinder.bindAndPrepare invokeExplosively ( ParameterBinder FrameworkMethod.java: 97 56 ) ~[spring-data-jpa-3.0.0.jar:3.0.0]
at org. springframework junit. data internal. jpa runners. repository statements. query InvokeMethod. PartTreeJpaQuery$QueryPreparer.invokeBinding evaluate ( PartTreeJpaQuery InvokeMethod.java: 312 17 ) ~[spring-data-jpa-3.0.0.jar:3.0.0]
at org. springframework junit. data internal. jpa runners. repository statements. query RunBefores. PartTreeJpaQuery$QueryPreparer.createQuery evaluate ( PartTreeJpaQuery RunBefores.java: 231 26 ) ~[spring-data-jpa-3.0.0.jar:3.0.0]
at org. springframework junit. data internal. jpa runners. repository statements. query RunAfters. PartTreeJpaQuery.doCreateQuery evaluate ( PartTreeJpaQuery RunAfters.java: 102 27 ) ~[spring-data-jpa-3.0.0.jar:3.0.0]
at org. springframework junit. data runners. jpa ParentRunner$3. repository.query.AbstractJpaQuery.createQuery evaluate ( AbstractJpaQuery ParentRunner.java: 234 306 ) ~[spring-data-jpa-3.0.0.jar:3.0.0]
at org. springframework junit. data runners. jpa.repository.query.JpaQueryExecution BlockJUnit4ClassRunner $ CollectionExecution 1. doExecute evaluate ( JpaQueryExecution BlockJUnit4ClassRunner.java: 127 100 ) ~[spring-data-jpa-3.0.0.jar:3.0.0]
at org. springframework junit. data runners. jpa ParentRunner. repository.query.JpaQueryExecution.execute runLeaf ( JpaQueryExecution ParentRunner.java: 90 366 ) ~[spring-data-jpa-3.0.0.jar:3.0.0]
at org. springframework junit. data runners. jpa BlockJUnit4ClassRunner. repository.query.AbstractJpaQuery.doExecute runChild ( AbstractJpaQuery BlockJUnit4ClassRunner.java: 148 103 ) ~[spring-data-jpa-3.0.0.jar:3.0.0]
at org. springframework junit. data runners. jpa BlockJUnit4ClassRunner. repository.query.AbstractJpaQuery.execute runChild ( AbstractJpaQuery BlockJUnit4ClassRunner.java: 136 63 ) ~[spring-data-jpa-3.0.0.jar:3.0.0]
at org. springframework junit. data runners. repository ParentRunner$4. core.support.RepositoryMethodInvoker.doInvoke run ( RepositoryMethodInvoker ParentRunner.java: 136 331 ) ~[spring-data-commons-3.0.0.jar:3.0.0]
at org. springframework junit. data runners. repository ParentRunner$1. core.support.RepositoryMethodInvoker.invoke schedule ( RepositoryMethodInvoker ParentRunner.java: 120 79 ) ~[spring-data-commons-3.0.0.jar:3.0.0]
at org. springframework junit. data runners. repository ParentRunner. core.support.QueryExecutorMethodInterceptor.doInvoke runChildren ( QueryExecutorMethodInterceptor ParentRunner.java: 164 329 ) ~[spring-data-commons-3.0.0.jar:3.0.0]
at org. springframework junit. data runners. repository ParentRunner. core.support.QueryExecutorMethodInterceptor.invoke access$100 ( QueryExecutorMethodInterceptor ParentRunner.java: 143 66 ) ~[spring-data-commons-3.0.0.jar:3.0.0]
at org. springframework junit. aop runners. framework ParentRunner$2. ReflectiveMethodInvocation.proceed evaluate ( ReflectiveMethodInvocation ParentRunner.java: 184 293 ) ~[spring-aop-6.0.3.jar:6.0.3]
at org. springframework junit. data runners. projection ParentRunner$3. DefaultMethodInvokingMethodInterceptor.invoke evaluate ( DefaultMethodInvokingMethodInterceptor ParentRunner.java: 77 306 ) ~[spring-data-commons-3.0.0.jar:3.0.0]
at org. springframework junit. aop runners. framework ParentRunner. ReflectiveMethodInvocation.proceed run ( ReflectiveMethodInvocation ParentRunner.java: 184 413 ) ~[spring-aop-6.0.3.jar:6.0.3]
at org. springframework junit. transaction runner. interceptor JUnitCore. TransactionInterceptor$1.proceedWithInvocation run ( TransactionInterceptor JUnitCore.java: 123 137 ) ~[spring-tx-6.0.3.jar:6.0.3]
at org com. springframework intellij. transaction junit4. interceptor JUnit4IdeaTestRunner. TransactionAspectSupport.invokeWithinTransaction startRunnerWithArgs ( TransactionAspectSupport JUnit4IdeaTestRunner.java: 388 69 ) ~[spring-tx-6.0.3.jar:6.0.3]
at org com. springframework intellij. transaction rt. interceptor junit. TransactionInterceptor IdeaTestRunner$Repeater$1. invoke execute ( TransactionInterceptor IdeaTestRunner.java: 119 38 ) ~[spring-tx-6.0.3.jar:6.0.3]
at org com. springframework intellij. aop rt. framework execution. ReflectiveMethodInvocation junit. proceed TestsRepeater.repeat ( ReflectiveMethodInvocation TestsRepeater.java: 184 11 ) ~[spring-aop-6.0.3.jar:6.0.3]
at org com. springframework intellij. dao rt. support junit. PersistenceExceptionTranslationInterceptor IdeaTestRunner$Repeater. invoke startRunnerWithArgs ( PersistenceExceptionTranslationInterceptor IdeaTestRunner.java: 137 35 ) ~[spring-tx-6.0.3.jar:6.0.3]
at org com. springframework intellij. aop rt. framework junit. ReflectiveMethodInvocation JUnitStarter. proceed prepareStreamsAndStart ( ReflectiveMethodInvocation JUnitStarter.java: 184 235 ) ~[spring-aop-6.0.3.jar:6.0.3]
at org com. springframework intellij. data rt. jpa junit. repository JUnitStarter. support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke main ( CrudMethodMetadataPostProcessor JUnitStarter.java: 134 54 ) ~[spring-data-jpa-3.0.0.jar:3.0.0]
... {noformat}
I’ve debug as I could and AFAICS, as both concrete entities share the same superclass, there is only one [MappedSuperclass|https://docs.jboss.org/hibernate/orm/6.1/javadocs/org/hi...] instance and the {{DeclaredIdentifierProperty}} is being replaced with the id of the last entity. After, this id is used to validate the query parameter value and they are incompatibles.
There is a similar problem [https://hibernate.atlassian.net/browse/HHH-11955|https://hibernate.atlass...], although I’m not sure that is exactly the same.
In Here is a test case demonstrating this repository issue in Hibernate 6 [https://github.com/franciscosousabr/ mappedsuperclassissue test-case-template-hibernate-orm6 |https://github.com/franciscosousabr/ mappedsuperclassissue test-case-template-hibernate-orm6 |smart-link] I have an example of this issue.
In branch {{master}} the error and here is occurring.
In branch {{hibernate-5}}, a test case demonstrating the same code works well working in Hibernate 5 [https://github. com/franciscosousabr/test-case-template-hibernate-orm5|https://github.com...]
( https://hibernate.atlassian.net/browse/HHH-16070#add-comment?atlOrigin=ey... ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-16070#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:fa7bc5f )
1 year, 8 months