我想在测试我的实体时使用 aa 一个特殊的数据源
https://code.google.com/p/datasource-proxy/
它包装了原始数据源(在我的例子中是 apache derby 的)ClientDataSource
那么如何在没有容器的情况下将数据源注入我的 JPA 中......?
我尝试使用 simple-jndi 但不起作用。(不使用 JPA2 的 eclipse 链接实现)
配置持久性单元时,有没有办法绕过数据源的 JNDI?
(以编程方式?)
谢谢。
我想在测试我的实体时使用 aa 一个特殊的数据源
https://code.google.com/p/datasource-proxy/
它包装了原始数据源(在我的例子中是 apache derby 的)ClientDataSource
那么如何在没有容器的情况下将数据源注入我的 JPA 中......?
我尝试使用 simple-jndi 但不起作用。(不使用 JPA2 的 eclipse 链接实现)
配置持久性单元时,有没有办法绕过数据源的 JNDI?
(以编程方式?)
谢谢。
我找到了 EclipseLink JPA 实现的方法
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();
我仍然想为任何 JPA 提供者找到更通用的方法
如果您想通过程序设置数据源而不使用容器或 JNDI 查找,则可以使用以下包含 Apache Derby 的ClientDataSource的方式:
public DataSource createDataSource() {
   ClientDataSource dataSource = new ClientDataSource();
   dataSource.setServerName("localhost");
   dataSource.setPortNumber(1527);
   dataSource.setDatabaseName("mytestdb");
   dataSource.setUser("myusername");
   dataSource.setPassword("mypasswd");
   return dataSource;
}
然后您可以将此代码放入您的应用程序中以获取 JDBC 连接:
Connection connection = dataSource.getConnection("myusername", "mypasswd");