我有這樣的代碼工作有我需要的輸出造成的:org.hibernate.QueryException:無法解析屬性:姓氏
String hql = "FROM Employee ORDER BY last_name";
Query query = session.createQuery(hql);
employeeList = query.list();
我想這個代碼,但它無法
String hql = "FROM Employee E ORDER BY E.last_name";
Query query = session.createQuery(hql);
employeeList = query.list();
這裏異常
Exception in thread "main" java.lang.IllegalArgumentException: org.hibernate.QueryException: could not resolve property: last_name of: com.hibernate.exercise6.model.Employee [FROM com.hibernate.exercise6.model.Employee E ORDER BY E.last_name]
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:131)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:155)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:162)
at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:631)
at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:101)
at com.hibernate.exercise6.service.EmployeeFunctions.listEmployees(EmployeeFunctions.java:208)
at com.hibernate.exercise6.app.GUIMain.displayListMenu(GUIMain.java:329)
at com.hibernate.exercise6.app.GUIMain.manageRecords(GUIMain.java:140)
at com.hibernate.exercise6.app.GUIMain.main(GUIMain.java:62)
Caused by: org.hibernate.QueryException: could not resolve property: last_name of: com.hibernate.exercise6.model.Employee [FROM com.hibernate.exercise6.model.Employee E ORDER BY E.last_name]
at org.hibernate.QueryException.generateQueryException(QueryException.java:120)
at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:103)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:218)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:142)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:115)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:152)
at org.hibernate.internal.AbstractSharedSessionContract.getQueryPlan(AbstractSharedSessionContract.java:521)
at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:623)
... 5 more
Caused by: org.hibernate.QueryException: could not resolve property: last_name of: com.hibernate.exercise6.model.Employee
at org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:62)
at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:56)
at org.hibernate.persister.entity.AbstractEntityPersister.toType(AbstractEntityPersister.java:1801)
at org.hibernate.hql.internal.ast.tree.FromElementType.getPropertyType(FromElementType.java:393)
at org.hibernate.hql.internal.ast.tree.FromElement.getPropertyType(FromElement.java:505)
at org.hibernate.hql.internal.ast.tree.DotNode.getDataType(DotNode.java:660)
at org.hibernate.hql.internal.ast.tree.DotNode.prepareLhs(DotNode.java:264)
at org.hibernate.hql.internal.ast.tree.DotNode.resolve(DotNode.java:204)
at org.hibernate.hql.internal.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:109)
at org.hibernate.hql.internal.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:104)
at org.hibernate.hql.internal.ast.HqlSqlWalker.resolve(HqlSqlWalker.java:1013)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.expr(HqlSqlBaseWalker.java:1286)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.orderExpr(HqlSqlBaseWalker.java:1860)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.orderExprs(HqlSqlBaseWalker.java:1657)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.orderClause(HqlSqlBaseWalker.java:1630)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:652)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:311)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:259)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:262)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:190)
... 11 more
Employee.java
package com.hibernate.exercise6.model;
import java.util.Set;
import java.util.Comparator;
public class Employee extends Id{
private int uniqueId;
private Name nameInfo;
private Address addressInfo;
private OtherInfo otherInfo;
private Set contact;
private Set role;
public Employee() {}
public Employee(Name nameInfo, Address addressInfo, OtherInfo otherInfo){
this.nameInfo = nameInfo;
this.addressInfo = addressInfo;
this.otherInfo = otherInfo;
}
public void setId(int uniqueId){
this.uniqueId = uniqueId;
}
public int getId(){
return uniqueId;
}
public void setName(Name nameInfo){
this.nameInfo = nameInfo;
}
public Name getName(){
return nameInfo;
}
public void setAddress(Address addressInfo){
this.addressInfo = addressInfo;
}
public Address getAddress(){
return addressInfo;
}
public void setOtherInfo(OtherInfo otherInfo){
this.otherInfo = otherInfo;
}
public OtherInfo getOtherInfo(){
return otherInfo;
}
public void setContacts(Set contact){
this.contact = contact;
}
public Set getContacts(){
return contact;
}
public void setRole(Set role){
this.role = role;
}
public Set getRole(){
return role;
}
public static Comparator<Employee> employeeGwaComparator = new Comparator<Employee>(){
public int compare(Employee emp1, Employee emp2){
String emp1Gwa = String.valueOf(emp1.otherInfo.getGwa());
String emp2Gwa = String.valueOf(emp2.otherInfo.getGwa());
return emp1Gwa.compareTo(emp2Gwa);
}
};
}
Name.java
package com.hibernate.exercise6.model;
public class Name{
private String firstName,
lastName,
middleName,
suffix,
title;
public Name() {}
public Name(String title, String firstName, String middleName, String lastName, String suffix){
this.title = title;
this.firstName = firstName;
this.middleName = middleName;
this.lastName = lastName;
this.suffix = suffix;
}
public void setTitle(String title){
this.title = title;
}
public String getTitle(){
return title;
}
public void setFirstName(String firstName){
this.firstName = firstName;
}
public String getFirstName(){
return firstName;
}
public void setMiddleName(String middleName){
this.middleName = middleName;
}
public String getMiddleName(){
return middleName;
}
public void setLastName(String lastName){
this.lastName = lastName;
}
public String getLastName(){
return lastName;
}
public void setSuffix(String suffix){
this.suffix = suffix;
}
public String getSuffix(){
return suffix;
}
}
應該不是第一碼和第二碼行爲相同的方式?只有一個別名被引入,我是否正確?
你可以張貼'Employee'代碼? – Saravana
您的「Employee」類中沒有'last_name'。它應該是你的HQL中的'lastName'。 –