persistence@glassfish.java.net

Re: Setting persistence.xml properties programmatically

From: Gordon Yorke <gordon.yorke_at_oracle.com>
Date: Mon, 28 Jul 2008 09:28:46 -0400

The properties can be passed in when calling
createEntityManagerFactory() or provide them to the @PersistenceContext
annotation in the case of injection.

        Map map = new HashMap();
        map.put("toplink.jdbc.user", "user1");
        ...
        EntityManagerFactory emf =
Persistence.createEntityManagerFactory("PUName", map);

or
        
@PersistenceContext(properties={_at_PersistenceProperty(name="toplink.jdbc.user",
value="user1")...})

--Gordon

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.
>