Search code examples
javaeclipselinkjndijpa-2.1

Set datasource to JPA without a container


I would like to use a a while testing my entities a special datasource

https://code.google.com/p/datasource-proxy/

which wraps the original datasource (in my case apache derby's) ClientDataSource

So how can i inject the datasource in my JPA without having a container...?

I tried to use simple-jndi but does not work. (not with eclipse link implementation of JPA2)

Is there a way to bypass the JNDI for the datasource when configuring persistence unit ?

(programatically ?)

Thanks.


Solution

  • I found a way for EclipseLink JPA implementation

    import org.eclipse.persistence.config.PersistenceUnitProperties;
    //define your datasource before proxyDS - not shown here
    //then add this property to entity manager factory prop map
    
    emfProps.put(PersistenceUnitProperties.NON_JTA_DATASOURCE, proxyDS);
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("CompanyPU", emfProps);
    EntityManager em = emf.createEntityManager();
    

    I still want to find a more generic way for any JPA provider