88748

Using Hibernate Ejb3Configuration with Container Management

Question:

Is there a way to use programmatic configuration provided by Ejb3Configuration with container managed entity management? I would like to dynamically add (additional) annotated classes that were discovered via ServiceLoader after the persistence.xml is processed.

I attempted to extend HibernatePersistence override the createContainerEntityManagerFactory method where I added the functionality to add the classes but I found two problems: 1) When I attempt to call the EjbConfiguration instances configure(PersistenceUnitInfo, Map) method within the new PersistenceProvider, I get null returned. 2) I get a NPE thrown by my app server (both GF 3.0.1 and GF 3.1) at application shutdown originating from either org.glassfish.persistsnce.jpa.JPAApplicationContainer.closeAllEMFs or org.glassfish.persistence.jpa.JPADeployer.closeEMFs, respectively

The following is my PersistenceProvider:

//left out createEntityManagerFactory for brevity public class DynamicEntityProvider extends HibernatePersistence { @Override public EntityManagerFactory createContainerEntityManagerFactory (PersistenceUnitInfo info, Map props) { final Ejb3Configuration config = new Ejb3Configuration(); final Ejb3Configuration configured = config.configure(info, props); if (configured != null) { //never gets here, configured is always returned null addDynamicEntities(configured); return configured.buildEntityManagerFactory(); } //logging code which notifies me that configured was null return null; } //addDynamicEntities() method impl

I simply replace the tag in my persistence.xml file to the class and it does indeed get called as expected...just with the problems listed above =)

Any ideas would be greatly appreciated!

Answer1:

I don't know if you already have solved your issue, but...

It returns null because the HibernatePersistence class that you are extending checks if the persistence.xml has the provider set to it not a subclass, so it doesn't recognize your class(DynamicEntityProvider) as the provider so it doesn't parse your xml.

if ( metadata.getProvider() == null || IMPLEMENTATION_NAME.equalsIgnoreCase(metadata.getProvider()) ) {

Where IMPLEMENTATION_NAME is

private static final String IMPLEMENTATION_NAME = HibernatePersistence.class.getName();

You can change it internally via reflection but it is not a "good" way of doing that.

Answer2:

It looks like some kind of classloader visibility issue. I know GF uses OSGi bundling so I'm not entirely surprised.

Basically config.configure returns null if it does not find that the persistence unit described by info matches the persistence provider at play. Try and display info and see if the persistence provider is correctly set up and watch for classloader issues (ie myClass != yourClass if the CL loading it is not the same)

Recommend

  • Eclipse doesn't initialize JPA persistence
  • NameNotFoundException in Container Managed EntityManager
  • SSRS ReportViewer nullreference exception on Disposing
  • ValueConverter for Background Color
  • Unsigned applet socket JavaScript
  • freed script with hasOwnProperty
  • How to hide an alias column in mysql EDITED
  • Generate entities from tables in Glassfish 3.1 for Eclipse 3.7
  • Order of evaluation for short-circuit operators and let in OCaml
  • start() had already been called. The second call will be ignored.?
  • Apache shutting down unexpectedly
  • Symfony2 Service Container - does get() return objects by reference or copy?
  • How to resolve “Ambiguous reference to a JS library”?
  • finding maximum depth of chapter
  • C++ how to handle tr1 and non-tr1 namespaces in portable code?
  • Does Java EE 6 framework only for Web Application Or can I use it for Client Application as well
  • openssl handshake failed
  • Correctly Importing Apache Commons Math Package
  • jersey/tomcat Description The origin server did not find a current representation for the target res
  • Unable to connect to OnVif enabled camera using C#
  • .NET video play library which allows to change the playback rate?
  • Getting different value with placeholder over CPU/GPU
  • ImageMagick, replace semi-transparent white with opaque white
  • Cannot connect to cassandra from Spark
  • How to make a tree having multiple type of nodes and each node can have multiple child nodes in java
  • C# - Serializing and deserializing static member
  • Resize panoramic image to fixed size
  • Volusion's generic SQL folder, functionality
  • Cross-Platform Protobuf Serialization
  • Sending data from AppleScript to FileMaker records
  • Alternatives to the OPTIONAL fallback SPARQL pattern?
  • Linker errors when using intrinsic function via function pointer
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • How to include full .NET prerequisite for Wix Burn installer
  • How do you join a server to an Active Directory (domain)?
  • coudnt use logback because of log4j
  • LevelDB C iterator
  • Is it possible to post an object from jquery to bottle.py?
  • How can i traverse a binary tree from right to left in java?
  • How can I use `wmic` in a Windows PE script?