2016-07-21 28 views
0

私は、このコードは私の所望の出力はによって引き起こさ:org.hibernate.QueryException:プロパティを解決できませんでした:last_nameの

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

は、第1のコードと第2のコードは、同じように振る舞うべきではないでしょうか。エイリアスだけが導入されました。正しいのですか?

+0

あなたは 'Employee'コードを投稿できますか? – Saravana

+0

あなたの 'Employee'クラスに' last_name'がありません。あなたのHQLでは 'lastName'でなければなりません。 –

答えて

1
  1. HQLでは、エンティティのプロパティによって実際の列名にアクセスします。あなたの場合、プロパティ名はlastName、列名はlast_nameです。したがって、HQLを使用している場合は、プロパティを使用する必要があります。NativeQueryを実装する場合は、列名を使用できます。

  2. あなたはプロパティとしてnameを持っており、これが最後のにもプロパティとしてlastNameを持ってEmployeeエンティティのSELECT句を行っています。だからあなたのクエリは怒鳴るように書かれている必要があります

    String hql = "FROM Employee E ORDER BY E.name.lastName"; 
    Query query = session.createQuery(hql); 
    employeeList = query.list(); 
    
+0

良い答えです!私は答えに追加したい - Criteria APIを使用する。保存されたクエリ言語。 Hibernateはあなたなしでクエリを構築します。 return createCriteria(Employee.class) .addOrder(Order.asc( "name"))。リスト() – flybox

+0

lastNameはフィールドであるため実際にはE.Name.lastNameです。アイデアのおかげで、 'Employee'のコンポーネント' Name' – Dee

関連する問題