我有两个表像empRegistration和empTransaction,并执行像这样的查询

时间:2016-09-30 05:41:46

标签: java mysql hibernate criteria

两张表赞这个

  Empregistration

  emp_id;

  Name;

  EmpTransaction

  emp_id

  trndate

我正在编写像这样的SQL查询

SELECT Max(trndate),empregistration.fname 
from emptransaction,empregistration 
where empregistration.emp_id=17 
AND empregistration.emp_id = emptransaction.emp_id 
group by empregistration.fname

并获得Perfect Out,但我在Hibernate中的条件查询中需要 所以如果有任何解决方案,请给我。

1 个答案:

答案 0 :(得分:0)

你可以Hibernate Criteria来做。首先,你必须正确地完成所有映射。

EmpRegistration.class

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "EMPREGISTRATION")
public class EmpRegistration implements Serializable {

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

    @Column(name = "NAME")
    private String name;

    public EmpRegistration() {
    }

    public EmpRegistration(int empId, String name) {
        this.empId = empId;
        this.name = name;
    }

    public int getEmpId() {
        return empId;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "EmpRegistration{" + "empId=" + empId + ", name=" + name + '}';
    }

}

EmpTransaction.class

import java.io.Serializable;
import java.sql.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import org.hibernate.annotations.Type;

@Entity
@Table(name = "EMPTRANSACTION")
public class EmpTransaction implements Serializable {

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

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "EMP_REG")
    private EmpRegistration empRegistration;

    @Column(name = "TRNDATE")
    @Type(type = "date")
    @Temporal(javax.persistence.TemporalType.DATE)
    private Date trnDate;

    public EmpTransaction() {
    }

    public EmpTransaction(int empId, EmpRegistration empRegistration, Date trnDate) {
        this.empId = empId;
        this.empRegistration = empRegistration;
        this.trnDate = trnDate;
    }

    public int getEmpId() {
        return empId;
    }

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

    public EmpRegistration getEmpRegistration() {
        return empRegistration;
    }

    public void setEmpRegistration(EmpRegistration empRegistration) {
        this.empRegistration = empRegistration;
    }

    public Date getTrnDate() {
        return trnDate;
    }

    public void setTrnDate(Date trnDate) {
        this.trnDate = trnDate;
    }

    @Override
    public String toString() {
        return "EmpTransaction{" + "empId=" + empId + ", empRegistration=" + empRegistration + ", trnDate=" + trnDate + '}';
    }

}

每个表都必须遵循您的数据库结构。

EmpRegistration表

enter image description here

EmpTransaction Table

enter image description here

当然,您必须正确映射hibernate.cfg.xml文件。在使用标准创建选择之前,很明显,我们想要进行投影。现在我们必须创建一个用于获取数据的类。

ResultEmpReg.class

import java.util.Date;

public class ResultEmpReg {

    private String name;
    private Date trnDate;

    public ResultEmpReg() {
    }

    public ResultEmpReg(String name, Date trnDate) {
        this.name = name;
        this.trnDate = trnDate;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getTrnDate() {
        return trnDate;
    }

    public void setTrnDate(Date trnDate) {
        this.trnDate = trnDate;
    }

    @Override
    public String toString() {
        return "ResultEmpReg{" + "name=" + name + ", trnDate=" + trnDate + '}';
    }

}

现在是时候用Hiberate Criteria编写你想要的代码了。

public List<ResultEmpReg> getList() {
    Session session = HibernateUtil.getInstance().openSession(); // It will change with your session creation
    Criteria criteria = session.createCriteria(EmpTransaction.class, "empt");
    criteria.add(Restrictions.eq("empt.empId", 17));
    criteria.createAlias("empt.empRegistration", "empReg");  // It is important to create alias for projection
    ProjectionList list = Projections.projectionList();
    list.add(Projections.groupProperty("empReg.name"), "name"); // Alias is maps with ResultEmpReg fields.
    list.add(Projections.property("empt.trnDate"), "trnDate");
    criteria.setProjection(list);
    criteria.setResultTransformer(new AliasToBeanResultTransformer(ResultEmpReg.class)); // This method transform the result to class. By Default it will object. If projection count is one, result list type will be same with this projection.
    return criteria.list();
}

我希望这种帮助能够做出类似的事情。