我让这段代码正常工作
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;
}
}
不应该是第一个代码和第二个代码的行为方式相同吗?只引入了一个别名,我是否正确?
答案 0 :(得分:2)
在HQL中,您可以通过实体的属性访问实际列名称。在您的情况下,属性名称为lastName
,列名称为last_name
。因此,如果您使用HQL,则必须使用属性,如果要实现NativeQuery
,则可以使用列名。
您正在SELECT
实体上执行Employee
条款,其中name
属性为lastName
,最后一个属性为String hql = "FROM Employee E ORDER BY E.name.lastName";
Query query = session.createQuery(hql);
employeeList = query.list();
。所以你的查询必须写成如下:
typedef struct libvlc_media_track_t
{
/* Codec fourcc */
uint32_t i_codec;
uint32_t i_original_fourcc;
int i_id;
libvlc_track_type_t i_type;
/* Codec specific */
int i_profile;
int i_level;
union {
libvlc_audio_track_t *audio;
libvlc_video_track_t *video;
libvlc_subtitle_track_t *subtitle;
};
unsigned int i_bitrate;
char *psz_language;
char *psz_description;
} libvlc_media_track_t;