-->
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.  [ 3 posts ] 
Author Message
 Post subject: Getting a Hibernate session/criteria from JPA EntityManager
PostPosted: Wed Sep 22, 2010 6:33 am 
Newbie

Joined: Sun Mar 27, 2005 1:22 pm
Posts: 15
Hi,

We have an application that has been built using the Hibernate API, most of our queries are built upon 'session.createCriteria'.

The decision has been made to inject a javax.persistence.EntityManager into the DAOs and our queries have to be made from that.

Rather than rewriting about 100 queries all in one go to use the JPA criteria API is there an easier way to get the hibernate session object to create the org.hibernate.Criteria?

Thanks


Top
 Profile  
 
 Post subject: Re: Getting a Hibernate session/criteria from JPA EntityManager
PostPosted: Wed Sep 22, 2010 2:16 pm 
Regular
Regular

Joined: Sun Feb 14, 2010 3:29 pm
Posts: 58
Location: USA
Have you tried
Code:
Session session = (Session)entityManager.getDelegate();
?

_________________
Zemian Deng
------------
Need a Java Scheduler? Try
http://bitbucket.org/timemachine/scheduler


Top
 Profile  
 
 Post subject: Re: Getting a Hibernate session/criteria from JPA EntityManager
PostPosted: Thu Sep 23, 2010 7:41 am 
Newbie

Joined: Thu Sep 23, 2010 4:43 am
Posts: 10
Hi,

you can inject sessionFactory class in base class, and add methods below to your base object.Reuse methods in childs.



@Autowired
private SessionFactory sessionFactory;

public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
protected Criteria criteria(Class clasz){
return session().createCriteria(clasz);
}
protected Session session(){
return getSessionFactory().getCurrentSession();
}

protected Session open(){
return getSessionFactory().openSession();
}
protected org.hibernate.Query createQuery(String queryStr){
org.hibernate.Query query = session().createQuery(queryStr);
return query;
}
protected org.hibernate.SQLQuery createSQLQuery(String queryStr){
org.hibernate.SQLQuery query = session().createSQLQuery(queryStr);
return query;
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.