您可以通過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表
EmpTransaction表
關當然,你必須做正確的映射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();
}
我希望這個幫助誰想要做那樣的事情。
也許這有助於https://wiki.eclipse.org/EclipseLink/Examples/JPA/2.0/Criteria –