2017-08-06 47 views
2

我遇到了一個問題 - 我是hibernate的新手,需要投一個表,由executing session.createSQLQuery(query).list()命令返回。 我嘗試添加addEntity(Result.class)命令,但程序會拋出異常:將默認的hibernate createSQLQuery轉換爲自制類的列表

Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.MappingException: Unknown entity: com.hib.entities.Result 

在mysql中我的成績表看起來像這樣:


| bookName | authorSurname | authorFirstName | authorPatronymic | releaseDate | amountBooks| 

我試圖創建實體類結果和註釋它,但似乎我失敗了。 我的實體類看起來是這樣的:

package com.hib.entities; 

import javax.persistence.Entity; 
import javax.persistence.Id; 
import javax.persistence.Table; 

@Entity 
@Table 
public class Result { 

    String bookName; 
    String authorSurname; 
    String authorFirstName; 
    String authorPatronymic; 
    Integer releaseDate; 
    Integer amountBooks; 

    public Result(String bookName, String authorSurname, String authorFirstName, String authorPatronymic, Integer releaseDate, Integer 
      amountBooks) { 
     this.bookName = bookName; 
     this.authorSurname = authorSurname; 
     this.authorFirstName = authorFirstName; 
     this.authorPatronymic = authorPatronymic; 
     this.releaseDate = releaseDate; 
     this.amountBooks = amountBooks; 
    } 

    public void setBookName(String bookName) { 
     this.bookName = bookName; 
    } 

    public void setAuthorSurname(String authorSurname) { 
     this.authorSurname = authorSurname; 
    } 

    public void setAuthorFirstName(String authorFirstName) { 
     this.authorFirstName = authorFirstName; 
    } 

    public void setAuthorPatronymic(String authorPatronymic) { 
     this.authorPatronymic = authorPatronymic; 
    } 

    public void setReleaseDate(Integer releaseDate) { 
     this.releaseDate = releaseDate; 
    } 

    public void setAmountBooks(Integer amountBooks) { 
     this.amountBooks = amountBooks; 
    } 

    public String getBookName() { 
     return bookName; 
    } 

    public String getAuthorSurname() { 
     return authorSurname; 
    } 

    public String getAuthorFirstName() { 
     return authorFirstName; 
    } 

    public String getAuthorPatronymic() { 
     return authorPatronymic; 
    } 

    public Integer getReleaseDate() { 
     return releaseDate; 
    } 

    public Integer getAmountBooks() { 
     return amountBooks; 
    } 

    @Override 
    public String toString() { 
     return "Result{" + 
       "bookName='" + bookName + '\'' + 
       ", authorSurname='" + authorSurname + '\'' + 
       ", authorFirstName='" + authorFirstName + '\'' + 
       ", authorPatronymic='" + authorPatronymic + '\'' + 
       ", releaseDate=" + releaseDate + 
       ", amountBooks=" + amountBooks + 
       '}'; 
    } 
} 

能否請你幫我解決這個問題?

回答

0

你,如果你使用session.createQuery(query)您的查詢將被寫入HQL(這非常類似於SQL)session.createQuery(query)session.createSQLQuery(query) 區分,你將能夠做到像

Query q = session.createQuery(query) List<Result> myResultList = q.list();

,如果你想使用session.createSQLQuery(query)你必須使用這個

List<Result> myResultList = session.createSQLQuery(query).setResultTransformer(
    Transformers.aliasToBean(Result.class)).list(); 

我個人推薦您使用session.createQuery(query)所以你可以在這個link

受益Hibernate映射的advatages你會發現關於休眠方面HQL和SQL之間的區別更多的解釋。