2014-12-23 62 views
0

我有以下表....如何將SQL查詢更改爲HQL或Criteria?

@Entity 
@Table 
public class Emp_PersonalDetails { 
@Id 
@GeneratedValue(strategy=GenerationType.IDENTITY) 
@Column(name="EMPID") 
private int id; 
private String firstName; 
private String middleName; 
private String lastName; 
private String driversLicenseNo; 
private String passportNo; 
@Column(columnDefinition="DATE") 
private Date pportLicenseExp; 
private String gender;; 
private String nationality; 
private String maritalStatus; 
@Column(columnDefinition="DATE") 
private Date dob; 
@Column(columnDefinition="mediumblob") 
byte[] image; 
@OneToOne(mappedBy = "details", cascade = CascadeType.ALL) 
private Emp_JobDetails jobDetails; 
@OneToOne(mappedBy = "details", cascade = CascadeType.ALL) 
private Emp_Reporting reportingDetails; 

\ contrutor +干將&制定者

和另一...

@Entity 
@Table 
public class Emp_JobDetails { 

@Id 
@GeneratedValue(strategy = GenerationType.IDENTITY) 
@Column(name = "EJDID") 
private int id; 
private String jobTitle; 
private String department; 
private String status; 
private String category; 
private Date startDate; 
private Date endDate; 
private String location; 
private Date joinedDate; 
@Column(length=1000) 
private String otherDetails; 
@OneToOne 
private Emp_PersonalDetails details; 

\ contrutor +干將&制定者 最後。 ..

@Entity 
@Table 
public class Emp_Reporting { 

@Id 
@GeneratedValue(strategy = GenerationType.IDENTITY) 
@Column(name = "RDID") 
private int id; 
private String manager; 
private String reportingMethod; 
private Date prDate; 
private Date nextPrDate; 
private String level; 
@Column(length=1000) 
private String comments; 
@OneToOne 

// @PrimaryKeyJoinColumn private Emp_PersonalDetails details;

這裏是SQL查詢工作正常,但只是太長的囉嗦。我如何將此更改爲基於HQL或Criteria的查詢?由於

SELECT EMPP.empid, EMPP.firstName, EMPP.middleName, EMPP.lastName, EMPJ.jobTitle, 
     EMPJ.status, EMPJ.department, EMPR.manager 
FROM Emp_PersonalDetails EMPP 
JOIN Emp_JobDetails EMPJ 
ON  EMPP.EMPID = EMPJ.EJDID 
JOIN Emp_Reporting EMPR 
ON  EMPP.EMPID = EMPR.RDID 
WHERE EMPP.firstName LIKE :name 
OR  EMPP.empid LIKE:id 

回答

0

我會做一個選擇從Emp_Reporting表:)

SELECT EMPP.empid, [ommited for formatting ...] 
FROM Emp_Reporting EMPR 
     inner join EMPR.details EMPP 
     inner join EMPP.jobDetails EMPJ 
WHERE EMPP.firstName LIKE :name OR EMPP.id= :id 

ProjectionList projectionList = Projections.projectionList(); 
projectionList.add(Projections.property("EMPP.id")); 
projectionList.add(Projections.property("EMPP.firstName")); 
projectionList.add(Projections.property("...")); 
Criteria criteria = createCriteria(Emp_Reporting.class,"EMPR") 
     .createAlias("details","EMPP") 
     .createAlias("EMPP.jobDetails","EMPJ") 
     .setProjection(projectionList) 
     .add(Restrictions.eq("EMPP.firstName", value)) 
     .add(Restrictions.eq("EMPP.id", value)); 
+0

我並沒有工作。 org.hibernate.hql.internal.ast.QuerySyntaxException:無效路徑:'null.details' – charliebounce

+0

您使用的是什麼休眠版本? –

+0

版本hibernate-core-4.3.6.Final – charliebounce