Search code examples
javahibernatehqlspring-4hibernate-4.x

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


I am getting the following error when I am trying to execute the insertEmployeeDetails method :

org.hibernate.QueryException: could not resolve property: query of: abc.def.mypackage.orm.Employee [INSERT INTO Employee (name,definition,( SELECT value_emp_id FROM COMPANY_DATA WHERE testing_id = 1234 AND company_employee_id = 3345))]

Looking at similar online other posts on Stack overflow, say for example, the following one says:

org.hibernate.QueryException: could not resolve property:

that "we don't make reference to database column names, we reference property names instead".So I have clearly mentioned name and definition as defined in the entity class Employee below. The subquery where I am trying to get the value of column COLUMN_ID uses the sub query so I am not sure if I would be able to use something in agreement with HQL rules?, I mean that SELECT subquery which is supposed to pull value_emp_id from COMPANY_DATA table is a SQL query that I have mentioned. Does that needs to be modified? Could this be the reason for this type of error? Please advise.

My Entity Class Employee.java is as follows:

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;


}

Here is how I am using the HQL in my method :

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;
    }

Solution

  • The problem here is that you are confusing createQuery() with createSQLQuery(), because createQuery() executes a HQL query and not an SQL query as you can see in the documentation:

    createQuery

    Create a new instance of Query for the given HQL query string.

    In fact the hqlInsert query string you passed to the createQuery() method is an SQL insert query, which is unsopported by createQuery(), you need to use createSQLQuery() so you can execute it with Hibernate, and make sure to use database columns instead of Entity attributes on it as it's an SQL query.