使用Hibernate注释加入两个表

时间:2018-01-08 21:07:01

标签: java hibernate

我在理解如何使用Hibernate注释将表连接在一起时遇到了一些麻烦。我希望得到一个简单的工作示例,以便更好地理解......

所以我有两个映射到表的对象:

@Entity
@Table(name = "LOANS")
public class Loan {

    @Id
    @Column(name="LOAN_ID")
    private Integer id;

    @Column(name="REQUESTAMOUNT")
    private String requestAmount;

    @Column(name="STATUS_ID")
    private Integer statusId

}

@Entity
@Table(name = "LOAN_STATUS")
public class LoanStatus {

    @Id
    @Column(name="STATUS_ID")
    private Integer statusId;

    @Column(name="STATUS_DESCRIPTION")
    private String statusDescription;

}

基本上我试图找到的是给定"贷款的所有信息" (包括LoanStatus表中的statusDescription),其中Loan.statusId = LoanStatus.statusId。我怎么能做到这一点?谢谢!

1 个答案:

答案 0 :(得分:1)

您需要根据从LoanStatus到Loan实体的表结构(@OneToMany/ @ManyToOne/@ManyToMany)以及从Loan到LoanStatus的相应(@OneToMany/ @ManyToOne/@ManyToMany)映射在pojo中进行映射:

像:

@OneToMany(mappedBy = "STATUS_ID")
private Set<User> Loan;
-----------------------------------
@ManyToOne
@JoinColumn(name = "STATUS_ID")
private LoanStatus loanStatus ;

您可以查看Question