openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jeremy Bauer" <techhu...@gmail.com>
Subject Re: Getting the persistence unit name from an EntityManagerFactory
Date Tue, 06 May 2008 15:10:23 GMT
Hi Roger,

If you are able to use the OpenJPA interfaces directly (as opposed to
purely using the implementation agnostic JPA interfaces) you can use
the OpenJPA SPI to get the pu name from the emf.  Here is some sample
code:

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

        if (emf instanceof
org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI)
        {
          org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI ojemf =
            (org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI) emf;
          org.apache.openjpa.conf.OpenJPAConfiguration ojc =
ojemf.getConfiguration();
          System.out.println("pu name: " + ojc.getId());
        }

The doc for the getId method indicates that it may return the PU name
or some other more-unique value for the current environment.  A quick
scan showed that the id should == the pu name unless you are in a
distributed db/slice environment.  (I could have missed something
though)

If you need to find the pu name from an em you can get the OpenJPA emf
with this code:

        EntityManager em = emf.createEntityManager();
        org.apache.openjpa.persistence.OpenJPAEntityManager ojem =
            (org.apache.openjpa.persistence.OpenJPAEntityManager)em;
        org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI ojemf =
            (org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI)ojem.getEntityManagerFactory();

-Jeremy

On 5/4/08, roger.keays <roger.keays@ninthavenue.com.au> wrote:
>
> Does OpenJPA have any tricks we could use to get the persistence unit name
> which was used to create an EntityManager{Factory}?
>
> Cheers,
>
> Roger
> --
> View this message in context: http://www.nabble.com/Getting-the-persistence-unit-name-from-an-EntityManagerFactory-tp17044912p17044912.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>
>

Mime
View raw message