假设我有一堂课,看起来像这样:
public class MyClass { @Id @Column(name = "ID") private long Id; }
我可以使用hibernate会话对此类进行获取或加载:
MyClass a = (MyClass)session.get(MyClass.class, new Long(100));
但是,假设我有一个包含多列的类作为主键:
public MyJoinClass implements Serializable { private static final long serialVersionUID = -5L; @Id @Column(name = "ID") private long id; @Id @Column(name = "EMAIL_ADDRESS_ID") private long emailAddressId; }
可以在此类中使用get或load吗?
尝试使用和@IdClass或@EmbeddedId
@IdClass
@EmbeddedId
public MyJoinClass implements Serializable { private static final long serialVersionUID = -5L; @EmbeddedId private MyJoinClassKey key; } public MyJoinClassKey implements Serializable{ @Column(name = "ID") private long id; @Column(name = "EMAIL_ADDRESS_ID") private long emailAddressId; }
然后使用
MyJoinClass a = (MyJoinClass )session.get(MyJoinClass .class, new MyJoinClassKey (1, "email"));
看一下这个问题,对此进行了广泛的解释。基本上,hibernate具有复合键的机制。