org.hibernate.QueryException:无法解析 属性:查询:abc.def.mypackage.orm.Employee
org.hibernate.QueryException: could not resolve property: query of: abc.def.mypackage.orm.Employee
我在尝试执行 insertEmployeeDetails
方法时遇到以下错误:
org.hibernate.QueryException: 无法解析 属性: 查询: abc.def.mypackage.orm.Employee [INSERT INTO Employee (name,definition,( SELECT value_emp_id FROM COMPANY_DATA 其中 testing_id = 1234 和 company_employee_id = 3345))]
看Stack overflow上类似的网上其他帖子,比如说,下面这个是这样说的:
org.hibernate.QueryException: could not resolve property:
“我们不引用数据库列名称,而是引用 属性 名称”。所以我已经在下面的实体 class Employee
中明确提到了 name
和 definition
。我试图获取列 COLUMN_ID
值的子查询使用子查询,所以我不确定我是否能够使用符合 HQL 规则的东西?,我的意思是 SELECT
子查询应该从 COMPANY_DATA
table 中提取 value_emp_id
是我提到的 SQL 查询。那需要修改吗?这可能是此类错误的原因吗?请指教
我的实体ClassEmployee.java
如下:
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;
}
以下是我在方法中使用 HQL 的方式:
public boolean insertEmployeeDetails(Employee employee)
{
logger.debug("Starting EmployeeDaoImpl.insert() .....");
Session session = null;
Transaction tx = null;
boolean status = true;
try {
session = sessionFactory.openSession();
tx = session.beginTransaction();
String hqlInsert = "INSERT INTO Employee (name,definition,"
+ "( SELECT value_emp_id FROM COMPANY_DATA WHERE testing_id = 1234 AND"
+ " company_employee_id = 3345))";
int createdEntities = session.createQuery( hqlInsert )
.executeUpdate();
session.persist(employee);
tx.commit();
System.out.println("Checking for hqlInsert");
System.out.println(hqlInsert);
System.out.println("Checking for CreatedEntities");
System.out.println(createdEntities);
} catch(Exception ex) {
tx.rollback();
ex.printStackTrace();
status = false;
} finally {
session.close();
}
logger.debug("Completed EmployeeDaoImpl.insert() .....");
return status;
}
这里的问题是您在混淆 createQuery()
with createSQLQuery()
,因为 createQuery()
执行 HQL
查询而不是 SQL
查询如您在文档中所见:
createQuery
Create a new instance of Query for the given HQL query string.
实际上,您传递给 createQuery()
方法的 hqlInsert
查询 string
是一个 SQL
插入查询,createQuery()
不支持,您需要使用 createSQLQuery()
以便您可以使用 Hibernate
执行它,并确保使用数据库列而不是实体属性,因为它是一个 SQL 查询。
我在尝试执行 insertEmployeeDetails
方法时遇到以下错误:
org.hibernate.QueryException: 无法解析 属性: 查询: abc.def.mypackage.orm.Employee [INSERT INTO Employee (name,definition,( SELECT value_emp_id FROM COMPANY_DATA 其中 testing_id = 1234 和 company_employee_id = 3345))]
看Stack overflow上类似的网上其他帖子,比如说,下面这个是这样说的:
org.hibernate.QueryException: could not resolve property:
“我们不引用数据库列名称,而是引用 属性 名称”。所以我已经在下面的实体 class Employee
中明确提到了 name
和 definition
。我试图获取列 COLUMN_ID
值的子查询使用子查询,所以我不确定我是否能够使用符合 HQL 规则的东西?,我的意思是 SELECT
子查询应该从 COMPANY_DATA
table 中提取 value_emp_id
是我提到的 SQL 查询。那需要修改吗?这可能是此类错误的原因吗?请指教
我的实体ClassEmployee.java
如下:
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;
}
以下是我在方法中使用 HQL 的方式:
public boolean insertEmployeeDetails(Employee employee)
{
logger.debug("Starting EmployeeDaoImpl.insert() .....");
Session session = null;
Transaction tx = null;
boolean status = true;
try {
session = sessionFactory.openSession();
tx = session.beginTransaction();
String hqlInsert = "INSERT INTO Employee (name,definition,"
+ "( SELECT value_emp_id FROM COMPANY_DATA WHERE testing_id = 1234 AND"
+ " company_employee_id = 3345))";
int createdEntities = session.createQuery( hqlInsert )
.executeUpdate();
session.persist(employee);
tx.commit();
System.out.println("Checking for hqlInsert");
System.out.println(hqlInsert);
System.out.println("Checking for CreatedEntities");
System.out.println(createdEntities);
} catch(Exception ex) {
tx.rollback();
ex.printStackTrace();
status = false;
} finally {
session.close();
}
logger.debug("Completed EmployeeDaoImpl.insert() .....");
return status;
}
这里的问题是您在混淆 createQuery()
with createSQLQuery()
,因为 createQuery()
执行 HQL
查询而不是 SQL
查询如您在文档中所见:
createQuery
Create a new instance of Query for the given HQL query string.
实际上,您传递给 createQuery()
方法的 hqlInsert
查询 string
是一个 SQL
插入查询,createQuery()
不支持,您需要使用 createSQLQuery()
以便您可以使用 Hibernate
执行它,并确保使用数据库列而不是实体属性,因为它是一个 SQL 查询。