persistence@glassfish.java.net

Re: choosing persistence unit at runtime?

From: Jon Miller <jemiller_at_uchicago.edu>
Date: Tue, 27 Feb 2007 16:36:40 -0600

I'm not sure if this is what you are asking how to do, but, you can have
multiple persistence units in the same persistence.xml. In the example
below, there are two. Basically, all you would need to do is something like
the following. You can use the createEntityManagerFactory() method with the
Map to pass in additional properties. Any properties you pass in override
what is in the XML file. You can also do things like use the
"javax.persistence.provider" property to control which provider it is that
you want to use (haven't tested this myself).

I'm not sure what happens if you have multiple jar files each with a
META-INF/persistence.xml. Presumably, they get merged together and it would
work the same way as having multiple persistence units in the same config
file.

        if(/* conditional expression */) {
            entityManagerFactory =
Persistence.createEntityManagerFactory("PersistenceUnit1");
        }
        else {
            entityManagerFactory =
Persistence.createEntityManagerFactory("PersistenceUnit2");
        }


<!-- persistence.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
    <persistence-unit name="PersistenceUnit1"
transaction-type="RESOURCE_LOCAL">
        <provider>oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider</provider>
        <class>Class1</class>
        ...
        <properties>
            ...
        </properties>
    </persistence-unit>
    <persistence-unit name="PersistenceUnit2"
transaction-type="RESOURCE_LOCAL">
        <provider>oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider</provider>
        <class>Class10</class>
        ...
        <properties>
            ...
        </properties>
    </persistence-unit>
    ...
</persistence>

Jon

----- Original Message -----
From: "jeff" <jeffrey.blattman_at_yahoo.com>
To: <persistence_at_glassfish.dev.java.net>
Sent: Tuesday, February 27, 2007 4:02 PM
Subject: Re: choosing persistence unit at runtime?


> hi jon,
>
> okay, how do i do that? i see a version of createEntityManager() that
> takes a Map of "properties", but the javadocs don't say how i could use
> that to affect the selection of a particular persistence unit.
>
> Jon Miller <jemiller_at_uchicago.edu> wrote: Wouldn't you just select the one
> you want by name when you call
> Persistence.createEntityManager()?
>
> Jon
>
> ----- Original Message -----
> From: "Craig L Russell"
> To:
>
> Sent: Tuesday, February 27, 2007 3:43 PM
> Subject: Re: choosing persistence unit at runtime?
>
>
>> You might consider putting the persistence.xml file into a jar file
>> of its own (a different jar file for each persistence.xml you want).
>> Then, select the jar file (with its persistence.xml inside) by
>> prepending it to your class path.
>>
>> Craig
>>
>> On Feb 27, 2007, at 1:23 PM, jeff wrote:
>>
>>> if i am using java persistence stand alone, am i able to choose the
>>> persistence unit at runtime?
>>>
>>> i can't see how to do this, because it appears that the
>>> persistence.xml file is read from the classpath.
>>>
>>> any ideas?
>>> thanks.
>>>
>>>
>>>
>>> The fish are biting.
>>> Get more visitors on your site using Yahoo! Search Marketing.
>>
>> Craig Russell
>> Architect, Sun Java Enterprise System http://java.sun.com/products/jdo
>> 408 276-5638 mailto:Craig.Russell_at_sun.com
>> P.S. A good JDO? O, Gasp!
>>
>>
>
>
>
>
> ---------------------------------
> Access over 1 million songs - Yahoo! Music Unlimited.