我有以下问题:
我的一个实体必须使用一个复杂的类作为 ID。(为了解决这个问题,我使用@EmbeddedId)
复杂类具有其他 2 个复杂类的组合主键 [我收到以下错误:原因:org.hibernate.AnnotationException:model.IElementKey 没有持久 id 属性:model.impl.Element.id]。
问题是如何在不向 ID 类添加非复杂类型的情况下解决这个问题。
编辑:我必须只使用 JPA [javax.persistence.*]
Edit2:小代码示例(为简单起见,省略了获取器/设置器)
@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>();
}