org.hibernate.QueryException:无法解析属性:查询:abc.def.mypackage.orm.Employee

时间:2017-10-02 20:49:05

标签: java hibernate hql spring-4 hibernate-4.x

当我尝试执行insertEmployeeDetails方法时,我收到以下错误:

org.hibernate.QueryException:无法解析属性:查询:abc.def.mypackage.orm.Employee [INSERT INTO Employee(名称,定义,(SELECT value_emp_id FROM COMPANY_DATA WHERE testing_id = 1234 AND company_employee_id = 3345)) ]

例如,在Stack溢出时查看类似的在线其他帖子,以下内容说:

org.hibernate.QueryException: could not resolve property:

我们不引用数据库列名,我们引用属性名称 ”。所以我清楚地提到了name和{{ 1}}如下面的实体类definition中所定义。我试图获取列Employee的值的子查询使用子查询,所以我不确定我是否能够使用与HQL规则一致的内容?我的意思是COLUMN_ID子查询应该从SELECT表中提取value_emp_id的是我提到的SQL查询。这需要修改吗?这可能是造成这类错误的原因吗?请指教。

我的实体班COMPANY_DATA如下:

Employee.java

以下是我在方法中使用HQL的方法:

package abc.def.mypackage.orm

@Entity
@Table(name = "EMPLOYEE")
public class Employee {
 public int getEmployeeId() {
  return employeeId;
 }

 public void setEmployeeId(int employeeId) {
  this.employeeId = employeeId;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public int getIsCorrect() {
  return isCorrect;
 }

 public void setIsCorrect(int isCorrect) {
  this.isCorrect = isCorrect;
 }

 public int getIsWrong() {
  return isWrong;
 }

 public void setIsWrong(int isWrong) {
  this.isWrong = isWrong;
 }

 public int getCompanyId() {
  return companyId;
 }

 public void setCompanyId(int companyId) {
  this.companyId = companyId;
 }

 public Integer getTransactionId() {
  return transactionId;
 }

 public void setTransactionId(Integer transactionId) {
 this.transactionId = transactionId;
 }

 public String getDefinition() {
  return definition;
 }

 public void setDefinition(String definition) {
  this.definition = definition;
 }

 @Id
 @Column(name = "EMPLOYEE_ID")
 @GeneratedValue(strategy = GenerationType.AUTO, generator = "seqgen")
 @SequenceGenerator(name = "seqgen", sequenceName = "EMPLOYEE_AUTOINC_SEQ")
 private int employeeId;

 @Column(name = "NAME")
 private String name;

 @Column(name = "DEFINITION")
 private String definition;

 @Column(name = "IS_CORRECT")
 private int isCorrect;

 @Column(name = "IS_WRONG")
 private int isWrong;

 @Column(name = "COMPANY_ID")
 private int companyId;

 @Column(name = "TRANSACTION_ID", nullable = true)
 private Integer transactionId;


}

1 个答案:

答案 0 :(得分:1)

此处的问题是您将 createQuery() createSQLQuery() 混淆,因为createQuery()执行了HQL查询而不是SQL查询,如文档中所示:

  

<强>的createQuery

     

为给定的HQL查询字符串 创建查询 的新实例。

事实上,您传递给hqlInsert方法的string查询createQuery()是一个SQL插入查询,由createQuery()取消,您需要使用createSQLQuery()以便您可以使用Hibernate执行它,并确保使用数据库列而不是实体属性,因为它是SQL查询。