[JIRA] (BVAL-784) Hibernate @TenantId annotation doesn't work with @Embedded class
by Sachin S Rao (JIRA)
Sachin S Rao ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=63d163b... ) *created* an issue
Bean Validation ( https://hibernate.atlassian.net/browse/BVAL?atlOrigin=eyJpIjoiMWI1MzQ4MTJ... ) / Bug ( https://hibernate.atlassian.net/browse/BVAL-784?atlOrigin=eyJpIjoiMWI1MzQ... ) BVAL-784 ( https://hibernate.atlassian.net/browse/BVAL-784?atlOrigin=eyJpIjoiMWI1MzQ... ) Hibernate @TenantId annotation doesn't work with @Embedded class ( https://hibernate.atlassian.net/browse/BVAL-784?atlOrigin=eyJpIjoiMWI1MzQ... )
Issue Type: Bug Assignee: Gavin King ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%... ) Created: 25/Jan/2023 09:21 AM Priority: Critical Reporter: Sachin S Rao ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=63d163b... )
I'm using Spring boot v3.0.1 and hibernate v6.1.6.Final. I'm trying the @TenantId annotation in my entity class. This annotation works fine if I declare the annotation in the entity class directly. But if I create a @Embeddable class and then declare the @TenantId annotation inside it, then the application doesn't start.
Below is the sample code:
BaseEntity.Java
import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;
import jakarta.persistence.MappedSuperclass;
import org.hibernate.annotations.TenantId;
@MappedSuperclass
@Embeddable
public class BaseEntity {
@TenantId
@Column(name = "tenant_id" , nullable = false )
private String tenantId;
public BaseEntity() {
}
public BaseEntity( String tenantId) {
this.tenantId = tenantId;
}
public String getTenantId() {
return tenantId;
}
public void setTenantId( String tenantId) {
this.tenantId = tenantId;
}
}
Person.java
import jakarta.persistence.*;
import java.util.Objects;
@Entity
public class Person {
@Id @GeneratedValue private Long id;
@Embedded
private BaseEntity baseEntity;
private String name;
public Long getId() {
return id;
}
public void setId( Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName( String name) {
this.name = name;
}
public BaseEntity getBaseEntity() {
return baseEntity;
}
public void setBaseEntity(BaseEntity baseEntity) {
this.baseEntity = baseEntity;
}
@Override
public String toString() {
return "Person{" + "id=" + id + ", name= '" + name + ' \ '' + ' }';
}
@Override
public boolean equals( Object o) {
if ( this == o) return true ;
if (o == null || getClass() != o.getClass()) return false ;
Person person = (Person) o;
return Objects.equals(id, person.id) && Objects.equals(baseEntity, person.baseEntity) && Objects.equals(name, person.name);
}
@Override
public int hashCode() {
return Objects.hash(id, baseEntity, name);
}
}
Error message:
*My title*
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class] : [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.cfg.NotYetImplementedException: Still need to wire in composite in-memory value generation at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1751) ~ [spring-beans-6.0.3.jar:6.0.3] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:599) ~ [spring-beans-6.0.3.jar:6.0.3] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:521) ~ [spring-beans-6.0.3.jar:6.0.3] at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326) ~ [spring-beans-6.0.3.jar:6.0.3] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~ [spring-beans-6.0.3.jar:6.0.3] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324) ~ [spring-beans-6.0.3.jar:6.0.3] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~ [spring-beans-6.0.3.jar:6.0.3] at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1130) ~ [spring-context-6.0.3.jar:6.0.3] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:905) ~ [spring-context-6.0.3.jar:6.0.3] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:584) ~ [spring-context-6.0.3.jar:6.0.3] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:730) ~ [spring-boot-3.0.1.jar:3.0.1] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:432) ~ [spring-boot-3.0.1.jar:3.0.1] at org.springframework.boot.SpringApplication.run(SpringApplication.java:308) ~ [spring-boot-3.0.1.jar:3.0.1] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1302) ~ [spring-boot-3.0.1.jar:3.0.1] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1291) ~ [spring-boot-3.0.1.jar:3.0.1] at example.springdata.jpa.hibernatemultitenant.partition.Application.main(Application.java:9) ~ [classes/:na] Caused by: jakarta.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.cfg.NotYetImplementedException: Still need to wire in composite in-memory value generation at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:421) ~ [spring-orm-6.0.3.jar:6.0.3] at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:396) ~ [spring-orm-6.0.3.jar:6.0.3] at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:352) ~ [spring-orm-6.0.3.jar:6.0.3] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1797) ~ [spring-beans-6.0.3.jar:6.0.3] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1747) ~ [spring-beans-6.0.3.jar:6.0.3]... 15 common frames omitted Caused by: org.hibernate.cfg.NotYetImplementedException: Still need to wire in composite in-memory value generation at org.hibernate.tuple.entity.EntityMetamodel$CompositeGenerationStrategyPairBuilder.buildPair(EntityMetamodel.java:601) ~ [hibernate-core-6.1.6.Final.jar:6.1.6.Final] at org.hibernate.tuple.entity.EntityMetamodel.buildGenerationStrategyPair(EntityMetamodel.java:460) ~ [hibernate-core-6.1.6.Final.jar:6.1.6.Final] at org.hibernate.tuple.entity.EntityMetamodel.(EntityMetamodel.java:296) ~ [hibernate-core-6.1.6.Final.jar:6.1.6.Final] at org.hibernate.persister.entity.AbstractEntityPersister.(AbstractEntityPersister.java:728) ~ [hibernate-core-6.1.6.Final.jar:6.1.6.Final] at org.hibernate.persister.entity.SingleTableEntityPersister.(SingleTableEntityPersister.java:152) ~ [hibernate-core-6.1.6.Final.jar:6.1.6.Final] at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~ [na:na] at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77) ~ [na:na] at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~ [na:na] at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499) ~ [na:na] at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480) ~ [na:na] at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:92) ~ [hibernate-core-6.1.6.Final.jar:6.1.6.Final] at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:75) ~ [hibernate-core-6.1.6.Final.jar:6.1.6.Final] at org.hibernate.metamodel.model.domain.internal.MappingMetamodelImpl.processBootEntities(MappingMetamodelImpl.java:278) ~ [hibernate-core-6.1.6.Final.jar:6.1.6.Final] at org.hibernate.metamodel.model.domain.internal.MappingMetamodelImpl.finishInitialization(MappingMetamodelImpl.java:211) ~ [hibernate-core-6.1.6.Final.jar:6.1.6.Final] at org.hibernate.metamodel.internal.RuntimeMetamodelsImpl.finishInitialization(RuntimeMetamodelsImpl.java:60) ~ [hibernate-core-6.1.6.Final.jar:6.1.6.Final] at org.hibernate.internal.SessionFactoryImpl.(SessionFactoryImpl.java:311) ~ [hibernate-core-6.1.6.Final.jar:6.1.6.Final] at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:415) ~ [hibernate-core-6.1.6.Final.jar:6.1.6.Final] at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1425) ~ [hibernate-core-6.1.6.Final.jar:6.1.6.Final] at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:66) ~ [spring-orm-6.0.3.jar:6.0.3] at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:376) ~ [spring-orm-6.0.3.jar:6.0.3] at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:409) ~ [spring-orm-6.0.3.jar:6.0.3]... 19 common frames omitted
( https://hibernate.atlassian.net/browse/BVAL-784#add-comment?atlOrigin=eyJ... ) Add Comment ( https://hibernate.atlassian.net/browse/BVAL-784#add-comment?atlOrigin=eyJ... )
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:3d33cb1 )
1 year, 7 months
[JIRA] (HHH-15951) Hibernate 6 Error : Already registered a copy: SqmBasicValuedSimplePath(fullyqualifiedclass.name)
by Francisco Sousa (JIRA)
Francisco Sousa ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=5e949f3... ) *commented* on HHH-15951 ( https://hibernate.atlassian.net/browse/HHH-15951?atlOrigin=eyJpIjoiMzdjOG... )
Re: Hibernate 6 Error : Already registered a copy: SqmBasicValuedSimplePath(fullyqualifiedclass.name) ( https://hibernate.atlassian.net/browse/HHH-15951?atlOrigin=eyJpIjoiMzdjOG... )
I had a similar situation.
After some days debugging the Hibernate’s code, I found the problem.
In my case, I was providing the same instance of Path for more then one instance of Predicate. In other words I had two instances of Predicate , but both had the same internal instance of Path.
It was happening because I am using Specification with JPARepository , and I was doing a paginated query, so JPA do two queries, one to get the result list, and a count query to get the total rows.
Thus, the method toPredicate of my spec was being called two times, once for list and once for count, but in my spec implementation I was reusing "root.get(myprop)".
Something like this:
public class MySpec<MyEntity> implements Specification<MyEntity> {
private Path<Y> path;
...
@Override
public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
if (path == null ) {
path = root.get( "myprod" );
}
return builder.equal(path, "myvalue" );
}
...
}
On each call to toPredicate , a new predicate is created by builder.equal , but in my case, the Path was the same and it was causing the trouble.
Anyway, I hope this can help someone.
( https://hibernate.atlassian.net/browse/HHH-15951#add-comment?atlOrigin=ey... ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-15951#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:3d33cb1 )
1 year, 7 months