-->
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.  [ 7 posts ] 
Author Message
 Post subject: is better Jpa or Hibernate to create the session in EJB 3.0?
PostPosted: Sat Jun 18, 2011 5:19 am 
Regular
Regular

Joined: Thu Jun 16, 2011 12:03 pm
Posts: 94
Hi all,

I am trying to create a hibernate search project and I am confused. It is better to use JPA or Hibernate Session? I am using EJB 3.0. My connection pool is in jboss AS6.

First, I try with JPA but I got "Hibernate Session is closed". Then I change to Hibernate and it seems to be working fine...what's the difference?

I have this:

@PersistenceContext(unitName="custDb")
EntityManager manager;

org.hibernate.Session session = (Session)manager.getDelegate();

Thanks in advance.


Top
 Profile  
 
 Post subject: Re: is better Jpa or Hibernate to create the session in EJB 3.0?
PostPosted: Sun Jun 19, 2011 6:40 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Hi,
for what matters for Hibernate Search's purposes they should behave exactly the same, being different only in the exposed interface.
Could you provide a test or example? As far as I know, that should work fine.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: is better Jpa or Hibernate to create the session in EJB 3.0?
PostPosted: Sun Jun 19, 2011 4:20 pm 
Regular
Regular

Joined: Thu Jun 16, 2011 12:03 pm
Posts: 94
That's exactly what i thought but with jpa i got the exception "hibernate session is closed".

I got the mysql pool at JBoss 6 and I inject the code in my EJB 3.0 with:

@PersistenceContext()
EntityManager em;

My example with JPA:

FullTextEntityManager fullTextEntityManager = org.hibernate.search.jpa.Search.getFullTextEntityManager(em);
final QueryBuilder b = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity( Myth.class ).get();

org.apache.lucene.search.Query luceneQuery = b.keyword().onField("history").boostedTo(3).matching("storm").createQuery();
javax.persistence.Query fullTextQuery = fullTextEntityManager.createFullTextQuery( luceneQuery );

List result = fullTextQuery.getResultList(); //return a list of managed objects

My example with Hibernate Session:

Session session = (Session) em.unwrap(Session.class);
FullTextSession fullTextSession = Search.getFullTextSession(session);

SearchFactory searchFactory = fullTextSession.getSearchFactory();
...
org.hibernate.Query fullTextQuery = fullTextSession.createFullTextQuery(luceneQuery);

List result = fullTextQuery.list(); //return a list of managed objects


It's like with JPA the Hibernate Session is never initialized..but I don't really know. I' ve got an example that works with Hibernate session. You can check it out at http://forum.hibernate.org/viewtopic.php?f=9&t=1011450.

With JPA, should i start the session? I think that the EJB does it for me....

Well, let me know any suggestions :)

Thanks in advance.


Top
 Profile  
 
 Post subject: Re: is better Jpa or Hibernate to create the session in EJB 3.0?
PostPosted: Sun Jun 19, 2011 4:52 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
yes you should not need to start the session, that should be done when it's injected.
Does something else work using this em? Can you check if a simple load, or a criteria work? Just to know if there's something wrong with your EntityManager, or with the Search integration.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: is better Jpa or Hibernate to create the session in EJB 3.0?
PostPosted: Sun Jun 19, 2011 5:06 pm 
Regular
Regular

Joined: Thu Jun 16, 2011 12:03 pm
Posts: 94
as far as i know, every time that i wanted to make a search query with JPA i got to do this:

FullTextEntityManager fullTextEntityManager = org.hibernate.search.jpa.Search.getFullTextEntityManager(em);
final QueryBuilder b = fullTextEntityManager.getSearchFactory()
javax.persistence.Query fullTextQuery = fullTextEntityManager.createFullTextQuery( luceneQuery );


I want to create a query and use facets so i just need to create my fullTextQuery one time and apply or eliminate the facets. With jpa that was impossible because i got the exception "Hibernate session is closed" when I use the fullTextQuery created before.

I would like to use jpa but i can't make it work...

thanks in advance.


Top
 Profile  
 
 Post subject: Re: is better Jpa or Hibernate to create the session in EJB 3.0?
PostPosted: Sun Jun 19, 2011 5:10 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
yes I understood that, but to try understanding your error I need to know if other queries work with the entitymanager (non fulltext but standard database queries).
Also, do you see exceptions or other error messages in the log?

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: is better Jpa or Hibernate to create the session in EJB 3.0?
PostPosted: Tue Jun 21, 2011 3:23 am 
Regular
Regular

Joined: Thu Jun 16, 2011 12:03 pm
Posts: 94
Hi,

General database querys in mysql are working and also fulltextEntitymanager :

public String doindex() throws InterruptedException
{
FullTextEntityManager fullTextEntityManager = org.hibernate.search.jpa.Search.getFullTextEntityManager(em);

fullTextEntityManager.createIndexer().startAndWait();

return "OK";
}

The problem, as you see in my code posted before, is that if i want to reuse a query created before, hibernate tells me that the session is closed.

I got everything working now but only when i want to reuse the query for faceting, i am using the fulltextsession:

Session session = (Session) em.unwrap(Session.class);
FullTextSession fullTextSession = Search.getFullTextSession(session);

Thanks in advance


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