You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@openjpa.apache.org by Erwan Le Goulven <er...@gmail.com> on 2007/04/06 13:46:17 UTC

Programmatical EntityManagerFactory init

Messieurs-dames,

As I could not find much clues about the right way to do it, here I come:
I'm trying to get an EntityManagerFactory properly setup from the
Persistence class, but I cannot get further than the following exception

javax.persistence.PersistenceException: No Persistence provider for
EntityManager named teamplay_test
    at javax.persistence.Persistence.createEntityManagerFactory(
Persistence.java:89)
    at com.gdteam.teamplay.domain.manager.TeamplayEMFactoryManager.get(
TeamplayEMFactoryManager.java:60)
    at com.gdteam.teamplay.test.persistence.PersistenceUtilTest.setUp(
PersistenceUtilTest.java:19)
    at junit.framework.TestCase.runBare(TestCase.java:125)
    at junit.framework.TestResult$1.protect(TestResult.java:106)
    at junit.framework.TestResult.runProtected(TestResult.java:124)
    at junit.framework.TestResult.run(TestResult.java:109)
    at junit.framework.TestCase.run(TestCase.java:118)
    at junit.framework.TestSuite.runTest(TestSuite.java:208)
    at junit.framework.TestSuite.run(TestSuite.java:203)

Here is the code part that leads to the exception:

public class TeamplayEMFactoryManager  {
...
    private final Map<String, String> props = new HashMap<String, String>();
    private final Map<String, EntityManagerFactory> map = new
HashMap<String, EntityManagerFactory>();
...
    private TeamplayEMFactoryManager() {
        props.put("openjpa.ConnectionDriverName", "org.hsqldb.jdbcDriver");
        props.put("openjpa.ConnectionUserName", "sa");
        props.put("openjpa.ConnectionPassword", "");
        props.put("openjpa.Log", "DefaultLevel=WARN, Tool=INFO");
        props.put("openjpa.MetaDataFactory", "jpa(Types="+buildTypes()+")");
    }

..

public EntityManagerFactory get(String url) {

        EntityManagerFactory factory = null;

        if (null == url || url.length() == 0 ) {
            url = "default";
        }

        factory = this.map.get(url);

        if (null == factory) {
            if ( "default".equals(url)) {
 1-               factory = Persistence.createEntityManagerFactory(null);
            } else {
                Map<String , String> map = new HashMap<String,
String>(props);
                map.put("openjpa.ConnectionURL", buildUrl(url));
 2-              factory = Persistence.createEntityManagerFactory(url,map);
            }
            this.map.put(url, factory);
        }
        return factory;
    }
...
}


Note :
I'm using a persistence.xml for single persitence unit creation, and this
test works fine. I've had a dive into the source code, and found the
provider is lookuped and found by the glassfish persistence implementation
class. The exception is raised on the open jpa side, and I know I'm missing
some configuration elements, but I cannot guess what's missing?
any hint?

thank you guys

Re: Programmatical EntityManagerFactory init

Posted by Erwan Le Goulven <er...@gmail.com>.
Well thanks for this piece of inf, Patrick, I'll go with my impl.

Keep on opening JPA !

2007/4/6, Patrick Linskey <pl...@bea.com>:
>
> Sorry, a correction. If you want to avoid the XML, you can't use the
> PersistenceProvider.createEntityManagerFactory(String,Map) signature.
> Instead, you must use
> PersistenceProvider.createContainerEntityManagerFactory(PersistenceUnitI
> nfo,Map).
>
> One thing to bear in mind is that OpenJPA may optimize default
> configuration options based on being invoked via createEMF() vs.
> createContainerEMF(). For example, OpenJPA changes the BrokerImpl
> default to be a non-finalizing Broker implementation when invoked via
> createContainerEMF(), based on the assumption that anyone using
> createContainerEMF() is going to be a good citizen and clean up
> EntityManagers responsibly. See
> http://svn.apache.org/repos/asf/incubator/openjpa/trunk/openjpa-persiste
> nce/src/main/java/org/apache/openjpa/persistence/PersistenceProviderImpl
> .java for details.
>
> -Patrick
>
> --
> Patrick Linskey
> BEA Systems, Inc.
>
> _______________________________________________________________________
> Notice:  This email message, together with any attachments, may contain
> information  of  BEA Systems,  Inc.,  its subsidiaries  and  affiliated
> entities,  that may be confidential,  proprietary,  copyrighted  and/or
> legally privileged, and is intended solely for the use of the individual
> or entity named in this message. If you are not the intended recipient,
> and have received this message in error, please immediately return this
> by email and then delete it.
>
> > -----Original Message-----
> > From: Patrick Linskey
> > Sent: Friday, April 06, 2007 10:08 AM
> > To: open-jpa-dev@incubator.apache.org
> > Subject: RE: Programmatical EntityManagerFactory init
> >
> > > Could you (or anyone else) confirm this last point?
> >
> > Your are correct -- when using the Persistence class for
> > bootstrapping,
> > you must provide a name of a persistence unit defined in XML.
> >
> > To create an EMF programmatically in a standards-based way, you should
> > use the classes and interfaces in javax.persistence.spi. Create an
> > implementation of PersistenceUnitInfo
> > (http://java.sun.com/javaee/5/docs/api/javax/persistence/spi/P
> > ersistence
> > UnitInfo.html), then instantiate the PersistenceProvider for the
> > implementation of your choice (this is the bit that goes into the
> > <provider> tag in the persistence.xml file).
> >
> > So, for OpenJPA, you would create your PersistenceUnitInfo,
> > and then do
> > something like so:
> >
> > PersistenceUnitInfo pui = ...;
> > PersistenceProvider pp = new
> > org.apache.openjpa.persistence.PersistenceProviderImpl();
> > EntityManagerFactory emf = pp.createEntityManagerFactory("foo", null);
> >
> > The above code assumes that you've configured all your
> > properties in the
> > PersistenceUnitInfo, and so is passing 'null' in as the map
> > parameter to
> > createEntityManagerFactory().
> >
> > If you don't want to implement the PersistenceUnitInfo
> > interface on your
> > own, you can use OpenJPA's (which is pretty general-purpose, but does
> > have some OpenJPA-specific helper methods in it):
> >
> > PersistenceUnitInfo pui = new
> > org.apache.openjpa.persistence.PersistenceUnitInfoImpl();
> > pui.setPersistenceUnitName("foo");
> > pui.setProperty("key", "value");
> >
> > HTH,
> >
> > -Patrick
> >
> > --
> > Patrick Linskey
> > BEA Systems, Inc.
> >
> > ______________________________________________________________
> > _________
> > Notice:  This email message, together with any attachments,
> > may contain
> > information  of  BEA Systems,  Inc.,  its subsidiaries  and
> > affiliated
> > entities,  that may be confidential,  proprietary,
> > copyrighted  and/or
> > legally privileged, and is intended solely for the use of the
> > individual
> > or entity named in this message. If you are not the intended
> > recipient,
> > and have received this message in error, please immediately
> > return this
> > by email and then delete it.
> >
> > > -----Original Message-----
> > > From: Erwan Le Goulven [mailto:erwan.legoulven@gmail.com]
> > > Sent: Friday, April 06, 2007 9:50 AM
> > > To: open-jpa-dev@incubator.apache.org
> > > Subject: Re: Programmatical EntityManagerFactory init
> > >
> > > Thank you for your time Patrik,
> > >
> > > Actually the persistence.xml used in the META-INF jar in the
> > > CP points to
> > > another persistence unit name.
> > > The thing is -and I may be mistaking about this practice- I want to
> > > programmatically my own persistence units, based on different
> > > name, at run
> > > time, and I dunnot wish do depend on persistence.xml
> > > descriptor to configure
> > > the persistence units I wish to use, because :
> > > -I don't know their number
> > > -I don't know their name
> > >
> > > I thought I could use the Map property provided by the
> > > Persistence api in
> > > order to proceed, but I'm now questionning about its
> > faisability. I've
> > > checked out the source code and noticed the persistence.xml
> > > resource is
> > > searched, whatever property I put in the configuration map.
> > >
> > > Could you (or anyone else) confirm this last point?
> > >
> > > Merci bien
> > > Erwan
> > > 2007/4/6, Patrick Linskey <pl...@bea.com>:
> > > >
> > > > > Note :
> > > > > I'm using a persistence.xml for single persitence unit
> > > > > creation, and this
> > > > > test works fine. I've had a dive into the source code,
> > > and found the
> > > > > provider is lookuped and found by the glassfish persistence
> > > > > implementation
> > > > > class. The exception is raised on the open jpa side, and I
> > > > > know I'm missing
> > > > > some configuration elements, but I cannot guess what's missing?
> > > > > any hint?
> > > >
> > > > The only exception that I see in the trace you gave us is
> > > thrown by the
> > > > spec implementation jar. I'm not looking at the source for
> > > that code,
> > > > but my guess would be that the failure is happening because of a
> > > > classpath error, such that OpenJPA is not available in the
> > > classloader
> > > > that is visible to the Persistence class.
> > > >
> > > > Could you post your persistence.xml file and any details
> > > about how your
> > > > classloader structure is set up?
> > > >
> > > > Thanks,
> > > >
> > > > -Patrick
> > > >
> > > > --
> > > > Patrick Linskey
> > > > BEA Systems, Inc.
> > > >
> > > >
> > > ______________________________________________________________
> > > _________
> > > > Notice:  This email message, together with any attachments,
> > > may contain
> > > > information  of  BEA Systems,  Inc.,  its subsidiaries  and
> > >  affiliated
> > > > entities,  that may be confidential,  proprietary,
> > > copyrighted  and/or
> > > > legally privileged, and is intended solely for the use of
> > > the individual
> > > > or entity named in this message. If you are not the
> > > intended recipient,
> > > > and have received this message in error, please immediately
> > > return this
> > > > by email and then delete it.
> > > >
> > > > > -----Original Message-----
> > > > > From: Erwan Le Goulven [mailto:erwan.legoulven@gmail.com]
> > > > > Sent: Friday, April 06, 2007 4:46 AM
> > > > > To: open-jpa-dev@incubator.apache.org
> > > > > Subject: Programmatical EntityManagerFactory init
> > > > >
> > > > > Messieurs-dames,
> > > > >
> > > > > As I could not find much clues about the right way to do it,
> > > > > here I come:
> > > > > I'm trying to get an EntityManagerFactory properly
> > setup from the
> > > > > Persistence class, but I cannot get further than the
> > > > > following exception
> > > > >
> > > > > javax.persistence.PersistenceException: No Persistence
> > > provider for
> > > > > EntityManager named teamplay_test
> > > > >     at javax.persistence.Persistence.createEntityManagerFactory(
> > > > > Persistence.java:89)
> > > > >     at
> > > > > com.gdteam.teamplay.domain.manager.TeamplayEMFactoryManager.get(
> > > > > TeamplayEMFactoryManager.java:60)
> > > > >     at
> > > com.gdteam.teamplay.test.persistence.PersistenceUtilTest.setUp(
> > > > > PersistenceUtilTest.java:19)
> > > > >     at junit.framework.TestCase.runBare(TestCase.java:125)
> > > > >     at junit.framework.TestResult$1.protect(TestResult.java:106)
> > > > >     at
> > > junit.framework.TestResult.runProtected(TestResult.java:124)
> > > > >     at junit.framework.TestResult.run(TestResult.java:109)
> > > > >     at junit.framework.TestCase.run(TestCase.java:118)
> > > > >     at junit.framework.TestSuite.runTest(TestSuite.java:208)
> > > > >     at junit.framework.TestSuite.run(TestSuite.java:203)
> > > > >
> > > > > Here is the code part that leads to the exception:
> > > > >
> > > > > public class TeamplayEMFactoryManager  {
> > > > > ...
> > > > >     private final Map<String, String> props = new
> > > > > HashMap<String, String>();
> > > > >     private final Map<String, EntityManagerFactory> map = new
> > > > > HashMap<String, EntityManagerFactory>();
> > > > > ...
> > > > >     private TeamplayEMFactoryManager() {
> > > > >         props.put("openjpa.ConnectionDriverName",
> > > > > "org.hsqldb.jdbcDriver");
> > > > >         props.put("openjpa.ConnectionUserName", "sa");
> > > > >         props.put("openjpa.ConnectionPassword", "");
> > > > >         props.put("openjpa.Log", "DefaultLevel=WARN,
> > Tool=INFO");
> > > > >         props.put("openjpa.MetaDataFactory",
> > > > > "jpa(Types="+buildTypes()+")");
> > > > >     }
> > > > >
> > > > > ..
> > > > >
> > > > > public EntityManagerFactory get(String url) {
> > > > >
> > > > >         EntityManagerFactory factory = null;
> > > > >
> > > > >         if (null == url || url.length() == 0 ) {
> > > > >             url = "default";
> > > > >         }
> > > > >
> > > > >         factory = this.map.get(url);
> > > > >
> > > > >         if (null == factory) {
> > > > >             if ( "default".equals(url)) {
> > > > >  1-               factory =
> > > > > Persistence.createEntityManagerFactory(null);
> > > > >             } else {
> > > > >                 Map<String , String> map = new HashMap<String,
> > > > > String>(props);
> > > > >                 map.put("openjpa.ConnectionURL", buildUrl(url));
> > > > >  2-              factory =
> > > > > Persistence.createEntityManagerFactory(url,map);
> > > > >             }
> > > > >             this.map.put(url, factory);
> > > > >         }
> > > > >         return factory;
> > > > >     }
> > > > > ...
> > > > > }
> > > > >
> > > > >
> > > > > Note :
> > > > > I'm using a persistence.xml for single persitence unit
> > > > > creation, and this
> > > > > test works fine. I've had a dive into the source code,
> > > and found the
> > > > > provider is lookuped and found by the glassfish persistence
> > > > > implementation
> > > > > class. The exception is raised on the open jpa side, and I
> > > > > know I'm missing
> > > > > some configuration elements, but I cannot guess what's missing?
> > > > > any hint?
> > > > >
> > > > > thank you guys
> > > > >
> > > >
> > > > Notice:  This email message, together with any attachments,
> > > may contain
> > > > information  of  BEA Systems,  Inc.,  its subsidiaries  and
> > >  affiliated
> > > > entities,  that may be confidential,  proprietary,
> > > copyrighted  and/or
> > > > legally privileged, and is intended solely for the use of
> > > the individual or
> > > > entity named in this message. If you are not the intended
> > > recipient, and
> > > > have received this message in error, please immediately
> > > return this by email
> > > > and then delete it.
> > > >
> > >
> >
> > Notice:  This email message, together with any attachments,
> > may contain information  of  BEA Systems,  Inc.,  its
> > subsidiaries  and  affiliated entities,  that may be
> > confidential,  proprietary,  copyrighted  and/or legally
> > privileged, and is intended solely for the use of the
> > individual or entity named in this message. If you are not
> > the intended recipient, and have received this message in
> > error, please immediately return this by email and then delete it.
> >
>
> Notice:  This email message, together with any attachments, may contain
> information  of  BEA Systems,  Inc.,  its subsidiaries  and  affiliated
> entities,  that may be confidential,  proprietary,  copyrighted  and/or
> legally privileged, and is intended solely for the use of the individual or
> entity named in this message. If you are not the intended recipient, and
> have received this message in error, please immediately return this by email
> and then delete it.
>

RE: Programmatical EntityManagerFactory init

Posted by Patrick Linskey <pl...@bea.com>.
Sorry, a correction. If you want to avoid the XML, you can't use the
PersistenceProvider.createEntityManagerFactory(String,Map) signature.
Instead, you must use
PersistenceProvider.createContainerEntityManagerFactory(PersistenceUnitI
nfo,Map).

One thing to bear in mind is that OpenJPA may optimize default
configuration options based on being invoked via createEMF() vs.
createContainerEMF(). For example, OpenJPA changes the BrokerImpl
default to be a non-finalizing Broker implementation when invoked via
createContainerEMF(), based on the assumption that anyone using
createContainerEMF() is going to be a good citizen and clean up
EntityManagers responsibly. See
http://svn.apache.org/repos/asf/incubator/openjpa/trunk/openjpa-persiste
nce/src/main/java/org/apache/openjpa/persistence/PersistenceProviderImpl
.java for details.

-Patrick

-- 
Patrick Linskey
BEA Systems, Inc. 

_______________________________________________________________________
Notice:  This email message, together with any attachments, may contain
information  of  BEA Systems,  Inc.,  its subsidiaries  and  affiliated
entities,  that may be confidential,  proprietary,  copyrighted  and/or
legally privileged, and is intended solely for the use of the individual
or entity named in this message. If you are not the intended recipient,
and have received this message in error, please immediately return this
by email and then delete it. 

> -----Original Message-----
> From: Patrick Linskey 
> Sent: Friday, April 06, 2007 10:08 AM
> To: open-jpa-dev@incubator.apache.org
> Subject: RE: Programmatical EntityManagerFactory init
> 
> > Could you (or anyone else) confirm this last point?
> 
> Your are correct -- when using the Persistence class for 
> bootstrapping,
> you must provide a name of a persistence unit defined in XML.
> 
> To create an EMF programmatically in a standards-based way, you should
> use the classes and interfaces in javax.persistence.spi. Create an
> implementation of PersistenceUnitInfo
> (http://java.sun.com/javaee/5/docs/api/javax/persistence/spi/P
> ersistence
> UnitInfo.html), then instantiate the PersistenceProvider for the
> implementation of your choice (this is the bit that goes into the
> <provider> tag in the persistence.xml file).
> 
> So, for OpenJPA, you would create your PersistenceUnitInfo, 
> and then do
> something like so:
> 
> PersistenceUnitInfo pui = ...;
> PersistenceProvider pp = new
> org.apache.openjpa.persistence.PersistenceProviderImpl();
> EntityManagerFactory emf = pp.createEntityManagerFactory("foo", null);
> 
> The above code assumes that you've configured all your 
> properties in the
> PersistenceUnitInfo, and so is passing 'null' in as the map 
> parameter to
> createEntityManagerFactory().
> 
> If you don't want to implement the PersistenceUnitInfo 
> interface on your
> own, you can use OpenJPA's (which is pretty general-purpose, but does
> have some OpenJPA-specific helper methods in it):
> 
> PersistenceUnitInfo pui = new
> org.apache.openjpa.persistence.PersistenceUnitInfoImpl();
> pui.setPersistenceUnitName("foo");
> pui.setProperty("key", "value");
> 
> HTH,
> 
> -Patrick
> 
> -- 
> Patrick Linskey
> BEA Systems, Inc. 
> 
> ______________________________________________________________
> _________
> Notice:  This email message, together with any attachments, 
> may contain
> information  of  BEA Systems,  Inc.,  its subsidiaries  and  
> affiliated
> entities,  that may be confidential,  proprietary,  
> copyrighted  and/or
> legally privileged, and is intended solely for the use of the 
> individual
> or entity named in this message. If you are not the intended 
> recipient,
> and have received this message in error, please immediately 
> return this
> by email and then delete it. 
> 
> > -----Original Message-----
> > From: Erwan Le Goulven [mailto:erwan.legoulven@gmail.com] 
> > Sent: Friday, April 06, 2007 9:50 AM
> > To: open-jpa-dev@incubator.apache.org
> > Subject: Re: Programmatical EntityManagerFactory init
> > 
> > Thank you for your time Patrik,
> > 
> > Actually the persistence.xml used in the META-INF jar in the 
> > CP points to
> > another persistence unit name.
> > The thing is -and I may be mistaking about this practice- I want to
> > programmatically my own persistence units, based on different 
> > name, at run
> > time, and I dunnot wish do depend on persistence.xml 
> > descriptor to configure
> > the persistence units I wish to use, because :
> > -I don't know their number
> > -I don't know their name
> > 
> > I thought I could use the Map property provided by the 
> > Persistence api in
> > order to proceed, but I'm now questionning about its 
> faisability. I've
> > checked out the source code and noticed the persistence.xml 
> > resource is
> > searched, whatever property I put in the configuration map.
> > 
> > Could you (or anyone else) confirm this last point?
> > 
> > Merci bien
> > Erwan
> > 2007/4/6, Patrick Linskey <pl...@bea.com>:
> > >
> > > > Note :
> > > > I'm using a persistence.xml for single persitence unit
> > > > creation, and this
> > > > test works fine. I've had a dive into the source code, 
> > and found the
> > > > provider is lookuped and found by the glassfish persistence
> > > > implementation
> > > > class. The exception is raised on the open jpa side, and I
> > > > know I'm missing
> > > > some configuration elements, but I cannot guess what's missing?
> > > > any hint?
> > >
> > > The only exception that I see in the trace you gave us is 
> > thrown by the
> > > spec implementation jar. I'm not looking at the source for 
> > that code,
> > > but my guess would be that the failure is happening because of a
> > > classpath error, such that OpenJPA is not available in the 
> > classloader
> > > that is visible to the Persistence class.
> > >
> > > Could you post your persistence.xml file and any details 
> > about how your
> > > classloader structure is set up?
> > >
> > > Thanks,
> > >
> > > -Patrick
> > >
> > > --
> > > Patrick Linskey
> > > BEA Systems, Inc.
> > >
> > > 
> > ______________________________________________________________
> > _________
> > > Notice:  This email message, together with any attachments, 
> > may contain
> > > information  of  BEA Systems,  Inc.,  its subsidiaries  and 
> >  affiliated
> > > entities,  that may be confidential,  proprietary,  
> > copyrighted  and/or
> > > legally privileged, and is intended solely for the use of 
> > the individual
> > > or entity named in this message. If you are not the 
> > intended recipient,
> > > and have received this message in error, please immediately 
> > return this
> > > by email and then delete it.
> > >
> > > > -----Original Message-----
> > > > From: Erwan Le Goulven [mailto:erwan.legoulven@gmail.com]
> > > > Sent: Friday, April 06, 2007 4:46 AM
> > > > To: open-jpa-dev@incubator.apache.org
> > > > Subject: Programmatical EntityManagerFactory init
> > > >
> > > > Messieurs-dames,
> > > >
> > > > As I could not find much clues about the right way to do it,
> > > > here I come:
> > > > I'm trying to get an EntityManagerFactory properly 
> setup from the
> > > > Persistence class, but I cannot get further than the
> > > > following exception
> > > >
> > > > javax.persistence.PersistenceException: No Persistence 
> > provider for
> > > > EntityManager named teamplay_test
> > > >     at javax.persistence.Persistence.createEntityManagerFactory(
> > > > Persistence.java:89)
> > > >     at
> > > > com.gdteam.teamplay.domain.manager.TeamplayEMFactoryManager.get(
> > > > TeamplayEMFactoryManager.java:60)
> > > >     at 
> > com.gdteam.teamplay.test.persistence.PersistenceUtilTest.setUp(
> > > > PersistenceUtilTest.java:19)
> > > >     at junit.framework.TestCase.runBare(TestCase.java:125)
> > > >     at junit.framework.TestResult$1.protect(TestResult.java:106)
> > > >     at 
> > junit.framework.TestResult.runProtected(TestResult.java:124)
> > > >     at junit.framework.TestResult.run(TestResult.java:109)
> > > >     at junit.framework.TestCase.run(TestCase.java:118)
> > > >     at junit.framework.TestSuite.runTest(TestSuite.java:208)
> > > >     at junit.framework.TestSuite.run(TestSuite.java:203)
> > > >
> > > > Here is the code part that leads to the exception:
> > > >
> > > > public class TeamplayEMFactoryManager  {
> > > > ...
> > > >     private final Map<String, String> props = new
> > > > HashMap<String, String>();
> > > >     private final Map<String, EntityManagerFactory> map = new
> > > > HashMap<String, EntityManagerFactory>();
> > > > ...
> > > >     private TeamplayEMFactoryManager() {
> > > >         props.put("openjpa.ConnectionDriverName",
> > > > "org.hsqldb.jdbcDriver");
> > > >         props.put("openjpa.ConnectionUserName", "sa");
> > > >         props.put("openjpa.ConnectionPassword", "");
> > > >         props.put("openjpa.Log", "DefaultLevel=WARN, 
> Tool=INFO");
> > > >         props.put("openjpa.MetaDataFactory",
> > > > "jpa(Types="+buildTypes()+")");
> > > >     }
> > > >
> > > > ..
> > > >
> > > > public EntityManagerFactory get(String url) {
> > > >
> > > >         EntityManagerFactory factory = null;
> > > >
> > > >         if (null == url || url.length() == 0 ) {
> > > >             url = "default";
> > > >         }
> > > >
> > > >         factory = this.map.get(url);
> > > >
> > > >         if (null == factory) {
> > > >             if ( "default".equals(url)) {
> > > >  1-               factory =
> > > > Persistence.createEntityManagerFactory(null);
> > > >             } else {
> > > >                 Map<String , String> map = new HashMap<String,
> > > > String>(props);
> > > >                 map.put("openjpa.ConnectionURL", buildUrl(url));
> > > >  2-              factory =
> > > > Persistence.createEntityManagerFactory(url,map);
> > > >             }
> > > >             this.map.put(url, factory);
> > > >         }
> > > >         return factory;
> > > >     }
> > > > ...
> > > > }
> > > >
> > > >
> > > > Note :
> > > > I'm using a persistence.xml for single persitence unit
> > > > creation, and this
> > > > test works fine. I've had a dive into the source code, 
> > and found the
> > > > provider is lookuped and found by the glassfish persistence
> > > > implementation
> > > > class. The exception is raised on the open jpa side, and I
> > > > know I'm missing
> > > > some configuration elements, but I cannot guess what's missing?
> > > > any hint?
> > > >
> > > > thank you guys
> > > >
> > >
> > > Notice:  This email message, together with any attachments, 
> > may contain
> > > information  of  BEA Systems,  Inc.,  its subsidiaries  and 
> >  affiliated
> > > entities,  that may be confidential,  proprietary,  
> > copyrighted  and/or
> > > legally privileged, and is intended solely for the use of 
> > the individual or
> > > entity named in this message. If you are not the intended 
> > recipient, and
> > > have received this message in error, please immediately 
> > return this by email
> > > and then delete it.
> > >
> > 
> 
> Notice:  This email message, together with any attachments, 
> may contain information  of  BEA Systems,  Inc.,  its 
> subsidiaries  and  affiliated entities,  that may be 
> confidential,  proprietary,  copyrighted  and/or legally 
> privileged, and is intended solely for the use of the 
> individual or entity named in this message. If you are not 
> the intended recipient, and have received this message in 
> error, please immediately return this by email and then delete it.
> 

Notice:  This email message, together with any attachments, may contain information  of  BEA Systems,  Inc.,  its subsidiaries  and  affiliated entities,  that may be confidential,  proprietary,  copyrighted  and/or legally privileged, and is intended solely for the use of the individual or entity named in this message. If you are not the intended recipient, and have received this message in error, please immediately return this by email and then delete it.

RE: Programmatical EntityManagerFactory init

Posted by Patrick Linskey <pl...@bea.com>.
> Could you (or anyone else) confirm this last point?

Your are correct -- when using the Persistence class for bootstrapping,
you must provide a name of a persistence unit defined in XML.

To create an EMF programmatically in a standards-based way, you should
use the classes and interfaces in javax.persistence.spi. Create an
implementation of PersistenceUnitInfo
(http://java.sun.com/javaee/5/docs/api/javax/persistence/spi/Persistence
UnitInfo.html), then instantiate the PersistenceProvider for the
implementation of your choice (this is the bit that goes into the
<provider> tag in the persistence.xml file).

So, for OpenJPA, you would create your PersistenceUnitInfo, and then do
something like so:

PersistenceUnitInfo pui = ...;
PersistenceProvider pp = new
org.apache.openjpa.persistence.PersistenceProviderImpl();
EntityManagerFactory emf = pp.createEntityManagerFactory("foo", null);

The above code assumes that you've configured all your properties in the
PersistenceUnitInfo, and so is passing 'null' in as the map parameter to
createEntityManagerFactory().

If you don't want to implement the PersistenceUnitInfo interface on your
own, you can use OpenJPA's (which is pretty general-purpose, but does
have some OpenJPA-specific helper methods in it):

PersistenceUnitInfo pui = new
org.apache.openjpa.persistence.PersistenceUnitInfoImpl();
pui.setPersistenceUnitName("foo");
pui.setProperty("key", "value");

HTH,

-Patrick

-- 
Patrick Linskey
BEA Systems, Inc. 

_______________________________________________________________________
Notice:  This email message, together with any attachments, may contain
information  of  BEA Systems,  Inc.,  its subsidiaries  and  affiliated
entities,  that may be confidential,  proprietary,  copyrighted  and/or
legally privileged, and is intended solely for the use of the individual
or entity named in this message. If you are not the intended recipient,
and have received this message in error, please immediately return this
by email and then delete it. 

> -----Original Message-----
> From: Erwan Le Goulven [mailto:erwan.legoulven@gmail.com] 
> Sent: Friday, April 06, 2007 9:50 AM
> To: open-jpa-dev@incubator.apache.org
> Subject: Re: Programmatical EntityManagerFactory init
> 
> Thank you for your time Patrik,
> 
> Actually the persistence.xml used in the META-INF jar in the 
> CP points to
> another persistence unit name.
> The thing is -and I may be mistaking about this practice- I want to
> programmatically my own persistence units, based on different 
> name, at run
> time, and I dunnot wish do depend on persistence.xml 
> descriptor to configure
> the persistence units I wish to use, because :
> -I don't know their number
> -I don't know their name
> 
> I thought I could use the Map property provided by the 
> Persistence api in
> order to proceed, but I'm now questionning about its faisability. I've
> checked out the source code and noticed the persistence.xml 
> resource is
> searched, whatever property I put in the configuration map.
> 
> Could you (or anyone else) confirm this last point?
> 
> Merci bien
> Erwan
> 2007/4/6, Patrick Linskey <pl...@bea.com>:
> >
> > > Note :
> > > I'm using a persistence.xml for single persitence unit
> > > creation, and this
> > > test works fine. I've had a dive into the source code, 
> and found the
> > > provider is lookuped and found by the glassfish persistence
> > > implementation
> > > class. The exception is raised on the open jpa side, and I
> > > know I'm missing
> > > some configuration elements, but I cannot guess what's missing?
> > > any hint?
> >
> > The only exception that I see in the trace you gave us is 
> thrown by the
> > spec implementation jar. I'm not looking at the source for 
> that code,
> > but my guess would be that the failure is happening because of a
> > classpath error, such that OpenJPA is not available in the 
> classloader
> > that is visible to the Persistence class.
> >
> > Could you post your persistence.xml file and any details 
> about how your
> > classloader structure is set up?
> >
> > Thanks,
> >
> > -Patrick
> >
> > --
> > Patrick Linskey
> > BEA Systems, Inc.
> >
> > 
> ______________________________________________________________
> _________
> > Notice:  This email message, together with any attachments, 
> may contain
> > information  of  BEA Systems,  Inc.,  its subsidiaries  and 
>  affiliated
> > entities,  that may be confidential,  proprietary,  
> copyrighted  and/or
> > legally privileged, and is intended solely for the use of 
> the individual
> > or entity named in this message. If you are not the 
> intended recipient,
> > and have received this message in error, please immediately 
> return this
> > by email and then delete it.
> >
> > > -----Original Message-----
> > > From: Erwan Le Goulven [mailto:erwan.legoulven@gmail.com]
> > > Sent: Friday, April 06, 2007 4:46 AM
> > > To: open-jpa-dev@incubator.apache.org
> > > Subject: Programmatical EntityManagerFactory init
> > >
> > > Messieurs-dames,
> > >
> > > As I could not find much clues about the right way to do it,
> > > here I come:
> > > I'm trying to get an EntityManagerFactory properly setup from the
> > > Persistence class, but I cannot get further than the
> > > following exception
> > >
> > > javax.persistence.PersistenceException: No Persistence 
> provider for
> > > EntityManager named teamplay_test
> > >     at javax.persistence.Persistence.createEntityManagerFactory(
> > > Persistence.java:89)
> > >     at
> > > com.gdteam.teamplay.domain.manager.TeamplayEMFactoryManager.get(
> > > TeamplayEMFactoryManager.java:60)
> > >     at 
> com.gdteam.teamplay.test.persistence.PersistenceUtilTest.setUp(
> > > PersistenceUtilTest.java:19)
> > >     at junit.framework.TestCase.runBare(TestCase.java:125)
> > >     at junit.framework.TestResult$1.protect(TestResult.java:106)
> > >     at 
> junit.framework.TestResult.runProtected(TestResult.java:124)
> > >     at junit.framework.TestResult.run(TestResult.java:109)
> > >     at junit.framework.TestCase.run(TestCase.java:118)
> > >     at junit.framework.TestSuite.runTest(TestSuite.java:208)
> > >     at junit.framework.TestSuite.run(TestSuite.java:203)
> > >
> > > Here is the code part that leads to the exception:
> > >
> > > public class TeamplayEMFactoryManager  {
> > > ...
> > >     private final Map<String, String> props = new
> > > HashMap<String, String>();
> > >     private final Map<String, EntityManagerFactory> map = new
> > > HashMap<String, EntityManagerFactory>();
> > > ...
> > >     private TeamplayEMFactoryManager() {
> > >         props.put("openjpa.ConnectionDriverName",
> > > "org.hsqldb.jdbcDriver");
> > >         props.put("openjpa.ConnectionUserName", "sa");
> > >         props.put("openjpa.ConnectionPassword", "");
> > >         props.put("openjpa.Log", "DefaultLevel=WARN, Tool=INFO");
> > >         props.put("openjpa.MetaDataFactory",
> > > "jpa(Types="+buildTypes()+")");
> > >     }
> > >
> > > ..
> > >
> > > public EntityManagerFactory get(String url) {
> > >
> > >         EntityManagerFactory factory = null;
> > >
> > >         if (null == url || url.length() == 0 ) {
> > >             url = "default";
> > >         }
> > >
> > >         factory = this.map.get(url);
> > >
> > >         if (null == factory) {
> > >             if ( "default".equals(url)) {
> > >  1-               factory =
> > > Persistence.createEntityManagerFactory(null);
> > >             } else {
> > >                 Map<String , String> map = new HashMap<String,
> > > String>(props);
> > >                 map.put("openjpa.ConnectionURL", buildUrl(url));
> > >  2-              factory =
> > > Persistence.createEntityManagerFactory(url,map);
> > >             }
> > >             this.map.put(url, factory);
> > >         }
> > >         return factory;
> > >     }
> > > ...
> > > }
> > >
> > >
> > > Note :
> > > I'm using a persistence.xml for single persitence unit
> > > creation, and this
> > > test works fine. I've had a dive into the source code, 
> and found the
> > > provider is lookuped and found by the glassfish persistence
> > > implementation
> > > class. The exception is raised on the open jpa side, and I
> > > know I'm missing
> > > some configuration elements, but I cannot guess what's missing?
> > > any hint?
> > >
> > > thank you guys
> > >
> >
> > Notice:  This email message, together with any attachments, 
> may contain
> > information  of  BEA Systems,  Inc.,  its subsidiaries  and 
>  affiliated
> > entities,  that may be confidential,  proprietary,  
> copyrighted  and/or
> > legally privileged, and is intended solely for the use of 
> the individual or
> > entity named in this message. If you are not the intended 
> recipient, and
> > have received this message in error, please immediately 
> return this by email
> > and then delete it.
> >
> 

Notice:  This email message, together with any attachments, may contain information  of  BEA Systems,  Inc.,  its subsidiaries  and  affiliated entities,  that may be confidential,  proprietary,  copyrighted  and/or legally privileged, and is intended solely for the use of the individual or entity named in this message. If you are not the intended recipient, and have received this message in error, please immediately return this by email and then delete it.

Re: Programmatical EntityManagerFactory init

Posted by Erwan Le Goulven <er...@gmail.com>.
Thank you for your time Patrik,

Actually the persistence.xml used in the META-INF jar in the CP points to
another persistence unit name.
The thing is -and I may be mistaking about this practice- I want to
programmatically my own persistence units, based on different name, at run
time, and I dunnot wish do depend on persistence.xml descriptor to configure
the persistence units I wish to use, because :
-I don't know their number
-I don't know their name

I thought I could use the Map property provided by the Persistence api in
order to proceed, but I'm now questionning about its faisability. I've
checked out the source code and noticed the persistence.xml resource is
searched, whatever property I put in the configuration map.

Could you (or anyone else) confirm this last point?

Merci bien
Erwan
2007/4/6, Patrick Linskey <pl...@bea.com>:
>
> > Note :
> > I'm using a persistence.xml for single persitence unit
> > creation, and this
> > test works fine. I've had a dive into the source code, and found the
> > provider is lookuped and found by the glassfish persistence
> > implementation
> > class. The exception is raised on the open jpa side, and I
> > know I'm missing
> > some configuration elements, but I cannot guess what's missing?
> > any hint?
>
> The only exception that I see in the trace you gave us is thrown by the
> spec implementation jar. I'm not looking at the source for that code,
> but my guess would be that the failure is happening because of a
> classpath error, such that OpenJPA is not available in the classloader
> that is visible to the Persistence class.
>
> Could you post your persistence.xml file and any details about how your
> classloader structure is set up?
>
> Thanks,
>
> -Patrick
>
> --
> Patrick Linskey
> BEA Systems, Inc.
>
> _______________________________________________________________________
> Notice:  This email message, together with any attachments, may contain
> information  of  BEA Systems,  Inc.,  its subsidiaries  and  affiliated
> entities,  that may be confidential,  proprietary,  copyrighted  and/or
> legally privileged, and is intended solely for the use of the individual
> or entity named in this message. If you are not the intended recipient,
> and have received this message in error, please immediately return this
> by email and then delete it.
>
> > -----Original Message-----
> > From: Erwan Le Goulven [mailto:erwan.legoulven@gmail.com]
> > Sent: Friday, April 06, 2007 4:46 AM
> > To: open-jpa-dev@incubator.apache.org
> > Subject: Programmatical EntityManagerFactory init
> >
> > Messieurs-dames,
> >
> > As I could not find much clues about the right way to do it,
> > here I come:
> > I'm trying to get an EntityManagerFactory properly setup from the
> > Persistence class, but I cannot get further than the
> > following exception
> >
> > javax.persistence.PersistenceException: No Persistence provider for
> > EntityManager named teamplay_test
> >     at javax.persistence.Persistence.createEntityManagerFactory(
> > Persistence.java:89)
> >     at
> > com.gdteam.teamplay.domain.manager.TeamplayEMFactoryManager.get(
> > TeamplayEMFactoryManager.java:60)
> >     at com.gdteam.teamplay.test.persistence.PersistenceUtilTest.setUp(
> > PersistenceUtilTest.java:19)
> >     at junit.framework.TestCase.runBare(TestCase.java:125)
> >     at junit.framework.TestResult$1.protect(TestResult.java:106)
> >     at junit.framework.TestResult.runProtected(TestResult.java:124)
> >     at junit.framework.TestResult.run(TestResult.java:109)
> >     at junit.framework.TestCase.run(TestCase.java:118)
> >     at junit.framework.TestSuite.runTest(TestSuite.java:208)
> >     at junit.framework.TestSuite.run(TestSuite.java:203)
> >
> > Here is the code part that leads to the exception:
> >
> > public class TeamplayEMFactoryManager  {
> > ...
> >     private final Map<String, String> props = new
> > HashMap<String, String>();
> >     private final Map<String, EntityManagerFactory> map = new
> > HashMap<String, EntityManagerFactory>();
> > ...
> >     private TeamplayEMFactoryManager() {
> >         props.put("openjpa.ConnectionDriverName",
> > "org.hsqldb.jdbcDriver");
> >         props.put("openjpa.ConnectionUserName", "sa");
> >         props.put("openjpa.ConnectionPassword", "");
> >         props.put("openjpa.Log", "DefaultLevel=WARN, Tool=INFO");
> >         props.put("openjpa.MetaDataFactory",
> > "jpa(Types="+buildTypes()+")");
> >     }
> >
> > ..
> >
> > public EntityManagerFactory get(String url) {
> >
> >         EntityManagerFactory factory = null;
> >
> >         if (null == url || url.length() == 0 ) {
> >             url = "default";
> >         }
> >
> >         factory = this.map.get(url);
> >
> >         if (null == factory) {
> >             if ( "default".equals(url)) {
> >  1-               factory =
> > Persistence.createEntityManagerFactory(null);
> >             } else {
> >                 Map<String , String> map = new HashMap<String,
> > String>(props);
> >                 map.put("openjpa.ConnectionURL", buildUrl(url));
> >  2-              factory =
> > Persistence.createEntityManagerFactory(url,map);
> >             }
> >             this.map.put(url, factory);
> >         }
> >         return factory;
> >     }
> > ...
> > }
> >
> >
> > Note :
> > I'm using a persistence.xml for single persitence unit
> > creation, and this
> > test works fine. I've had a dive into the source code, and found the
> > provider is lookuped and found by the glassfish persistence
> > implementation
> > class. The exception is raised on the open jpa side, and I
> > know I'm missing
> > some configuration elements, but I cannot guess what's missing?
> > any hint?
> >
> > thank you guys
> >
>
> Notice:  This email message, together with any attachments, may contain
> information  of  BEA Systems,  Inc.,  its subsidiaries  and  affiliated
> entities,  that may be confidential,  proprietary,  copyrighted  and/or
> legally privileged, and is intended solely for the use of the individual or
> entity named in this message. If you are not the intended recipient, and
> have received this message in error, please immediately return this by email
> and then delete it.
>

RE: Programmatical EntityManagerFactory init

Posted by Patrick Linskey <pl...@bea.com>.
> Note :
> I'm using a persistence.xml for single persitence unit 
> creation, and this
> test works fine. I've had a dive into the source code, and found the
> provider is lookuped and found by the glassfish persistence 
> implementation
> class. The exception is raised on the open jpa side, and I 
> know I'm missing
> some configuration elements, but I cannot guess what's missing?
> any hint?

The only exception that I see in the trace you gave us is thrown by the
spec implementation jar. I'm not looking at the source for that code,
but my guess would be that the failure is happening because of a
classpath error, such that OpenJPA is not available in the classloader
that is visible to the Persistence class.

Could you post your persistence.xml file and any details about how your
classloader structure is set up?

Thanks,

-Patrick

-- 
Patrick Linskey
BEA Systems, Inc. 

_______________________________________________________________________
Notice:  This email message, together with any attachments, may contain
information  of  BEA Systems,  Inc.,  its subsidiaries  and  affiliated
entities,  that may be confidential,  proprietary,  copyrighted  and/or
legally privileged, and is intended solely for the use of the individual
or entity named in this message. If you are not the intended recipient,
and have received this message in error, please immediately return this
by email and then delete it. 

> -----Original Message-----
> From: Erwan Le Goulven [mailto:erwan.legoulven@gmail.com] 
> Sent: Friday, April 06, 2007 4:46 AM
> To: open-jpa-dev@incubator.apache.org
> Subject: Programmatical EntityManagerFactory init
> 
> Messieurs-dames,
> 
> As I could not find much clues about the right way to do it, 
> here I come:
> I'm trying to get an EntityManagerFactory properly setup from the
> Persistence class, but I cannot get further than the 
> following exception
> 
> javax.persistence.PersistenceException: No Persistence provider for
> EntityManager named teamplay_test
>     at javax.persistence.Persistence.createEntityManagerFactory(
> Persistence.java:89)
>     at 
> com.gdteam.teamplay.domain.manager.TeamplayEMFactoryManager.get(
> TeamplayEMFactoryManager.java:60)
>     at com.gdteam.teamplay.test.persistence.PersistenceUtilTest.setUp(
> PersistenceUtilTest.java:19)
>     at junit.framework.TestCase.runBare(TestCase.java:125)
>     at junit.framework.TestResult$1.protect(TestResult.java:106)
>     at junit.framework.TestResult.runProtected(TestResult.java:124)
>     at junit.framework.TestResult.run(TestResult.java:109)
>     at junit.framework.TestCase.run(TestCase.java:118)
>     at junit.framework.TestSuite.runTest(TestSuite.java:208)
>     at junit.framework.TestSuite.run(TestSuite.java:203)
> 
> Here is the code part that leads to the exception:
> 
> public class TeamplayEMFactoryManager  {
> ...
>     private final Map<String, String> props = new 
> HashMap<String, String>();
>     private final Map<String, EntityManagerFactory> map = new
> HashMap<String, EntityManagerFactory>();
> ...
>     private TeamplayEMFactoryManager() {
>         props.put("openjpa.ConnectionDriverName", 
> "org.hsqldb.jdbcDriver");
>         props.put("openjpa.ConnectionUserName", "sa");
>         props.put("openjpa.ConnectionPassword", "");
>         props.put("openjpa.Log", "DefaultLevel=WARN, Tool=INFO");
>         props.put("openjpa.MetaDataFactory", 
> "jpa(Types="+buildTypes()+")");
>     }
> 
> ..
> 
> public EntityManagerFactory get(String url) {
> 
>         EntityManagerFactory factory = null;
> 
>         if (null == url || url.length() == 0 ) {
>             url = "default";
>         }
> 
>         factory = this.map.get(url);
> 
>         if (null == factory) {
>             if ( "default".equals(url)) {
>  1-               factory = 
> Persistence.createEntityManagerFactory(null);
>             } else {
>                 Map<String , String> map = new HashMap<String,
> String>(props);
>                 map.put("openjpa.ConnectionURL", buildUrl(url));
>  2-              factory = 
> Persistence.createEntityManagerFactory(url,map);
>             }
>             this.map.put(url, factory);
>         }
>         return factory;
>     }
> ...
> }
> 
> 
> Note :
> I'm using a persistence.xml for single persitence unit 
> creation, and this
> test works fine. I've had a dive into the source code, and found the
> provider is lookuped and found by the glassfish persistence 
> implementation
> class. The exception is raised on the open jpa side, and I 
> know I'm missing
> some configuration elements, but I cannot guess what's missing?
> any hint?
> 
> thank you guys
> 

Notice:  This email message, together with any attachments, may contain information  of  BEA Systems,  Inc.,  its subsidiaries  and  affiliated entities,  that may be confidential,  proprietary,  copyrighted  and/or legally privileged, and is intended solely for the use of the individual or entity named in this message. If you are not the intended recipient, and have received this message in error, please immediately return this by email and then delete it.