Hibernate:映射@OneToMany / @ManyToOne问题(不使用实体主键)
我是 Hibernate / Spring 的新手,尝试在创建一个小型 Web 实用程序的同时映射遗留数据库,这应该可以减轻同事的一些工作。由于我已经映射了实体并可以访问底层数据,因此我在以后的道路上遇到了一些问题。长话短说,我有两个实体,客户
@Entity
public class Customer implements Serializable{
@Id
@Column(name = "RecordID")
private Integer id;
@Column(name = "CUSTOMERNAME1")
private String name;
@OneToMany
@JoinColumn(name="CUSTOMER1", referencedColumnName="CUSTOMERNAME1")
private List<Contract> contracts;
}
和合同:
@Entity
public class Contract implements Serializable {
@Id
@Column(name = "RECORDID") //RecordID
private Integer id;
@Column(name = "CONTRACTID1") //ContractID1
private String contractId;
@Column(name = "CUSTOMER1") //Customer1
private String customerName;
//@ManyToOne
//private Customer customer; // how can I write the reverse mapping?
}
从客户到合同的映射有效(一个客户可以有许多合同,我可以使用客户中的“列表合同”字段获取所有合同,但我的问题是,我怎样才能实现相反的目标 - 即获取客户,将合同映射到哪个客户?
I am new to Hibernate / Spring, trying to map legacy database while creating a little web utility, which should ease some work for the colleagues. As I had mapped the entities and have access to the underlying data, I have some issues further down the road. To make long story short, I have two entites, Customer
@Entity
public class Customer implements Serializable{
@Id
@Column(name = "RecordID")
private Integer id;
@Column(name = "CUSTOMERNAME1")
private String name;
@OneToMany
@JoinColumn(name="CUSTOMER1", referencedColumnName="CUSTOMERNAME1")
private List<Contract> contracts;
}
and Contract:
@Entity
public class Contract implements Serializable {
@Id
@Column(name = "RECORDID") //RecordID
private Integer id;
@Column(name = "CONTRACTID1") //ContractID1
private String contractId;
@Column(name = "CUSTOMER1") //Customer1
private String customerName;
//@ManyToOne
//private Customer customer; // how can I write the reverse mapping?
}
The mapping from Customer to Contracts works (one customer can have many contracts, I can get them all using List contracts field in customer, but my question is, how can I achieve the reverse - that is, get the customer, to which to contract is mapped?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Java API 有一些如何使用 ManyToOne 注释的示例:
http://java.sun.com/javaee/6 /docs/api/javax/persistence/ManyToOne.html
如下所示:
客户类:
合同类:
The Java API has some examples of how to use the ManyToOne annotation:
http://java.sun.com/javaee/6/docs/api/javax/persistence/ManyToOne.html
something like the below:
Customer class:
Contract class: