Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Gemini » Howto create customized EntityManagerFactory
Howto create customized EntityManagerFactory [message #651358] Sat, 29 January 2011 19:18 Go to next message
Nepomuk Seiler is currently offline Nepomuk SeilerFriend
Messages: 88
Registered: December 2010
Member
Hi,

As I'm using derby in embedded mode and I want to set programmatically
where my db is stored, I needed to create a my EntityManagerFactory via

PersistenceProvider.createEntityManagerFactory(name, props);


So, how to get this? The Gemini JPA Sample told me to register a ServiceTracker
on EntityManagerFactory.class which let's me access EntityManagerFactories
only configured via the persistence.xml.

The Answer:
Just use another ServiceTracker for javax.persistence.spi.PersistenceProvider.class.getName().
Gemini JPA is acting the same way.

public void start(final BundleContext context) throws Exception {
		super.start(context);
		plugin = this;
		
		providerTracker = new ServiceTracker(context, PersistenceProvider.class.getName(), new ServiceTrackerCustomizer() {
			
			@Override
			public void removedService(ServiceReference reference, Object service) {				
			}
			
			@Override
			public void modifiedService(ServiceReference reference, Object service) {
			}
			
			@Override
			public Object addingService(ServiceReference reference) {
				PersistenceProvider provider = (PersistenceProvider) context.getService(reference);
				EntityManagerFactory emf = provider.createEntityManagerFactory("PUnit", new Properties());
				emf.createEntityManager();
				return provider;
			}
		});
		providerTracker.open();
		
	}



Soon I will upload my custom JPAUtil class which will hopefully give a good idea on how you
could manage your PersistenceProvider.

-Muki
Re: Howto create customized EntityManagerFactory [message #651363 is a reply to message #651358] Sat, 29 January 2011 22:32 Go to previous messageGo to next message
Michael Keith is currently offline Michael KeithFriend
Messages: 243
Registered: July 2009
Senior Member
Hi Muki,

You should not really be using the PersistenceProvider API since that is an API meant to be used by containers and the Persistence class.

If you want to pass in your JDBC properties dynamically then you should be using the EntityManagerFactoryBuilder API. Create a persistence.xml file but don't include the JDBC properties. An EntityManagerFactoryBuilder service will be made available from which you can call createEntityManagerFactory(name, props) and include the JDBC props. Of course, you should listen for the availability of the JDBC driver service before you call into the EMFB since it will try to look up the JDBC service and fail if it is not available.

-Mike
Re: Howto create customized EntityManagerFactory [message #651388 is a reply to message #651358] Sun, 30 January 2011 10:16 Go to previous messageGo to next message
Nepomuk Seiler is currently offline Nepomuk SeilerFriend
Messages: 88
Registered: December 2010
Member
Hi Mike,

Wow, Gemini JPA is very smart! Works fine.

-Muki
Re: Howto create customized EntityManagerFactory [message #652487 is a reply to message #651388] Fri, 04 February 2011 09:38 Go to previous messageGo to next message
Garth is currently offline GarthFriend
Messages: 1
Registered: February 2011
Junior Member
Hi Muki,

Could you supply me with some sample code on how you went about creating your entityManagerFactory?
Re: Howto create customized EntityManagerFactory [message #653210 is a reply to message #651358] Tue, 08 February 2011 21:20 Go to previous message
Nepomuk Seiler is currently offline Nepomuk SeilerFriend
Messages: 88
Registered: December 2010
Member
Hi,

Currently I'm using this class to connect to my db:
http://www.mukis.de/pub/JPAUtil.java

I think it's not a very clean and good coding style, however it works.
This is how to use this Util-Class.

The activator providing this class should look like something like this:

	public void start(final BundleContext context) throws Exception {
		super.start(context);
		plugin = this;
		database = new Database();
		
		emfbTracker = new ServiceTracker(context, EntityManagerFactoryBuilder.class.getName(),
				JPAUtil.getServiceTrackerCustomizer("punit", database.getProperties()));
		emfbTracker.open();
	}



The util class has a "isAvailable" method to check if you can connect and an "createEntityManager" method for creating EntityManagers with the given properties and persistence unit.

Hope this helps a bit. I'm glad for any tip to improve this class (even if you say "do it completly again" Razz )
Previous Topic:Gemini JPA/dbaccess and Eclipse RCP Features (Sample)
Next Topic:Gemini JPA 1.0 Milestone 4
Goto Forum:
  


Current Time: Tue Apr 23 08:37:06 GMT 2024

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

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

Back to the top