-->
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.  [ 5 posts ] 
Author Message
 Post subject: Select for multiple values and to Insert
PostPosted: Mon Apr 23, 2007 7:29 am 
Newbie

Joined: Tue Jan 16, 2007 7:54 am
Posts: 6
Hi friends,

I have to query a table something like this:

Code:
Select * from person where id in ('1','2','3')


How do we write a hibernate equivalent for the above?

I have one more problem - I want to insert an id (id of Y) value in a table say x, as soon as a record is entered into a table Y.
I am confused about these.

Any help/suggestion is much appreciated.

Thanks
Vishal



Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.1

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Problems with Session and transaction handling?

Read this: http://hibernate.org/42.html


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 23, 2007 7:51 am 
Regular
Regular

Joined: Mon Jun 12, 2006 5:33 am
Posts: 63
Hi h1bernate,

use HQL (closer to SQL). Try this:
Code:
List<Person> persons = session.createQuery("from Person p where p.id in (1, 2, 3)").list();

chucky

Don't forget to rate if this helps
-------------------------------------


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 23, 2007 8:33 am 
Regular
Regular

Joined: Mon Mar 26, 2007 12:38 am
Posts: 119
Hi,
>>> I have one more problem - I want to insert an id (id of Y) value in a table say x, as soon as a record is entered into a table Y.

You can use database triggers for the same. ( database should support it )

Using Interceptors is one more option. I have a sample code here,

public class MyInterceptor extends EmptyInterceptor {
@Override
public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
Session session = HibernateUtil.currentSession() ;
X audit = new X() ; // audit table
Y y ; // entity being saved
if (entity instanceof Y) {
y = (Y) entity ;
audit.setId(y.getId()) ;
session.save(y) ;
}
return super.onSave(entity, id, state, propertyNames, types);
}
}

And then, you can use this at session level. ( sample ) // in HibernateUtil.java ( or anything similar )

public synchronized static Session currentSession(Interceptor interceptor) throws HibernateException {
Session s = session.get();
if (s == null) {
s = sessionFactory.openSession(interceptor);
session.set(s);
}
return s;
}


Concerns: ( in the sample )
i) Session is used inside the interceptor code. When session context is bound to Thread, I think, this should not cause problems. I don't know if there are any pit falls. ( Read more and then use it )


You can read more on interceptors at,
http://www.hibernate.org/hib_docs/v3/re ... terceptors

-------------------------------------------------------------------
Rate the reply if you find it helpful


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 23, 2007 9:21 am 
Regular
Regular

Joined: Mon Jun 12, 2006 5:33 am
Posts: 63
Hi Hi h1bernate,
or if there's a relationship between the to tables, more simple: cascade (can be specified in the mapping file)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 24, 2007 5:43 am 
Newbie

Joined: Tue Jan 16, 2007 7:54 am
Posts: 6
Thanks for your responses - let mw go through them.

Thanks
Vishal


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.