-
1. Re: Is it possible to have a "dynamic" @PersistenceContext?
andreitognolo Jun 29, 2013 11:11 AM (in response to edilmar)Hi Edilmar,
My solution can't use some kind of multitenancy, because each client must have a separate database, it's a major pre-requisite.
If I'm not wrong, you can use the "Separate Database" approach of Hibernate Multi-tenancy (http://docs.jboss.org/hibernate/orm/4.1/devguide/en-US/html/ch16.html#d5e4570). What do you think about use this approach?
My big problem is @PersistenceContext(unitName="XXX"), because XXX is defined statically, I don't get to change XXX after
"an analysis of the user to log on".
I believe that there's no way to do it. But, even if you had a way to dynamic define, you still have the problem of dynamic configure multiples persistence-unit in your persistence.xml, and I don't know how to solve without Hibernate Multi-tenancy.
-
2. Re: Is it possible to have a "dynamic" @PersistenceContext?
kwintesencja Jun 30, 2013 10:16 PM (in response to andreitognolo)Never used Hibernate multitenancy but if you cant use it there is always the brute force way, maybe it helps:
public class EntityManagerProvider {
@PersistenceContext(unitName = "unit1")
EntityManager em1;
@PersistenceContext(unitName = "unit2")
EntityManager em2;
@PersistenceContext(unitName = "unit3")
EntityManager em3;
@Inject User user;
public EntityManager getCurrentEntityManager(){
if(user.somePropertyThatIdentifiesItsDatabase == 'database1'){
return em1;
}
else if(user.somePropertyThatIdentifiesItsDatabase == 'database2'){
return em2;
}
//and so on
}
}
@Stateless
public class MyService{
@Inject EntityManagerProvider entityManagerProvider;
EntityManager em;
@PostContruct
public void initEntityManager(){
this.em = entityManagerProvider.getCurrentEntityManager();
}
}
-
3. Re: Is it possible to have a "dynamic" @PersistenceContext?
edilmar Jul 3, 2013 5:04 PM (in response to kwintesencja)Andrei,
I think the unique alternative for this is to create many EntityManagers injected just commented by Rafael.
This is a very ugly solution, because each time a new database is created for a client, I have to change the code and persistence.xml.
Multitenancy with separate databases also doesn't work because I have the same problem to inject the databases.
I would like something like this in my DAO but I know today CDI/Weld doesn't have a way to inject an object into a method:
public class MyDAO implements MyInterfaceDAO {
EntityManager em;
@PostConstruct
public void initDAO() {
User user = getUserFromSessionAttribute(); // a method to access the session
if(user.somePropertyThatIdentifiesItsDatabase == 'database1') {
@PersistenceContext(unitName = "pu1") EntityManager em1;
em = em1;
}
else if(user.somePropertyThatIdentifiesItsDatabase == 'database2') {@PersistenceContext(unitName = "pu2") EntityManager em2;
em = em2;
}
// etc...}
}
-
4. Re: Is it possible to have a "dynamic" @PersistenceContext?
edilmar Jul 4, 2013 3:48 PM (in response to edilmar)I got! Look at this: http://www.hostettler.net/blog/2012/11/20/multi-tenancy/