[b]Hibernate version: 3.2 [/b]
[b]Mapping documents:
Employee : <class name="Employee" table="EMPLOYEE">
<id name="emplId" column="EMP_ID" type="java.lang.String"
unsaved-value="null">
<generator class="native">
</id>
<list name="lstMsgs" table="MSG_DETAIL" lazy="false"
cascade="none">
<key column="EMP_ID"></key>
<index column="MSG_ID" />
<many-to-many class="Messages"
column="MSG_ID" outer-join="auto" />
</list>
</class>
Messages: <class name="Messages" table="MSG">
<id name="msgId" column="MSG_ID" type="java.lang.Integer"
unsaved-value="null">
<generator class="native">
</generator>
</id>
<property name="startDate" type="java.util.Date" column="MSG_START_DATE" not-null="true" />
<property name="endDate" type="java.util.Date" column="MSG_END_DATE" not-null="true" />
<list name="employees" table="MSG_DETAIL" lazy="false"
cascade="none">
<key column="MSG_ID"></key>
<index column="EMP_ID" />
<many-to-many class="Employee"
column="EMP_ID" outer-join="auto" />
</list>
</class>
[/b]
I am trying to get list of messages for a particular employee id between certain dates.
SQL : Select msg.msg_id, emp.empID, msg.startDate, msg.endDate
from Msgs m, MSG_DETAIL det
where det.msg_id = m.id
and det.emplId = <value>
and m.startDate >= <value>
and m.endDate <= <value>
How do i implement this using Criteria API. I am new to Hibernate so please help and explain.
[b]Code between sessionFactory.openSession() and session.close():
Session session = HibernateUtil.getSession();
Criteria crit = session.createCriteria(Messages.class);
if ((startDate != null) && (startDate.length() > 0))
crit.add(Restrictions.geProperty(startDate, startDate));
if ((endDate != null) && (endDate.length() > 0))
crit.add(Restrictions.leProperty(endDate, endDate));
if ((emplId != null) && (emplId.length() > 0))
{
crit.add(Restrictions.sqlRestriction(""));
}
lstMessages = crit.list();
session.close();
[/b]
Thanks,
Niraj.
|