如何处理加入Hibernate查询和春季的注解? [英] How to handle join query in Hibernate and Spring with annotations?

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

问题描述

我使用Spring和MySQL的Hibernate的开发应用。我对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类是:

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_info 并现场的状态字段中的 EMPID,empname,JOBTITLE employee_login 表时在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...

任何建议和指导AP preciated ...

Any suggestions and guidance are appreciated...

推荐答案

您正在考虑在数据库/ SQL纯条款当你谈论执行与select语句连接。休眠的力量(和危险)是从你这个抽象出来,而让你觉得在对象方面。你需要的是两个对象之间的关系,然后让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/休眠/ ORM / 3.3 /参考/ EN / HTML / associations.html

要获得更好的理解Hibernate怎样能有所帮助。

to get a better understanding of how Hibernate can help.

这篇关于如何处理加入Hibernate查询和春季的注解?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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