Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » [JPA] Initializing a persistence unit with information retrieved at runtime(Using EclipseLink JPA without a persistence.xml)
[JPA] Initializing a persistence unit with information retrieved at runtime [message #638294] Wed, 10 November 2010 18:01 Go to next message
Alexandros Karypidis is currently offline Alexandros Karypidis
Messages: 59
Registered: March 2010
Member
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 18:08]

Report message to a moderator

Re: [JPA] Initializing a persistence unit with information retrieved at runtime [message #638565 is a reply to message #638294] Thu, 11 November 2010 16:08 Go to previous messageGo to next message
James Sutherland is currently offline James Sutherland
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

You should look into EclipseLink OSGi support,

http://wiki.eclipse.org/EclipseLink/Examples/OSGi

You could create a dummy persistence.xml and then access it using your properties, but not sure how you will get the classes/mappings into it. How have you mapped the classes?

You could always create EntityManagerFactoryImpl directly yourself. You can create one using a ServerSession, or using an EntityManagerSetupImpl, which you could also hand build.


James : Wiki : Book : Blog : Twitter
Re: [JPA] Initializing a persistence unit with information retrieved at runtime [message #638620 is a reply to message #638565] Thu, 11 November 2010 19:43 Go to previous messageGo to next message
Alexandros Karypidis is currently offline Alexandros Karypidis
Messages: 59
Registered: March 2010
Member
Hello,

Regarding my mappings, I am using Teneo to generate code with JPA annotations from an EMF model. I can therefore use the ORMGenerator class at runtime, to create a String object whose value is the "orm.xml" for the EMF model:
	final ORMGenerator ormGenerator = new ORMGenerator();
	ormGenerator.generateORM(new EPackage[] { MyPackage.eINSTANCE });

Therefore, I suppose I need to somehow "feed this into" EclipseLink as well.

I'm currently searching the web for examples on using EntityManagerFactoryImpl directly (I'm not familiar with the EclipseLink-specific API). I see two constructors:

public EntityManagerFactoryImpl(ServerSession serverSession) {
public EntityManagerFactoryImpl(EntityManagerSetupImpl setupImpl, Map properties) {

The first-one seems like a chicken-and-egg situation as it requires that I create a ServerSession first, which as far as I understand is essentially the same thing (I need to provide the persistence unit information and mappings in an EclipseLink-specific way). Since I only have the JPA version of the mapping (the orm.xml in a String) this probably won't go well in my case.

So for now, I'm focusing on the second constructor and trying to figure out how to create an appropriate EntityManagerSetupImpl.

In any case, thank you for your pointers (and I'd really appreciate any further guidance you could spare).
Re: [JPA] Initializing a persistence unit with information retrieved at runtime [message #638641 is a reply to message #638620] Thu, 11 November 2010 22:15 Go to previous message
Alexandros Karypidis is currently offline Alexandros Karypidis
Messages: 59
Registered: March 2010
Member
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?
Previous Topic:Bug on using pagination on Oracle?
Next Topic:Not finding persistence.xml from linked resource
Goto Forum:
  


Current Time: Mon Sep 15 02:14:27 GMT 2014

Powered by FUDForum. Page generated in 0.01812 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software