-->
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: ejb 3 and entity manager
PostPosted: Sat Jul 28, 2012 3:42 am 
Regular
Regular

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

I have a simple but important question...

Lately i have been testing my hibernate search app (EJB 3.0 and SOA) and i discover the next thing:

- I have an entity ejb stateless (ItemEjb) with an entity manager. i create some entities and i get them correctly.
- I have a search ejb stateless entity (SearchEjb) with an entity manager. If i search i dont have the last entity i created in the ItemEjb.

I am thinking that i have two sessions. I am using refresh method...

so my question is what is the right way to share a session between ejbs:

Code:
private ItemEjb...{
...
@javax.persistence.PersistenceContext
private EntityManager em;
...
}

private SearchEjb...{
...
@javax.persistence.PersistenceContext
private EntityManager em;
...
}


Thanks,

pd: if this right, i try to post my daofactory and more to check if everything is right (ejb patterns).

Hibernator,


Top
 Profile  
 
 Post subject: Re: ejb 3 and entity manager
PostPosted: Mon Jul 30, 2012 6:17 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

what is your call sequence? If ItemEjb creates a new entity within a transaction and this transaction completes (and you rely on automatic indexing), the index should be successfully updated. When the a search request comes in the new indeed entity should be found.

Indexing and searching with the same session/transaction won't work. Maybe you can clarify what you are doing.

In order to debug the problem I recommend turning on debug or even trace logging for the category org.hibernate.search. You should be able to see what is happening against the database as well as against the Lucene index. Also have a look at the index with Luke. Last, but not least, are you sure that your query is actually matching something (remember for example that in most cases you need to use the same analyzer for indexing as well as searches).

--Hardy


Top
 Profile  
 
 Post subject: Re: ejb 3 and entity manager
PostPosted: Tue Jul 31, 2012 9:20 am 
Regular
Regular

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

Now it seems to be working. I have one thing in my code that I don't really like. I show you an example and talk about that:

Code:
@Stateless
private ItemEjb...{
...
@javax.persistence.PersistenceContext
private EntityManager em;

   @Override
   public ItemEntity getItem(int id)
   {
          Session session = em.unwrap(Session.class);
       FactoryEao factoryEao = new FactoryEao(session);
            .....
        }
...
}


public class FactoryEao
{
   private Session session;

   public FactoryEao(Session session)
   {
      this.session = session;
   }
   
   public IItemEao getItemEao()
   {
      return new ItemEao(session);
   }
   
   ...all EAOs
}



As you can see, I use FactoryEao where I can get any of my DAOs. What I don't like is that i have to get the hibernate session and create the FactoryEao in every EJB method.

I would like to instance FactoryEao and Hibernate session once in my stateless EJB and use it everywhere. I tried it and i got "session is closed" exception....

I would say that I have a problem of my pattern design. For the moment, it is working but I would like to know the right way to do it...

Thanks for your time :)

Hibernator.


Top
 Profile  
 
 Post subject: Re: ejb 3 and entity manager
PostPosted: Tue Jul 31, 2012 9:40 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

to be honest I have not idea what FactoryEao is. Is this one of your classes? I have now idea why you are unwrapping the Session and why you need this factory at all.

--Hardy


Top
 Profile  
 
 Post subject: Re: ejb 3 and entity manager
PostPosted: Tue Jul 31, 2012 10:43 am 
Regular
Regular

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

FactoryEao is a pattern Factory for Data Access Objects Strategy. http://java.sun.com/blueprints/corej2eepatterns/Patterns/DataAccessObject.html

My FactoryEao class contains every entity access object. (ItemDao, CarEntity).

Anyway, i just wanted to use a factoryDAO and i have it working....i had a doubt and wanted to know if this was the right way to do it...

thanks,

Hibernator,


Top
 Profile  
 
 Post subject: Re: ejb 3 and entity manager
PostPosted: Tue Jul 31, 2012 5:06 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
The pattern and the article you point to are very old. Generally the EntityManager should be used directly, as it already is designed to provide a good compromise of abstraction level.

I didn't fully understand your problem, but the EJB container provides the EntityManager and manages its lifecycle properly so that you don't have to write that boilderplate code and can use your coding skills on something less boring.

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


Top
 Profile  
 
 Post subject: Re: ejb 3 and entity manager
PostPosted: Tue Jul 31, 2012 5:14 pm 
Regular
Regular

Joined: Thu Jun 16, 2011 12:03 pm
Posts: 94
It doesn't matter...I really think that it is a good point using a FactoryDao in case you want to change your data repository you dont have to change the whole code.

Maybe i have been a little bit naive or simple...i just wanted to share my point and code to come up with something better....

Hibernator,


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.