Java 将默认hibernate createSQLQuery强制转换为自制类的列表
我有一个问题-我刚开始休眠,需要强制转换一个表,该表由执行session.createSQLQuery(query.list()命令返回。 我已尝试添加Java 将默认hibernate createSQLQuery强制转换为自制类的列表,java,mysql,hibernate,Java,Mysql,Hibernate,我有一个问题-我刚开始休眠,需要强制转换一个表,该表由执行session.createSQLQuery(query.list()命令返回。 我已尝试添加addEntity(Result.class)命令,但程序引发异常: Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.MappingException: Unknown entity: com.hib.entities.Result
addEntity(Result.class)
命令,但程序引发异常:
Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.MappingException: Unknown entity: com.hib.entities.Result
我在mysql中的结果表如下所示:
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 +
'}';
}
}
我曾尝试创建实体类结果并对其进行注释,但似乎失败了。 我的实体类如下所示:
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 +
'}';
}
}
您能帮我解决这个问题吗?您必须区分
会话.createQuery(查询)
和会话.createSQLQuery(查询)
如果使用session.createQuery(query)
您的查询将用HQL
(非常类似于SQL)编写,您将能够这样做
Query q=session.createQuery(查询)
List myResultList=q.List()代码>
但是
如果要使用session.createSQLQuery(query)
必须使用此
List<Result> myResultList = session.createSQLQuery(query).setResultTransformer(
Transformers.aliasToBean(Result.class)).list();
List myResultList=session.createSQLQuery(query).setResultTransformer(
Transformers.aliasToBean(Result.class)).list();
就我个人而言,我建议您使用session.createQuery(query)
,这样您就可以从hibernate映射的建议中获益
在这篇文章中,您将找到关于hibernate上下文中HQL和SQL之间差异的更多解释