gpt4 book ai didi

java - org.hibernate.QueryException : could not resolve property: query of: abc. def.mypackage.orm.Employee

转载 作者:行者123 更新时间:2023-11-30 06:28:53 27 4
gpt4 key购买 nike

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

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

查看有关 Stack Overflow 的类似在线其他帖子,例如以下内容:

org.hibernate.QueryException: could not resolve property:

我们不引用数据库列名称,而是引用属性名称”。所以我明确提到了名称definition,如下面的实体类Employee 中所定义。我尝试获取列 COLUMN_ID 值的子查询使用子查询,因此我不确定是否能够使用符合 HQL 规则的内容?我的意思是 应该从 COMPANY_DATA 表中提取 value_emp_id 的 SELECT 子查询是我提到过的 SQL 查询。那需要修改吗?这可能是此类错误的原因吗?请指教。

我的实体类Employee.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() 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 查询。

关于java - org.hibernate.QueryException : could not resolve property: query of: abc. def.mypackage.orm.Employee,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46533530/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com