-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Criteria Many-to-Many Association
PostPosted: Wed Dec 13, 2006 4:20 pm 
Newbie

Joined: Wed Dec 13, 2006 3:34 pm
Posts: 4
[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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.