Hi folks,
I'm using the JPA and JBoss 4.2.0, and I'm having some trouble with a column name that has an underscore in the middle, named "shift_state"; the field is actually an enum, and has appropriate getters and setters. When I use the javax.persistence.Column annotation with name="shift_state", and use the literal "shift_state" in my queries, they fail silently with no results.
When I take out the @Column(name="shift_state") annotation, and change my HQL to use "shiftState = :boundParam", I get my expected results. Either way, no warning is thrown, and the generated SQL using "hibernate.show_sql" looks great.
Here's some of the specifics:
An example of a working query:
Code:
      Query alreadyWorkingQuery = entityManager.createQuery(
            "from EmployeeShiftVO WHERE "+
            (startDate != null ? "DATE(shift_date) >= DATE(:startDate) AND " : "DATE(shift_date) >= DATE(NOW()) AND ") +
            (endDate != null ? "DATE(shift_date) <= DATE(:endDate) AND " : "" )+
            "nextElement is null AND "+
            "employee = :employee AND "+
            "(shiftState = :shiftState1 OR shiftState = :shiftState2)"
            )
            .setParameter("employee", employee)
            .setParameter("shiftState1", EmployeeShiftState.ADDED_SEAT)
            .setParameter("shiftState2", EmployeeShiftState.ALLOCATED_SEAT)
            ;
      if(startDate != null)
         alreadyWorkingQuery.setParameter("startDate", startDate);
      
      if(endDate != null)
         alreadyWorkingQuery.setParameter("endDate", endDate);
An example of a working mapping in my POJO class:
Code:
   private EmployeeShiftState shift_state;
   @Column
   @Enumerated(value=EnumType.ORDINAL)
   public EmployeeShiftState getShiftState()
   {
      return shift_state;
   }
   public void setShiftState(EmployeeShiftState shiftState)
   {
      this.shift_state = shiftState;
   }
The generated/dumped AST:
Code:
2008-02-25 16:42:16,574 INFO  [STDOUT] Hibernate:
    select
        employeesh0_.id as id131_,
        employeesh0_.version as version131_,
        employeesh0_.nextElement_id as nextEle12_131_,
        employeesh0_.role as role131_,
        employeesh0_.employee_ID as employee13_131_,
        employeesh0_.spanish as spanish131_,
        employeesh0_.shift_date as shift5_131_,
        employeesh0_.survey_id as survey14_131_,
        employeesh0_.shiftConfigVO_id as shiftCo15_131_,
        employeesh0_.previousElement_id as previou11_131_,
        employeesh0_.shiftState as shiftState131_,
        employeesh0_.last_updated_timestamp as last7_131_,
        employeesh0_.last_updated_by as last8_131_,
        employeesh0_.created_by as created9_131_,
        employeesh0_.creation_timestamp as creation10_131_
    from
        employee_shifts employeesh0_
    where
        date(shift_date)>=date(now())
        and (
            employeesh0_.nextElement_id is null
        )
        and employeesh0_.employee_ID=?
        and (
            employeesh0_.shiftState=?
            or employeesh0_.shiftState=?
        )
Now, here's the broken stuff that returns zero results (nothing appears wrong to me):
Broken query (?):
Code:
      Query alreadyWorkingQuery = entityManager.createQuery(
            "from EmployeeShiftVO WHERE "+
            (startDate != null ? "DATE(shift_date) >= DATE(:startDate) AND " : "DATE(shift_date) >= DATE(NOW()) AND ") +
            (endDate != null ? "DATE(shift_date) <= DATE(:endDate) AND " : "" )+
            "nextElement is null AND "+
            "employee = :employee AND "+
            "(shift_state = :shiftState1 OR shift_state = :shiftState2)"
            )
            .setParameter("employee", employee)
            .setParameter("shiftState1", EmployeeShiftState.ADDED_SEAT)
            .setParameter("shiftState2", EmployeeShiftState.ALLOCATED_SEAT)
            ;
if(startDate != null)
         alreadyWorkingQuery.setParameter("startDate", startDate);
      
      if(endDate != null)
         alreadyWorkingQuery.setParameter("endDate", endDate);
The only difference in the mapping is:
Code:
@Column(name="shift_state")
The broken AST:
Code:
2008-02-25 16:46:28,055 INFO  [STDOUT] Hibernate: 
    select
        employeesh0_.id as id186_,
        employeesh0_.version as version186_,
        employeesh0_.nextElement_id as nextEle12_186_,
        employeesh0_.role as role186_,
        employeesh0_.employee_ID as employee14_186_,
        employeesh0_.spanish as spanish186_,
        employeesh0_.shift_date as shift5_186_,
        employeesh0_.survey_id as survey13_186_,
        employeesh0_.shiftConfigVO_id as shiftCo15_186_,
        employeesh0_.previousElement_id as previou11_186_,
        employeesh0_.shift_state as shift6_186_,
        employeesh0_.last_updated_timestamp as last7_186_,
        employeesh0_.last_updated_by as last8_186_,
        employeesh0_.created_by as created9_186_,
        employeesh0_.creation_timestamp as creation10_186_ 
    from
        employee_shifts employeesh0_ 
    where
        date(shift_date)>=date(now()) 
        and (
            employeesh0_.nextElement_id is null
        ) 
        and employeesh0_.employee_ID=? 
        and (
            shift_state=? 
            or shift_state=?
        )
Does Hibernate hate underscores? Thank you for any responses!