本文介绍了Hibernate:如何使用Criteria Query从组合键获取记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经在hbm档案中加入了复合档案,如下所示:
< hibernate-mapping>
< class name =EmployeeSignintable =EMPLOYEE_SIGNIN>
< composite-id name =idclass =EmployeeSigninId>
< key-property name =empIdtype =string>
< column name =EMP_IDlength =10/>
< / key-property>
< key-property name =signinDatetype =date>
< column name =SIGNIN_DATElength =7/>
< / key-property>
< / composite-id>
< / class>
< / hibernate-mapping>
现在我想查询如下
select * from EmployeeSignin where emp_id ='12345'and signin_date> '一些初始日期'和signin_date< ='一些最后日期'
我不知道我怎么能因为存在引用EmployeeSignId的组合键,所以实现相同。如何在这种情况下创建标准查询?
我在下面尝试,但获得0条记录
标准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));
列表empAttendanceList = empAttendanceCr
.add(attdDateCondition).list();
解决方案
我试过下面哪个工作过。
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));
I have added the composite filed in hbm files as follows
<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>
Now I want to Query as follows
select * from EmployeeSignin where emp_id='12345' and signin_date > 'some initial date' and signin_date<= 'some last date'
I am not getting how can i achieve the same because there is composite key which refers to EmployeeSignId. how can i create criteria query in this case ?
I tried below but getting 0 records
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();
解决方案
Issue solved.
I tried below which worked
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));
这篇关于Hibernate:如何使用Criteria Query从组合键获取记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!