问题
I would like to use EclipseLink as my JPA engine. We are using a subclass of java.sql.Connection and I would like to know if I can force EclipseLink to use my connection. I would like to set the connection object programmatically.
Something like
MyConnection conn = new MyConnection(JDBC_URL, USR, PWD);
EntityManagerFactory factory = Persistence.createEntityManagerFactory(conn);
回答1:
You can set a custom connection programatically using the following code:
Map properties = new HashMap();
// Configure the internal EclipseLink connection pool
properties.put(JDBC_DRIVER, "oracle.jdbc.OracleDriver");
properties.put(JDBC_URL, "jdbc:oracle:thin:@localhost:1521:ORCL");
properties.put(JDBC_USER, "user-name");
properties.put(JDBC_PASSWORD, "password");
Persistence.createEntityManagerFactory("unit-name", properties);
I found this answer here after a quick google search:
http://eclipse.1072660.n5.nabble.com/Defining-persistence-xml-programatically-td2536.html
I dont believe that there is a way to force Eclipselink to connect using only that java.sql.Connection
; not out of the box at least. You could make a simple utility method to pass in said connection and build a properties map as I described above.
public Map convertConnectionToProperties(Connection conn) {
// Do the mapping
return theMap;
}
If you take a look at this link you will see that
Persistence.java
createEntityManagerFactory(String persistenceUnitName)
createEntityManagerFactory(String persistenceUnitName, Map properties)
That is all it can take as parameters. Simply put; what you are asking for simply isn't possible without taking your connection object and adjust it to return something that complies with what createEntityManagerFactory
can take in.
class MyConnection {
// Your code here
public Map convertToMap() {
// Do the mapping
return theMap;
}
public String getPersistenceUnitName() {
return "Some-Name";
}
}
And then utilize it like this:
MyConnection conn = new MyConnection(JDBC_URL, USR, PWD);
EntityManagerFactory factory = Persistence.createEntityManagerFactory(conn.getPersistenceUnitName(), conn.convertToMap());
回答2:
* This solution use Spring Framework.
javax.sql.DataSource
First you need to wrap your connection in a javax.sql.DataSource
, e.g.:
import java.sql.Connection;
import java.sql.SQLException;
import org.springframework.jdbc.datasource.AbstractDataSource;
public class MyDataSource extends AbstractDataSource {
private static final String JDBC_URL = "jdbc:mysql://localhost/test";
private static final String USR = "root";
private static final String PWD = "";
public Connection getConnection() throws SQLException {
return new MyConnection(JDBC_URL, USR, PWD);
}
public Connection getConnection(String username, String password)
throws SQLException {
return new MyConnection(JDBC_URL, USR, PWD);
}
}
To avoid overwriting all methods, you can extend an existing class in Spring.
javax.persistence.EntityManagerFactory
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter;
public class Main {
public static void main(String[] args) {
LocalContainerEntityManagerFactoryBean bean =
new LocalContainerEntityManagerFactoryBean();
bean.setDataSource(new MyDataSource());
bean.setPersistenceUnitName("TestPU");
bean.setJpaVendorAdapter(new EclipseLinkJpaVendorAdapter());
bean.afterPropertiesSet();
EntityManagerFactory factory = bean.getNativeEntityManagerFactory();
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();
User user = new User();
user.setFullname("Mister");
em.persist(user);
em.getTransaction().commit();
em.close();
}
}
NOTES
You may want to need add the next property in your
persistence.xml
:<property name="eclipselink.weaving" value="false"/>
I used a Standalone Maven Project for test the code and the dependencies in the
pom.xml
looks like this:<dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.31</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.0.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>4.0.5.RELEASE</version> </dependency> <dependency> <groupId>org.eclipse.persistence</groupId> <artifactId>eclipselink</artifactId> <version>2.5.1</version> </dependency> </dependencies>
Or if you prefer, the required jars:
mysql-connector-java-5.1.31.jar spring-context-4.0.5.RELEASE.jar spring-aop-4.0.5.RELEASE.jar aopalliance-1.0.jar spring-beans-4.0.5.RELEASE.jar spring-core-4.0.5.RELEASE.jar commons-logging-1.1.3.jar spring-expression-4.0.5.RELEASE.jar spring-orm-4.0.5.RELEASE.jar spring-jdbc-4.0.5.RELEASE.jar spring-tx-4.0.5.RELEASE.jar eclipselink-2.5.1.jar javax.persistence-2.1.0.jar commonj.sdo-2.1.1.jar
来源:https://stackoverflow.com/questions/24452376/eclipselink-jpa-2-1-user-supplied-connection