JPA / Hibernate中的复合键与继承的类

时间:2010-11-18 14:58:04

标签: hibernate jpa composite-primary-key composite-id

我在我的类结构上定义了一个复合id,如下所示。不幸的是,我总是得到一个hibernate错误,抱怨未找到“part2”:

“在实体MoreClass:part2中找不到@IdClass的属性”

有人可以帮我解决问题吗? (或者至少指出一个有用的jpa / hibernate doc?)

@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;
}

3 个答案:

答案 0 :(得分:2)

实际上碰到了same problem

如:

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

似乎有用,我认为这是一个错误。请参阅https://hibernate.atlassian.net/browse/HHH-9114

答案 1 :(得分:2)

Michael提到的HHH-9114 bug解决方法,例如:在我的情况下,添加到TwitterListedCount :(请注意,必须添加@Id @Type才能使用户类型仍然有用

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

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

顺便说一句,当与架构生成一起使用时,解决方法有nasty side-effect HHH-9350,它会生成重复的复合列:

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
);

我试图完全不使用@MappedSuperclass,但仍然会发生错误的模式生成。顺便说一下,我正在使用DefaultComponentSafeNamingStrategy,这可能是错误的所在。这可能是一个不同的错误,在Hibernate find with composite key. Invalid column name Exception

中提出

正确的解决方法是手动添加@Column(name=),这适用于模式生成:

@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;

仅供参考,Spring Data JPA 一起使用时,需要@Id <删除@TypeMappedSuperclass注释/ strong>即可。如果不删除这些,则会出现错误。它不会改变这个Hibernate bug的性质,BTW。

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 :(得分:1)

来自JPA规范:

  

主键必须在作为实体层次结构的根或在a上的实体类上定义   映射的超类,它是实体层次结构中所有实体类的(直接或间接)超类。该   必须在实体层次结构中将主键定义为一次

因此,根据JPA,您无法重新定义@Id。我不会称这是一个错误。

尽管此处给出的解决方法可能有效,但对于其他JPA框架而言,它可能无法正常工作。