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());