2
我有以下問題:@Embeddable和@EmbeddedId只包含複雜類別
我的一個實體必須使用複雜的類作爲ID。 (爲了解決這個問題,我使用@EmbeddedId)
複雜類有一個其他複雜類的組合主鍵[我得到以下錯誤:引起:org.hibernate.AnnotationException:model.IElementKey沒有持久性id屬性:model.impl.Element.id]。
問題是如何解決這個問題,而無需添加一個非複雜類型的ID類。
編輯:我要用戶只JPA [javax.persistence。*]
EDIT2:小代碼示例(getter/setter方法留出爲簡單起見)
@Embeddable
public class EntityKey implements IEntityKey, Serializable {
private static final long serialVersionUID = 1L;
@ManyToOne(targetEntity = Entity1.class, optional = false)
private IEntity1 entity1 = null;
@ManyToOne(targetEntity = Entity2.class, optional = false)
private IEntity2 entity2 = null;
}
@Entity
public class MixEntity implements IMixEntity {
@EmbeddedId
private IEntityKey id = null;
}
@Entity
public class Entity1 implements IEntity1 {
@Id
private Long id = null;
@OneToMany(targetEntity = MixEntity.class, mappedBy = "id.entity1")
private List<IMixEntity> mixEntities = new ArrayList<IMixEntity>();
}
@Entity
public class Entity2 implements IEntity2 {
@Id
private Long id = null;
@OneToMany(targetEntity = MixEntity.class, mappedBy = "id.entity2")
private List<IMixEntity> mixEntities = new ArrayList<IMixEntity>();
}
爲什麼你讓你的生活困難。爲什麼不按照最佳實踐並使用單列技術自動生成的ID? – 2013-04-04 21:27:16
我希望我能,但不幸的是,由於許多原因,這不是一個選項。 – Hemeroc 2013-04-04 22:15:49