Hibernate:映射@OneToMany / @ManyToOne问题(不使用实体主键)

发布于 2024-12-04 15:32:13 字数 988 浏览 1 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别靠近我心 2024-12-11 15:32:13

Java API 有一些如何使用 ManyToOne 注释的示例:
http://java.sun.com/javaee/6 /docs/api/javax/persistence/ManyToOne.html

如下所示:

客户类:

@OneToMany(mappedBy="customer", cascade=CascadeType.ALL)
private List<Contract> contracts;

合同类:

@ManyToOne
@JoinColumn(name="customer_id", nullable=false)
private Customer customer;  

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:

@OneToMany(mappedBy="customer", cascade=CascadeType.ALL)
private List<Contract> contracts;

Contract class:

@ManyToOne
@JoinColumn(name="customer_id", nullable=false)
private Customer customer;  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文