You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomee.apache.org by Alexander Saint Croix <sa...@gmail.com> on 2007/12/13 04:22:24 UTC

Alternatives to persistence provider properties in persistence.xml file?

Hello,

I'm working on an application that at the moment includes a single
(proof-of-concept) entity bean in my main web application WAR, and also
large number of entity beans which are JAR'd in the WEB-INF/lib.

Knowing that the persistence management is all outboard and not contained
inside of OpenEJB, perhaps you guys could help me with a matter of
configuration anyway.

I wonder whether there is any way for me to NOT include the <provider> and
<properties> elements defining the persistence provider implementation,
connection URL, connection driver name, username and password in the
persistence.xml file for my entity/POJO jar (the one inside of WEB-INF/lib),
but rather configure that specific portion of the persistence.xml file
inside of the context of the application that is using these beans?  It
doesn't seem to be any of the POJO JAR's business what type of persistence
provider is being used to persist its contents.

I've attached an example of the <provider> and <properties> elements I wish
to exclude below.  I want to define classes but not persistence provider
information in the entity JARs, and define the persistence provider for
these persistence-units once at the container level.  Is this possible, and
if so, how might I accomplish that separation with OpenEJB?

Regards,
--
Alexander R. Saint Croix



<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
    <persistence-unit name="example" transaction-type="RESOURCE_LOCAL">
        <provider>
            org.apache.openjpa.persistence.PersistenceProviderImpl
        </provider>

        <class>org.eremite.SomeBeanInThisJar</class>

        <properties>
            <property name="openjpa.jdbc.SynchronizeMappings"
                      value="buildSchema"/>

            <property name="openjpa.ConnectionURL"
                      value="jdbc:mysql://localhost/corm"/>

            <property name="openjpa.ConnectionDriverName"
                      value="com.mysql.jdbc.Driver"/>

            <property name="openjpa.ConnectionUserName"
                      value="username"/>

            <property name="openjpa.ConnectionPassword"
                      value="password"/>
        </properties>
    </persistence-unit>
</persistence>

Re: Alternatives to persistence provider properties in persistence.xml file?

Posted by Alexander Saint Croix <sa...@gmail.com>.
Thanks, Jacek--we worked this out, but it's good to have the answer on the
list for others.

My final strategy was NOT to place a persistence.xml file in my component
library JARs at all.  I instead let the client application determine how the
persistence unit and data sources are set up.  It works out very well.

Cheers,
--
Alexander

On Jan 5, 2008 3:47 AM, Jacek Laskowski <ja...@laskowski.net.pl> wrote:

> On Dec 13, 2007 4:22 AM, Alexander Saint Croix
> <sa...@gmail.com> wrote:
>
> > I wonder whether there is any way for me to NOT include the <provider>
> and
> > <properties> elements defining the persistence provider implementation,
> > connection URL, connection driver name, username and password in the
> > persistence.xml file for my entity/POJO jar (the one inside of
> WEB-INF/lib),
> > but rather configure that specific portion of the persistence.xml file
> > inside of the context of the application that is using these beans?  It
> > doesn't seem to be any of the POJO JAR's business what type of
> persistence
> > provider is being used to persist its contents.
>
> It's been a while since you wrote it, but I thought I'd jump in and
> add a little.
>
> Simple answer is NO - you can't. It's because that's the main goal of
> a ejb container to manage all what needs to be managed, e.g. jpa
> entity managers. When <provider> is left out it means you rely on the
> default jpa provider of a ejb container. <properties> are not required
> at all and are to configure parameters that are specific to a given
> jpa provider. Your persistence.xml file could look as follows:
>
> <persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
>   <persistence-unit name="example" />
> </persistence>
>
> All other elements and attributes are optional. You won't be able to
> override the values from persistence.xml file unless you do the job of
> an ejb container, i.e. you get your hands dirty managing jpa objects,
> like jpa entity manager factory and its entity managers.
>
> Jacek
>
> --
> Jacek Laskowski
> http://www.JacekLaskowski.pl
>

Re: Alternatives to persistence provider properties in persistence.xml file?

Posted by Jacek Laskowski <ja...@laskowski.net.pl>.
On Dec 13, 2007 4:22 AM, Alexander Saint Croix
<sa...@gmail.com> wrote:

> I wonder whether there is any way for me to NOT include the <provider> and
> <properties> elements defining the persistence provider implementation,
> connection URL, connection driver name, username and password in the
> persistence.xml file for my entity/POJO jar (the one inside of WEB-INF/lib),
> but rather configure that specific portion of the persistence.xml file
> inside of the context of the application that is using these beans?  It
> doesn't seem to be any of the POJO JAR's business what type of persistence
> provider is being used to persist its contents.

It's been a while since you wrote it, but I thought I'd jump in and
add a little.

Simple answer is NO - you can't. It's because that's the main goal of
a ejb container to manage all what needs to be managed, e.g. jpa
entity managers. When <provider> is left out it means you rely on the
default jpa provider of a ejb container. <properties> are not required
at all and are to configure parameters that are specific to a given
jpa provider. Your persistence.xml file could look as follows:

<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
   <persistence-unit name="example" />
</persistence>

All other elements and attributes are optional. You won't be able to
override the values from persistence.xml file unless you do the job of
an ejb container, i.e. you get your hands dirty managing jpa objects,
like jpa entity manager factory and its entity managers.

Jacek

-- 
Jacek Laskowski
http://www.JacekLaskowski.pl

Re: Alternatives to persistence provider properties in persistence.xml file?

Posted by Dain Sundstrom <da...@iq80.com>.
The openjpa.ConnectionURL, openjpa.ConnectionDriverName,  
openjpa.ConnectionUserName and openjpa.ConnectionPassword properties  
are OpenJPA specific properties that you use when you are in an  
unmanaged environment.  When embedded into a managed environment,  
like OpenEJB, you should use the spec defined "jta-data-source" and  
"non-jta-data-source" elements nested under the "persistence-unit"  
element.  For example:

https://svn.apache.org/repos/asf/openejb/trunk/openejb3/examples/ 
webapps/ejb-examples/src/main/resources/META-INF/persistence.xml

You will most likely want to keep the  
openjpa.jdbc.SynchronizeMappings property which causes OpenJPA to  
auto create missing database tables and columns.  This is a vendor  
specific property that is required by spec to be ignored by other  
vendors.  If you want to support other JPA implementations, you will  
have to determine what the equivalent property is for each vendor (or  
in this case provide you users some SQL to create the tables).

-dain


On Dec 12, 2007, at 7:22 PM, Alexander Saint Croix wrote:

> Hello,
>
> I'm working on an application that at the moment includes a single
> (proof-of-concept) entity bean in my main web application WAR, and  
> also
> large number of entity beans which are JAR'd in the WEB-INF/lib.
>
> Knowing that the persistence management is all outboard and not  
> contained
> inside of OpenEJB, perhaps you guys could help me with a matter of
> configuration anyway.
>
> I wonder whether there is any way for me to NOT include the  
> <provider> and
> <properties> elements defining the persistence provider  
> implementation,
> connection URL, connection driver name, username and password in the
> persistence.xml file for my entity/POJO jar (the one inside of WEB- 
> INF/lib),
> but rather configure that specific portion of the persistence.xml file
> inside of the context of the application that is using these  
> beans?  It
> doesn't seem to be any of the POJO JAR's business what type of  
> persistence
> provider is being used to persist its contents.
>
> I've attached an example of the <provider> and <properties>  
> elements I wish
> to exclude below.  I want to define classes but not persistence  
> provider
> information in the entity JARs, and define the persistence provider  
> for
> these persistence-units once at the container level.  Is this  
> possible, and
> if so, how might I accomplish that separation with OpenEJB?
>
> Regards,
> --
> Alexander R. Saint Croix
>
>
>
> <persistence xmlns="http://java.sun.com/xml/ns/persistence"  
> version="1.0">
>     <persistence-unit name="example" transaction- 
> type="RESOURCE_LOCAL">
>         <provider>
>             org.apache.openjpa.persistence.PersistenceProviderImpl
>         </provider>
>
>         <class>org.eremite.SomeBeanInThisJar</class>
>
>         <properties>
>             <property name="openjpa.jdbc.SynchronizeMappings"
>                       value="buildSchema"/>
>
>             <property name="openjpa.ConnectionURL"
>                       value="jdbc:mysql://localhost/corm"/>
>
>             <property name="openjpa.ConnectionDriverName"
>                       value="com.mysql.jdbc.Driver"/>
>
>             <property name="openjpa.ConnectionUserName"
>                       value="username"/>
>
>             <property name="openjpa.ConnectionPassword"
>                       value="password"/>
>         </properties>
>     </persistence-unit>
> </persistence>