Search code examples
jpaeclipselinkspring-datamulti-tenantspring-data-jpa

EclipseLink MultiTenant and Spring Data JPA - @IdClass annotation required - Why?


I'm developing a multi-tenant (multi-schema) application using Spring-Data-JPA and EclipseLink. When not using multi-tenant capabilities everything is ok, JPA entity works as a charme and obviously works with only one schema. When I try to activate the multi-tenant adding the folloqing annotation to the entity :

@Multitenant(value=MultitenantType.TABLE_PER_TENANT)
@TenantTableDiscriminator(type=TenantTableDiscriminatorType.SCHEMA, contextProperty="eclipselink-tenant.id")

and I restart the application, i get the following exception :

Caused by: java.lang.IllegalArgumentException: No @IdClass attributes exist on the IdentifiableType [EntityTypeImpl@15818739:CrsMomiJob [ javaType: class com.gpdati.momi.model.core.CrsMomiJob descriptor: RelationalDescriptor(com.gpdati.momi.model.core.CrsMomiJob --> [DatabaseTable(CRS_MOMI_JOB)]), mappings: 7]].  There still may be one or more @Id or an @EmbeddedId on type.
at org.eclipse.persistence.internal.jpa.metamodel.IdentifiableTypeImpl.getIdClassAttributes(IdentifiableTypeImpl.java:169)
at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation$IdMetadata.<init>(JpaMetamodelEntityInformation.java:170)
at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.<init>(JpaMetamodelEntityInformation.java:71)
at org.springframework.data.jpa.repository.support.JpaEntityInformationSupport.getMetadata(JpaEntityInformationSupport.java:65)
at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getEntityInformation(JpaRepositoryFactory.java:146)
at com.gpdati.momi.jpa.MultiTenantJpaRepositoryFactory.getTargetRepository(MultiTenantJpaRepositoryFactory.java:30)
at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:67)
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:136)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:153)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:43)
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:142)
... 79 more

It seems like the @Id annotation on the Id field is no more read from Spring-Data that look for a @IdClass annotation (I thought @IdClass annotation is required when using a composite primary key, that's not my case)

Any clue? Thanks!

Here the full entity code :

@Entity
@Table(name="CRS_MOMI_JOB")
@Multitenant(value=MultitenantType.TABLE_PER_TENANT)
@TenantTableDiscriminator(type=TenantTableDiscriminatorType.SCHEMA, contextProperty="eclipselink-tenant.id")
public class CrsMomiJob implements Serializable {
private static final long serialVersionUID = -432489894772L;

private String abilitata;

@Column(name="HOT_CODICE")
private String hotCodice;

@Column(name="INT_CODICE")
private String intCodice;

private Long intervallo;

private String note;

private String parametri;

@Id
private BigDecimal id;      

public CrsMomiJob() {
}

... all getters and setters ...

}

Solution

  • Seems to be a bug in the EclipseLink meta model code in hasSingleIdAttribute(), this is returning true (as the id is composite for multitenants) but this should be hidden, so should be returning false.

    Please log a bug.