我没有完全按照您的要求进行操作,但这可能会使您朝着正确的方向入手。我认为应该可以。此页面非常详细地解释这些注解。
@Entitypublic class Foo { @Id @GeneratedValue(strategy=GenerationType.AUTO) public Long getId(){ } ... @oneToMany(mappedBy="foo") public Collection<Bar> getBars() { }...}@Entity@IdClass(BarPk.class)public class Bar implements Serializable { @ManyToOne @JoinColumn(name="foo") @Id public Foo getFoo() { return foo; } @Column(name="key", length=255) @Id public String getKey(){ }}@Embeddablepublic class BarPk implements Serializable { public Foo getFoo() { return foo; } public void setFoo(Foo foo) { } public String getKey(){ ... } public void setKey(String key){ ... } //you must include equals() and hashpre() }


