休眠:带有@EmbeddedId

时间:2019-04-05 01:01:43

标签: hibernate hibernate-mapping

我正在尝试使用@OneToMany单向关联来关联2个pojo。 这是我的Pojos:

@Entity
@Table(name="game_template")
class SingleGameTemplate {
   @Id
   @Column(name = "id")
   private long m_id;

   @OneToMany
   @OneToMany(fetch = FetchType.EAGER)
   @JoinColumns({ //
      @JoinColumn(name = "game_template_id"), //
      @JoinColumn(name = "type")//
   })
   @MapKey(name = "m_id.m_type")
   private Map<ScoreFeatureType, ScoreFeature> m_features;

   ...
}

@Entity
@Table(name = "score_feature")
class Feature {
  @EmbeddedId
  private FeatureId m_id;

  ...
}

@Embeddable
class FeatureId {
   @Column(name = "game_template_id")
   private m_templateId

   @Column(name = "type")
   private FeatureType m_type;

   ...
}

我得到的异常:

org.hibernate.AnnotationException: A Foreign key refering com.shareddata.entity.game.SingleGameTemplate from com.shareddata.entity.game.ScoreFeature has the wrong number of column. should be 1
    at org.hibernate.cfg.annotations.TableBinder.bindFk(TableBinder.java:657)
    at org.hibernate.cfg.annotations.CollectionBinder.bindCollectionSecondPass(CollectionBinder.java:1708)
    at org.hibernate.cfg.annotations.CollectionBinder.bindOneToManySecondPass(CollectionBinder.java:939)
    at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:854)
    at org.hibernate.cfg.annotations.MapBinder$1.secondPass(MapBinder.java:97)
    at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:54)
    at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1684)
    at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1652)
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:286)
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
    at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:473)
    at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:84)number of column. should be 1

请注意,我的表包含2个字段作为主键,例如:

id   template_id
 1        'A'
 1        'B'
 1        'C'
 2        'A'
 2        'B'
 2        'C'

我在做什么错? 谢谢!

0 个答案:

没有答案