Can i pass the persistence unit to the Ejb before it inject the entity mana

Hey,

The user in my system can create his personal schema, so i need to work with multiple schemas.

As i understand i need to create at run time persistence unit for each schema.

The problem is that my DAO i inject the entity manager in the following way:

@PersistenceContext(persistenceUnit="bla-bla")

publicvoid setEntityManager(EntityManager em){

this.em = em;

}

i need to write the persistence unit name hard coded.

How can i solve this?

Thank you

[663 byte] By [AvihaiMara] at [2007-11-27 5:49:12]
# 1

If there is only one persistence unit defined, the unitName() does not need to be specified. In that

case, the @PersistenceContext will just default to whatever one persistence unit exists.

If you need to define multiple persistence units but you don't know which one a piece of code

needs to use, another approach is to override the unitName of the @PersistenceContext in the

corresponding deployment descriptor.

E.g.

private @PersistenceContext(name="my_pc_reference") EntityManager em;

Note that the name() attribute is not the same as the unitName() attribute. name() just

refers to the named location of the dependency within the private component environment.

unitName() refers to the corresponding persistence unit.

Then, in the deployment descriptor :

<persistence-context-ref>

<persistence-context-ref-name>my_pc_reference</persistence_context_ref_name>

<persistence-unit-name>bla-bla</persistence-unit-name>

<persistence-context-ref>

The .xml representation of the persistence context dependency is associated with the

corresponding @PersistenceContext by using the value of name() in the

persistence-context-ref-name field.This way you can set the correct unit name value

for a particular dependency right before you're ready to deploy, without having to change

any source code or recompile.

ken

ksaksa at 2007-7-12 15:35:26 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2

Sorry, maybee i wasnt clear.

each user can create his own schema, so in run time i create additonal schema (which mean as i understand new persistence units one for each schema)

I have GenericDAO, that expose the CRUD operations.

This GenericDAO serve all persistence unit , so i need to be able to pass him the persistence unit or other trick before it inject the entity manager.

If i have persistence unit A,B...Z at run time i need to create the entity manager for this schema .

I dont want to create the same DAO for each schema, and i dont know the schema name at compile time.

Thank you

AvihaiMara at 2007-7-12 15:35:26 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3

There's no way to get this kind of dynamic association to a persistence unit for container-managed

persistence contexts.You'll need to explicitly create an EntityManager using the

EntityManagerFactory API, where you specify the name of the persistence unit at runtime. Even in

that case, the persistence unit must already be defined.

--ken

ksaksa at 2007-7-12 15:35:26 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 4

Sure the persistence unit will be created before .

i still dont know how to do it at run time , but i will figure out -:)

if you have an idea you are more than welcome.

So the problem that left is how to craete the entity manager at run time in the stateless session bean.

should i for each method in my DAO create a entity manager ? (this is stateless)

how can i obtain the factory?

and the most important question , how i attach the entity manager transaction to the Stateless transaction (i mean to the JTA). the injected did this work for me , but now i need to fo it manually.

Thank you very much.

AvihaiMara at 2007-7-12 15:35:26 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 5

>

> So the problem that left is how to craete the entity

> manager at run time in the stateless session bean.

>

EntityManagerFactory emf = Persistence.createEntityManagerFactory("pu_name");

EntityManager em = emf.createEntityManager();

There's an example here :

https://glassfish.dev.java.net/javaee5/persistence/persistence-example.html#Using_in_Java_SE

> should i for each method in my DAO create a entity

> manager ? (this is stateless)

>

Depends on your how your app is structured. You need to make sure you

don't have multiple threads accessing the same EM instance.If the DAO

is not accessed concurrently, it's ok to store it in instance state.

> how can i obtain the factory?

See above.

> and the most important question , how i attach the

> entity manager transaction to the Stateless

> transaction (i mean to the JTA). the injected did

> this work for me , but now i need to fo it manually.

There's an API called EM.joinTransaction(). With an application-managed

EM it's up to the application to call this to explicitly associate the EM with

the current transaction.

>

> Thank you very much.

ksaksa at 2007-7-12 15:35:26 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 6

Thank you again.

but still we have the issue here.

the issue that you need to create the factory each time the DAO is called.

This is very expensive.

i tried the jointransaction and didnt work for me.

did you tried it.

Thank you a lot.

you have a lot of knowledge.

AvihaiMara at 2007-7-12 15:35:26 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 7
> > i tried the jointransaction and didnt work for me.What happened when you called it? Was an exception thrown?
ksaksa at 2007-7-12 15:35:26 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 8

Yep, an exception was thrown.

i make a test in Pojo class that were called from stateless (with transaction - required) in the Pojo i ceate factory,

get em,

begin transaction

join....

in the begin i thought that automaticly the transaction will join to the stateless transaction , because i am in server side and in the persistence.xml the data source is JTA, but it didnt join.

than i call to join and get an exception.

AvihaiMara at 2007-7-12 15:35:26 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 9
Can you post the full stack trace and the code that creates the EM and calls joinTransaction?
ksaksa at 2007-7-12 15:35:26 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 10

Sure, thank you . i will do it tommorow.

what about the other question that i ask you.

it is too much expensive to create the factory each time.

i think on two ways:

1. bind the factory to the Jndi, when i create it at the first time.

i saw that the persistence. xml has a bind property to the jndi.

i still dont know how to this and if it will join the transaction automaticly like injection does.

2. create an interceptor that will set the persistence unit name before the injection - i dont know if it is possible,

thank you

AvihaiMara at 2007-7-12 15:35:26 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...