-->
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.  [ 2 posts ] 
Author Message
 Post subject: Translate Native SQL to Criteria Query
PostPosted: Wed Mar 05, 2008 6:40 pm 
Newbie

Joined: Mon May 14, 2007 1:57 am
Posts: 12
Hibernate version:
3.2.4

Mapping documents:
MEMBERSHIP
<hibernate-mapping package="mx.com.diaca.persistent">
<class name="Membership" table="MEMBERSHIP">
<id name="membershipID" type="long" column="MEMBERSHIP_ID">
<generator class="native" />
</id>
<property name="type" type="byte" column="TYPE" />
<many-to-one name="client" column="CLIENT_ID" class="Client" not-null="true" />
<property name="expireDate" type="date" column="EXPIRE_DATE" />
</class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():
1 session.beginTransaction();
2 int b=1;
3 String qry=
4 "select m2.* from "+
5 "(select * from "+
6 " (select * from membership order by expire_date desc) as m1 "+
7 " group by m1.client_id) as m2 "+
8 " where DATEDIFF(m2.EXPIRE_DATE, CURRENT_DATE())<=60 AND DATEDIFF(m2.EXPIRE_DATE, CURRENT_DATE())>0";

9 Query q1 = session.createSQLQuery(qry).addEntity(Membership.class);

10 List mbspCloseToExpire = q1.list();
11 session.getTransaction().commit();

12 for(int i=0; i<mbspCloseToExpire.size(); i++) {
13 Afiliacion m1 = (Membership) mbspCloseToExpire.get(i);
14 System.out.println(" > "+m1.getMembershipID()+": "+m1.getClient().getName()+"; membership expires next: "+m1.getExpireDate());
15 }
16

Name and version of the database you are using:
MySQL 5

Problem?
Hi,
I've a table called Membership where I store client's memberships. Client table contains client's info such as name, address, phone, etc. When a membership has expired or is close to expire, a new one can be requested (created). Old memberships are kept stored. I need to retrieve memberships that are close to expire (within next 60 days); in lines 3-8 I've the query that retrieves me those memberships.

1. Is there a way to have this native sql query into a criteria query?

2. When executing the code above I'm receiving the "Abstract Lazy Initializer" exception (I do commit before iterating through the list), I'm aware of this lazy relationship, so when I commit in line 16 instead of line 11 I don't get any exception and query works ok. How do I can do a "join fetch" in this native query? Btw I need to fecth the "Client" which owns the membership.


Top
 Profile  
 
 Post subject: Use SQLCriterion
PostPosted: Mon Mar 17, 2008 7:11 am 
Beginner
Beginner

Joined: Wed Jan 16, 2008 8:16 am
Posts: 23
Hi,

if I understand you correctly, you can use the SQLCriterion class. After creating the Criteria object you can add following sql restriction:

Code:
String restr = "{alias}.id in (your sql)";
criteria.add(Restrictions.sqlRestriction(restr, ...);


It could be done - of course - much nicer, but it would works.

best regards,
michi


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

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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.