-->
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.  [ 10 posts ] 
Author Message
 Post subject: How to work with multiple persistence units
PostPosted: Thu May 24, 2007 6:54 pm 
Regular
Regular

Joined: Wed May 02, 2007 2:42 pm
Posts: 101
Hey,

I have a generic DAO.
The DAO is a stateless and in the DAO the persistence context is injected:

@PersistenceContext
public void setEntityManager(EntityManager em) {
this.em = em;
}

The problem is that I have 5 schemas.
Each schema has a persistence unit, so I can’t use in the default persistence unit.

How you guys work in case that you have multiple schemas.

Thank you


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 29, 2007 2:40 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
multiple schemas does not imply multiple persistent units. What is you use case?

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Here is my use case
PostPosted: Wed May 30, 2007 12:02 pm 
Regular
Regular

Joined: Wed May 02, 2007 2:42 pm
Posts: 101
The user can craete his own schema, at run time.

I have a GenericDAO that serve all the CRUD request.

So, i cant define the persistence unit name in the DAO , because it need to server multiple persistence unit or to be more accurate multiple schemas , because each user has its own schema.

You said that "multiple schemas does not imply multiple persistent units."

How can i do this?

How does it know with wich schema it should work?

Thank you a lot.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 30, 2007 12:45 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
In your case you'll have to take care of the EMF lifecycle manually. You won't be able to inject it.

What I meant is that you can define the schema name for a given entity, hence having entities in several schemas.
But you want to duplicate the same entities across several schema (same DDL across different schemas).

_________________
Emmanuel


Top
 Profile  
 
 Post subject: So, what can i do? can i write an intercepror that will push
PostPosted: Wed May 30, 2007 1:57 pm 
Regular
Regular

Joined: Wed May 02, 2007 2:42 pm
Posts: 101
persistence unit befor it inject the entity manager?

what you mean to control manually? how can i do this in a stateless session bean and does the entity manager that i create manually attach to the stateless transaction?

Thnk you a lot.

i know that this is a lot of question, but i cant find a solution.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 31, 2007 1:24 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
control manually means that you will have to do
EM em = emf.createEntityManager();
em.close();
in your code.

_________________
Emmanuel


Top
 Profile  
 
 Post subject: What about the transaction?
PostPosted: Thu May 31, 2007 5:34 pm 
Regular
Regular

Joined: Wed May 02, 2007 2:42 pm
Posts: 101
does any time that i will call to factory.getEntityManger in this ejb transaction context i will get the same entity manager?

for example:

Facade - stateless session bean , transaction required
Dao - stateless session bean , get the entiy manager manually


In the facade i have method - persist(){
ItemDao.persist
BidDao.persist
}

does BidDao will get the same entity manager that i get in ItemDao?
does rollback in BidDao will cause to rollback in changes that were done in ItemDao?

as i understand if i create the entity manager manually they will not share the same transaction (unlike injection) and this is big issue.

Thank you very very much


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 01, 2007 8:59 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
factory.getEntityManager() does not exists, it's only factory.createEM, so the EM will be different, ie a different Persistence Context.

If your Tx is JTA and if you point to the same datasource, then you will share the same transaction.

_________________
Emmanuel


Top
 Profile  
 
 Post subject: I test it and it didnt share the same transaction
PostPosted: Sat Jun 02, 2007 12:42 pm 
Regular
Regular

Joined: Wed May 02, 2007 2:42 pm
Posts: 101
Hey,

My test was like this:

Facade - stateless session bean (transaction required)
SaveItem - simple class (no ejb)

Facde call to SaveItem and in SaveItem i create entity manager and save item.

In the facade i throw an exception after the call to SaveItme return , but roll back didnt happen. the data was persist in the DB.

Another issue is that i want the same persistence context , what are you thinking about using in the thread local pattern , although i am working in J2EE container.

I mean that my DAO will check if there is a EM in the thread local and if now will create and put it in the thread local and from now all the time that i will call to any dao instance in the same thread i will get the same EM.

Is this a good design for J2EE?

Thank you very much.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 06, 2007 11:46 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Well, if properly configured it definitively work if the exception is runtime and the transaction type is defined as JTA in HEM

The thread local session is no a good solution.
Maybe you should fall back to the sessionFactory and use getCurrentSession() check the reference doc on how to configure it to work in a JTA environment.

_________________
Emmanuel


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