2016-09-30 45 views
-1

兩個表像這樣我有兩個表像empRegistration和empTransaction和執行這樣的查詢

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 

,並得到完美的輸出放,但我需要在這個查詢條件中休眠 因此,如果任何有解決方案,請給我。

+0

也許這有助於https://wiki.eclipse.org/EclipseLink/Examples/JPA/2.0/Criteria –

回答

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表

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

我希望這個幫助誰想要做那樣的事情。

相關問題