How to handle connection request in Hibernate and Spring with annotations?

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

Now I need to apply joins in select query to retrieve data from multiple tables using annotations. I was looking for him, but still I had no idea ...

Here are my database tables and bean classes:

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

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

And my bean classes:

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;
}

}

Requirements:

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

Please help me complete my work ...

Any suggestions and recommendations appreciated ...

+5
3

/ SQL, select. ( ) Hibernate , . , Hibernate .

:

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

, Hibernate .

+5

EmployeeInfoForm EmployeeLoginForm, . , ? , . , . :

 public class Employee{

      @OneToMany(cascade = CascadeType.ALL, mappedBy = "employee")
      private Set<EmployeeLoginForm> loginForms = new HashSet<EmployeeLoginForm>();

      ...
 }

EmployeeLoginForm:

 @ManyToOne
 Employee employee;

, :

 emploee = (id, etc ...)
 employeelogin = (id, employee, ....)

, , Employee .

 Set<EmployeeLoginForm> logins = e.getLoginForms(); //where e is an employee object.

,

 select o from EmployeeLoginForm o join o.employee

.

+4

:

public List extractEmployeeAttributes() {
    log.debug("extractEmployeeAttributes");
    try {
                    Session session = sessionFactory.getCurrentSession();
                    session.beginTransaction();
           Criteria c1 = session.createCriteria(employee_info.class,emp_info);
           Criteria c2 = session.createCriteria(employee_login.class,emp_log);
           c1.setProjection(Projections.projectionList()
                                .add(Projections.property("empid"))
                                .add(Projections.property("empname"))
                                .add(Projections.property("jobtitle"))
                                .add(Projections.property("employee_info "))
                                .add(Restrictions.and(Property.eqName(emp_info.empId,emp_log.empId))
          return c1.list();

    } catch (RuntimeException re) {
        log.error("extractEmployeeAttributes failed", re);
        throw re;

    }

}
+2

All Articles