[JPA] Initializing a persistence unit with information retrieved at runtime [message #638294] |
Wed, 10 November 2010 13:01  |
Eclipse User |
|
|
|
Hello,
I am using EclipseLink in a JavaSE application, in which I need to initialize a persistence unit from a JAR that is loaded at runtime, but does NOT have a persistence.xml.
So, for example, suppose I have a class loader for "myentitites.jar", which does NOT include a META-INF/persistence.xml file. Suppose also that even though the persistence.xml is missing, I somehow have all the required values in a runtime Object (e.g. puInfo). How do I get from that to creating an EntityManagerFactory? I've tried:
properties.put(PersistenceUnitProperties.CLASSLOADER, myClassLoaderForAccessingTheJAR);
properties.put(PersistenceUnitProperties.SESSION_NAME, puInfo.getName());
properties.put(PersistenceUnitProperties.TARGET_DATABASE, puInfo.getDatabase());
properties.put(PersistenceUnitProperties.JDBC_DRIVER, puInfo.getDriver());
properties.put(PersistenceUnitProperties.JDBC_URL, puInfo.getURL());
properties.put(PersistenceUnitProperties.JDBC_USER, puInfo.getUser());
properties.put(PersistenceUnitProperties.JDBC_PASSWORD, puInfo.getPassword());
factory = new PersistenceProvider().createEntityManagerFactory(puInfo.getName(), properties);
However, EclipseLink returns a null object for the EntityManagerFactory without logging some error or throwing an exception...
1st edit: I use EclipseLink in an OSGi environment (using the Rich Ajax Platform).
2nd edit: I should point out that I don't care about using EclipseLink-specific APIs to achieve this. So although I would use JPA in the rest of the code, it's ok to create my EntityManagerFactory using non-standard APIs.
[Updated on: Wed, 10 November 2010 13:08] by Moderator
|
|
|
|
|
Re: [JPA] Initializing a persistence unit with information retrieved at runtime [message #638641 is a reply to message #638620] |
Thu, 11 November 2010 17:15  |
Eclipse User |
|
|
|
Hi,
I've managed to create an EntityManager using the approach I thought would be harder:
private EntityManagerFactory getEntityManagerFactory() {
DatabaseLogin dbl = new DatabaseLogin();
dbl.setUserName("sa");
dbl.setPassword("");
// dbl.setConnectionString("jdbc:derby:mydb;create=true");
// dbl.setDriverClassName("org.apache.derby.jdbc.EmbeddedDriver");
dbl.setConnector(new DefaultConnector(
"org.apache.derby.jdbc.EmbeddedDriver", "jdbc:derby:",
"mydb;create=true") {
private static final long serialVersionUID = 1L;
@Override
protected void loadDriverClass(Session session)
throws DatabaseException {
ClassLoader loader = Thread.currentThread()
.getContextClassLoader();
try {
ClassLoader derbyCl = org.apache.derby.jdbc.EmbeddedDriver.class
.getClassLoader();
Thread.currentThread().setContextClassLoader(derbyCl);
super.loadDriverClass(session);
} finally {
Thread.currentThread().setContextClassLoader(loader);
}
}
});
Project prj = new Project(dbl);
ServerSession serverSession = (ServerSession) prj.createServerSession();
return new EntityManagerFactoryImpl(serverSession);
}
My current problems are:
1) The EntityManager's persistence unit is empty. How do I feed my "orm.xml" into it?
2) I used setConnector() instead of simply setConnectionString()/setDriverClassName() because it was the only way I could find to override the class loader that provides access to the driver, Is this a proper way to do this?
Any pointers for these?
|
|
|
Powered by
FUDForum. Page generated in 0.07835 seconds