如何使用注释处理 Hibernate 和 Spring 中的连接查询? [英] How to handle join query in Hibernate and Spring with annotations?

查看:25
本文介绍了如何使用注释处理 Hibernate 和 Spring 中的连接查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Spring 和 Hibernate 和 MySQL 开发应用程序.我是 Hibernate 的新手并且做了基本的任务......

I am developing application using Spring and Hibernate with MySQL. I am new to Hibernate and did basic tasks...

现在我需要在选择查询中应用连接以使用注释从多个表中获取数据.我已经搜索过了,但我仍然没有任何想法...

Now I need to apply joins in select query to get data from multiple table using annotations. I have searched for it but still I didn't get any idea...

这里是我的数据库表和 bean 类:

Table 1: 'employee_info' ( id, empid, empname, doj and jobtitle )

Table 2: 'employee_login' ( username, password, status and empid )

我的 bean 类是:

And my bean classes are:

EmployeeInfoForm.java

@Entity()
@Table(name = "employee_info")
public class EmployeeInfoForm {

@Id
@GeneratedValue
@Column(name = "id", unique = true, nullable = true)
private int id;

@Column(name = "empId")
private int empId;

@Column(name = "empname")
private String empName;

@Column(name = "doj")
private Date empDoj;

@Column(name = "jobtitle")
private String empJobTitle;

public int getEmpId() {
    return empId;
}

public void setEmpId(int empId) {
    this.empId = empId;
}

public void setEmpDoj(Date empDoj) {
    this.empDoj = empDoj;
}

public String getEmpName() {
    return empName;
}

public void setEmpName(String empName) {
    this.empName = empName;
}

public Date getEmpDoj() {
    return empDoj;
}

public void setEmp_Doj(Date empDoj) {
    this.empDoj = empDoj;
}

public String getEmpJobTitle() {
    return empJobTitle;
}

public void setEmpJobTitle(String empJobTitle) {
    this.empJobTitle = empJobTitle;
}


}

EmployeeLoginForm.java

@Entity()
@Table(name = "employee_login")
public class EmployeeLoginForm {

@Id
@Column(name = "username")
private String empUserName;

@Column(name = "password")
private String empPassword;

@Column(name = "status")
private String empStatus;

@Column(name = "empid")
private int empId;

public String getEmpUserName() {
    return empUserName;
}

public int getEmpId() {
    return empId;
}

public void setEmpId(int empId) {
    this.empId = empId;
}

public void setEmpUserName(String empUserName) {
    this.empUserName = empUserName;
}

public String getEmpPassword() {
    return empPassword;
}

public void setEmpPassword(String empPassword) {
    this.empPassword = empPassword;
}

public String getEmpStatus() {
    return empStatus;
}

public void setEmpStatus(String empStatus) {
    this.empStatus = empStatus;
}

}

要求:

我想从 employee_infoemployee_login 表中选择字段 empid、empname、jobtitle 和字段 status两个表上的 empid 匹配...

I want to select fields empid, empname, jobtitle from employee_info and field status from employee_login table when the empid matches on both table...

请帮我完成我的工作...

Please help me to complete my work...

感谢任何建议和指导...

Any suggestions and guidance are appreciated...

推荐答案

当您谈论使用 select 语句执行连接时,您正在考虑数据库/纯 SQL 术语.Hibernate 的强大(和危险)在于它把它从你身上抽象出来,让你用对象的方式思考.您需要的是 2 个对象之间的关系,然后让 Hibernate 处理这种关系.

You are thinking in database / pure SQL terms when you talk about performing joins with select statements. The power (and danger) of Hibernate is that it abstracts this away from you and lets you think in Object terms. What you need is a relationship between the 2 objects and then let Hibernate handle this relationship.

我建议您花一些时间阅读本文:

I recommend you spend some time reading this:

http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/associations.html

更好地了解 Hibernate 如何提供帮助.

to get a better understanding of how Hibernate can help.

这篇关于如何使用注释处理 Hibernate 和 Spring 中的连接查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆