Does anyone know how to force HibernatePersistence.createEntityManagerFactory to return a null if it cannot find the requested persistence-unit?
I have an application where I have multiple persistence providers on my classpath (toplink and hibernate). In my persistence.xml I have a single persistence-unit specifying a toplink provider. When I call javax.persistence.Persistence.createEntityManagerFactory() with my persistence-unit, I get the following HibernateException: "Hibernate Dialect must be explicitly set"
I ran it again and stepped into javax.persistence.Persistence.createEntityManagerFactory() to see what was going on. The code in this method is simple. Iterate over all known providers and try to create an EntityManagerFactory. If null is returned assume there is no persistence-unit for the current provider and move on to the next. If no provider can create an EntityManagerFactory, then throw the following exception: "No Persistence provider for EntityManager named ..."
I did not see a problem with this code so I dove deeper. It turns out that the Hibernate provider is the first provider in the list. I have no persistence.xml that specifies Hibernate as the provider, nor do I have a hibernate.properties. All I have is the provider.
On line 3 of HibernatePersistence.createEntityManagerFactory, cfg.configure() does nothing because there is no Hibernate persistence-unit, so the override properties are thrown away. Now there are no Hibernate properties specified, so when cfg.buildEntityManagerFactory() executes, an exception is thrown because there is no dialect or databasename. This is bad. I would assume that cfg.buildEntityManagerFactory() would return null if it could not create the desired EntityManagerFactory.
Now, I tried to work around this by providing a hibernate.properties file with some dummy values. Surprisingly
, HibernatePersistence.createEntityManagerFactory returned me an EntityManagerFactory. It should not because there is no persistence.xml on my classpath that specifies hibernate as a provider and has the requested persistence-unit.
Is there anyway I can make this work without re-writing HibernatePersistence.createEntityManagerFactory or javax.persistence.Persistence.createEntityManagerFactory?
|