Component Key in JPA / Hibernate with Inherited Class

I have a compound identifier defined in my class structure as shown below. Unfortunately, I always get a sleeping error that complains about the "part2" not found:

"The @IdClass property was not found in the MoreClass: part2 object"

Can someone help me in solving the problem? (or at least point me to a useful jpa / hibernate document?)

@IdClass(ClassKey.class) @Entity public class MoreClass extends LessClass implements Serializable { @Id String part1; } @MappedSuperclass public class LessClass implements Serializable { @Id String part2; } public class ClassKey implements Serializable { String part1; String part2; } 
+6
hibernate jpa composite-primary-key
source share
3 answers

Actually came across the same problem .

how

 @Override @Id public getPart2() { return super.getPart2(); } 

It seems to work, I think this is a mistake. See https://hibernate.atlassian.net/browse/HHH-9114 .

+2
source share

The specified workaround for error HHH-9114 from Michael works , for example. in my case, adding to TwitterListedCount : (note that in order for user types to still work, both @Id and @Type must be added)

 // TODO: https://hibernate.atlassian.net/browse/HHH-9114 @Override @Id public long getTwitterUserId() { return super.getTwitterUserId(); } @Override @Id public DateTime getFetchTime() { return super.getFetchTime(); } 

By the way, the workaround has the unpleasant side effect of HHH-9350 when used with circuit generation , it generates repeating composite columns:

 CREATE TABLE buzz.twitterlistedcount ( id_fetchtime timestamp without time zone NOT NULL, id_twitteruserid bigint NOT NULL, _identifiermapper_fetchtime timestamp without time zone NOT NULL, _identifiermapper_twitteruserid bigint NOT NULL, listedcount integer NOT NULL, CONSTRAINT twitterlistedcount_pkey PRIMARY KEY (id_fetchtime, id_twitteruserid) ) WITH ( OIDS=FALSE ); 

I tried not to use @MappedSuperclass at all, but the wrong schema creation is still happening. BTW I am using DefaultComponentSafeNamingStrategy where there may be an error. This is probably another error set in Hibernate find with a composite key. Invalid column name exception

The correct workaround involves adding @Column(name=) manually, which works well with generating the schema:

 @Id @Basic() @Column(name="twitteruserid") private long twitterUserId = 0; @Id @Basic() @Column(name="fetchtime") @Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime") private DateTime fetchTime = null; 

FYI, when used with Spring Data JPA , requires the removal of @Id and @Type annotations from MappedSuperclass . If they are not deleted, errors below will appear. This does not change the nature of this Hibernate, BTW error.

 org.springframework.data.mapping.model.MappingException: Ambiguous mapping! Annotation Id configured on field twitterUserId and one of its accessor methods in class TwitterFollowerCount! at org.springframework.data.mapping.model.AnnotationBasedPersistentProperty.populateAnnotationCache(AnnotationBasedPersistentProperty.java:111) at org.springframework.data.mapping.model.AnnotationBasedPersistentProperty.<init>(AnnotationBasedPersistentProperty.java:66) at org.springframework.data.jpa.mapping.JpaPersistentPropertyImpl.<init>(JpaPersistentPropertyImpl.java:86) at org.springframework.data.jpa.mapping.JpaMetamodelMappingContext.createPersistentProperty(JpaMetamodelMappingContext.java:67) at org.springframework.data.jpa.mapping.JpaMetamodelMappingContext.createPersistentProperty(JpaMetamodelMappingContext.java:35) at org.springframework.data.mapping.context.AbstractMappingContext$PersistentPropertyCreator.createAndRegisterProperty(AbstractMappingContext.java:449) at org.springframework.data.mapping.context.AbstractMappingContext$PersistentPropertyCreator.doWith(AbstractMappingContext.java:427) at org.springframework.util.ReflectionUtils.doWithFields(ReflectionUtils.java:607) at org.springframework.data.mapping.context.AbstractMappingContext.addPersistentEntity(AbstractMappingContext.java:295) at org.springframework.data.mapping.context.AbstractMappingContext.addPersistentEntity(AbstractMappingContext.java:257) at org.springframework.data.mapping.context.AbstractMappingContext.initialize(AbstractMappingContext.java:373) at org.springframework.data.jpa.repository.config.JpaRepositoryConfigExtension$JpaMetamodelMappingContextFactoryBean.createInstance(JpaRepositoryConfigExtension.java:216) at org.springframework.data.jpa.repository.config.JpaRepositoryConfigExtension$JpaMetamodelMappingContextFactoryBean.createInstance(JpaRepositoryConfigExtension.java:169) at org.springframework.beans.factory.config.AbstractFactoryBean.afterPropertiesSet(AbstractFactoryBean.java:134) at org.springframework.data.jpa.repository.config.JpaRepositoryConfigExtension$JpaMetamodelMappingContextFactoryBean.afterPropertiesSet(JpaRepositoryConfigExtension.java:230) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1612) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1549) ... 40 more 
+2
source share

From the JPA specification:

The primary key must be defined in the entity class, which is the root of the entity hierarchy or to the associated superclass, which is the (direct or indirect) superclass of all entity classes in the entity hierarchy. the primary key must be defined exactly once in the hierarchy of entities.

Thus, according to JPA, you cannot override @Id . I would not call it a mistake.

Although the workaround given as an answer here may work, it may happen that for other JPA frameworks it does not work.

+1
source share

All Articles