我目前正在尝试创建一对Hibernate带注释的类,以便从遗留系统中的一对表中加载(只读)。遗留系统使用一致(如果有点过时)方法来键控表。我试图映射的表格如下:
Customer CustomerAddress
-------------------------- ----------------------------
customerNumber:string (pk) customerNumber:string (pk_1)
name:string sequenceNumber:int (pk_2)
street:string
postalCode:string
我通过创建这样的CustomerAddress类来解决这个问题:
@Entity
@Table(name="CustomerAddress")
@IdClass(CustomerAddressKey.class)
public class CustomerAddress {
@Id
@AttributeOverrides({
@AttributeOverride(name = "customerNumber", column = @Column(name="customerNumber")),
@AttributeOverride(name = "sequenceNumber", column = @Column(name="sequenceNumber"))
})
private String customerNumber;
private int sequenceNumber;
private String name;
private String postalCode;
...
}
CustomerAddressKey类是一个带有两个关键字段的简单Serializable对象。然后将Customer对象定义为:
@Entity
@Table(name = "Customer")
public class Customer {
private String customerNumber;
private List<CustomerAddress> addresses = new ArrayList<CustomerAddress>();
private String name;
...
}
所以,我的问题是:如何在Customer表上表达OneToMany关系?
答案 0 :(得分:0)
我可能会回答我自己的问题。将以下内容添加到客户:
@OneToMany(mappedBy="customer")
@JoinColumn(name="customerNumber")
List<CustomerAddress> addresses = new ArrayList<CustomerAddress>();
以下是CustomerAddress:
@ManyToOne
@JoinColumn(name="customerNumber")
protected Customer customer;
这比我最初想的更容易解决。典型
答案 1 :(得分:0)
我假设您已阅读过Bauer / King Hibernate一书,该书非常难以解释如何正确实现复合主键。不要让自己被一本坏书所迷惑:复合主键是不遗留系统的属性......