小编典典

Hibernate:如何使用条件查询从组合键获取记录

hibernate

我已经在hbm文件中添加了复合文件,如下所示

<hibernate-mapping>
<class name="EmployeeSignin" table="EMPLOYEE_SIGNIN">
<composite-id name="id" class="EmployeeSigninId">
        <key-property name="empId" type="string">
            <column name="EMP_ID" length="10" />
        </key-property>
        <key-property name="signinDate" type="date">
            <column name="SIGNIN_DATE" length="7" />
        </key-property>
</composite-id>
</class>
</hibernate-mapping>

现在我想查询如下

select * from EmployeeSignin where emp_id='12345' and signin_date > 'some initial date' and signin_date<= 'some last date'

我没有得到相同的结果,因为有一个引用EmployeeSignId的组合键。在这种情况下,如何创建条件查询?

我在下面尝试过但得到0条记录

Criteria empAttendanceCr=session2.createCriteria(EmployeeSigninId.class);
Criterion attdDateCondition = Restrictions.conjunction()
                .add(Restrictions.eq("empId", user.getEmpId()))
                .add(Restrictions.le("signinDate", lastDate))
                .add(Restrictions.ge("signinDate", startDate));

        List empAttendanceList=empAttendanceCr
                .add(attdDateCondition).list();

阅读 311

收藏
2020-06-20

共1个答案

小编典典

问题已解决。

我尝试了下面的工作

Criteria empAttendanceCr=session2.createCriteria(EmployeeSignin.class);
        //EmployeeSigninId empId=new EmployeeSigninId(user.getEmpId().toString());

        Criterion attdDateCondition = Restrictions.conjunction()                    
                .add(Restrictions.eq("id.empId",user.getEmpId().toString()))
                .add(Restrictions.le("id.signinDate", lastDate))
                .add(Restrictions.ge("id.signinDate", startDate));
2020-06-20