您有很多错误。如果您正在使用,
@EmbeddedId则您的ID类需要是可嵌入的。
@Embeddablepublic class InteractionId { @Column(name="name") String name; Long drugId; //type should be same as for ID field on Medicine //equals and hashpre etc.}您还需要一个从
DrugInteraction到
Medicine注释的关系
MapsId:
@Entity@Table(name = "drugInteractions")public class DrugInteraction { @EmbeddedId private InteractionId interactionId; @MapsId("drugId")//value corresponds to property in the ID class @ManyToOne @JoinColumn(name = "drug_id") private Medicine medicine;}要保存新实例:
DrugInteraction di = new DrugInteraction();Medicine medicine = //an existing medicinedi.setName("Some Name");di.setMedicine(medicine);//save或者 ,也可以使用
IDClass而不是
EmbeddedId:
//not an embeddablepublic class InteractionId { String name; Long drugId; //type should be same as for ID field on Medicine //equals and hashpre etc.}并更改映射:
@Entity@Table(name = "drugInteractions")@IdClass(InteractionId.class) //specify the ID classpublic class DrugInteraction { @Id private String name; @Id @ManyToOne @JoinColumn(name = "drug_id") private Medicine medicine;}


