persistence@glassfish.java.net

RE: Setting persistence.xml properties programmatically

From: <DOUGLAS.CLARKE_at_oracle.com>
Date: Mon, 28 Jul 2008 07:02:15 -0700 (PDT)

RMMM,

You will find a document listing many of the persistence unit configurations using either persistence.xml or API properties at:

http://www.oracle.com/technology/products/ias/toplink/JPA/essentials/toplink-jpa-extensions.html

You will find a similar document for EclipseLink at:

http://wiki.eclipse.org/Using_EclipseLink_JPA_Extensions_%28ELUG%29#Using_EclipseLink_JPA_Extensions_for_JDBC

For most properties these is both an example of the XML usage as well as the API usage with a Map of properties.

If you are just starting a new project and are considering using Toplink Essentials you may want to consider using EclipseLink. EclipseLink JPA is replacing TopLink Essentials as the reference implementation for JPA 2.0 and is also replacing its usage within GlassFish/SunAS and the Oracle Application Server.

Doug

www.eclipse.org/eclipselink

-----Original Message-----
From: TOM.WARE_at_ORACLE.COM
Sent: Monday, July 28, 2008 9:39 AM
To: persistence_at_glassfish.dev.java.net
Subject: Re: Setting persistence.xml properties programmatically


The reason you do not see examples about this in TopLink Essentials-specfic
documentation is that there is a well known JPA-spec-based way to do this.

EntityManagerFactory creation takes a list of properties. Here is how the API
for the Persistence class is defined in the spec:

/**
* Called by Persistence class when an EntityManagerFactory
* is to be created.
*
* @param emName The name of the persistence unit
* @param map A Map of properties for use by the
* persistence provider. These properties may be used to
* override the values of the corresponding elements in
* the persistence.xml file or specify values for
* properties not specified in the persistence.xml
* (and may be null if no properties are specified).
* @return EntityManagerFactory for the persistence unit,
* or null if the provider is not the right provider
*/
public EntityManagerFactory createEntityManagerFactory(String
emName, Map map);

The "map" above is where you would pass your properties. I believe there are
similar ways of doing this when injecting.

FYI: If you are just getting started with a project, I suggesting going to
EclipseLink as your persistence provider since it will be the persistence
provider for newer GlassFish releases and is shipped with GlassFish V3:

http://blogs.sun.com/theaquarium/entry/eclipselink_in_glassfish_v3_as

For information about EclipseLink, the following link is a good starting place:

www.eclipse.org/eclipselink

-Tom


RMMM wrote:
> I'm writing an application that will connect to a database that depends on
> the user. For
> that reason, I can't have the connection information in persistence.xml (the
> schema
> is always the same though). I'm tentatively planning on using TopLink
> Essentials for
> the JPA implementation, assuming I can get answers my questions ...
>
> The connection is determined by 4 properties in the persistence.xml file:
> jdbc driver,
> and the database url, user, and password.
>
> Given the 4 strings (driver, url, user, password), what Java code do I write
> that is
> equivalent to having those in the persistence.xml file?
>
> I'm a little puzzled why this isn't spelled out anywhere I've looked. Isn't
> it one of the
> most obvious, basic issues? I'd think it would be near the beginning of
> almost any
> discussion of JPA or TopLink Essentials ..., but it isn't. Is it documented
> somewhere?
> Is there a snippet of example code I can find? Would someone be kind enough
> to just
> give me the code?
>
> Thanks.