Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] NullPointer trying JPA2 (MetamodelImpl.java:370) - em is not initialized before use

Hi Michael,

 It worked, i was injecting from factory and forgot to create entityManager... now im injecting EntityManager and its working like expected. 

Thanks... it was a really fast answer.

2009/12/16 Michael O'Brien <michael.obrien@xxxxxxxxxx>
Diego,
  Hi, I think I may have figured out your issue.
  Your persistence unit is not initilized in your DAO - since you are using the EMF directly, you are running an SE PU which requires that you create the EM before attempting to use the PU - there is no predeploy before you attempt to get the CriteriaBuilder (which uses the Metamodel)
  Normally, you would either get the EMF via injection or bootstrap Persistence class and get the EM via CreateEntityManager(SE) or via injection(EE) - thereby doing a predepoy (where the metamodel is pre-initialized properly)

  Your Fix:
-----------------
     add the following call to your code before emf.getCriteriaBuilder() - you may use entityManager.getCriteriaBuilder
     EntityManager entityManager = emf.createEntityManager()

  Reproduction:
-----------------
  If I insert the following invalid code before EM creation - I get the same NPE as you.
  by inserting emf.getCriteriaBuilder() before we do an emf.createEntityManager()
  private void initialize(String puName) {
      try {
              // Initialize an application managed JPA emf and em via META-INF
              emf  = Persistence.createEntityManagerFactory(puName);
invalid--> emf.getCriteriaBuilder(); // EM is not created or initialized yet - this inserted line is invalid code as the descriptors will not be initialized
              entityManager = emf.createEntityManager();          

  Note that it was decided in DI 42 that we would not check for a null javaClass on the ClassDescriptor - this would be an invalid state - therefore we assume that all RelationalDescriptors always have their javaClass set

Currently there is no NPE handling for a null javaType set in the ManagedType contructor - as this is an invalid state.
  protected ManagedTypeImpl(
      MetamodelImpl metamodel, RelationalDescriptor descriptor) {
      // A valid descriptor will always have a javaClass set
      super(descriptor.getJavaClass());

[EL Finest]: 2009-12-16 08:22:47.492--ServerSession(8880493)--Thread(Thread[main,5,main])--End predeploying Persistence Unit dao.create.tables.derby; session default-session; state Predeployed; factoryCount 1
java.lang.NullPointerException
  at org.eclipse.persistence.internal.jpa.metamodel.MetamodelImpl.initialize(MetamodelImpl.java:370)
  at org.eclipse.persistence.internal.jpa.metamodel.MetamodelImpl.<init>(MetamodelImpl.java:101)
  at org.eclipse.persistence.internal.jpa.metamodel.MetamodelImpl.<init>(MetamodelImpl.java:120)
  at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.getMetamodel(EntityManagerSetupImpl.java:1939)
  at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.getMetamodel(EntityManagerFactoryImpl.java:472)
  at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.getCriteriaBuilder(EntityManagerFactoryImpl.java:456)
  at org.eclipse.persistence.example.jpa.server.common.DDLGenerationClient.initialize(DDLGenerationClient.java:149)

  If you use a valid entityManager to get the criteriaBuilder then you will have no issues.
  thank you
  /michael

Michael O'Brien wrote:
Diego,
  1) Looks like your manually generated "Status_.java" canonical class may be missing an import reference to "Status_.java" - this would cause generation of the javaType for the "status" OneToOne to fail - however the NPE needs to be fixed and replaced with a warning that the canonical model is incomplete.
  Try generating your "_" classes with the command in 2) below, or use the dynamic non-canonical ones created in memory during deployment.

For...

public static volatile SingularAttribute<Usuario, Status> status;

Add just after your package declaration...

import your.package.Status;

     As a result of this, the javaType of "Status" is not set on the "status" SingularAttribute.
     The fix will be to reference your other Status class.
     However, the NPE in this case needs to be addressed by keeping the superType as null == root level object
     I have raised bug #297928 to fix this in 2.0.1
https://bugs.eclipse.org/bugs/show_bug.cgi?id=297928

      // Assign all superType fields on all IdentifiableTypes (only after all managedType objects have been created)
      for(ManagedTypeImpl<?> potentialIdentifiableType : managedTypes.values()) {
          Class aClass = potentialIdentifiableType.getJavaType();
          /**
           * The superclass for top-level types is Object - however we set [null] as the supertype for root types.
           * 1) We are constrained by the fact that the spec requires that a superType be an IdentifiableType.
           *    Since [Object] is not an Entity or MappedSuperclass - it fails this criteria - as it would be a BasicType
           *    because it has no @Entity or @MappedSuperclass annotation.<p>
           * 2) Another object space reasoning issue behind this is to separate the Java and Metamodel object spaces.
           * In Java all types inherit from Object, however in the JPA Metamodel all types DO NOT inherit from a common type.
           * Therefore in the metamodel top-level root types have a superType of null.
           * See design issue discussion:
           * http://wiki.eclipse.org/EclipseLink/Development/JPA_2.0/metamodel_api#DI_42:_20090709:_IdentifiableType.supertype_-_what_do_top-level_types_set_it_to
           */
370-->  Class superclass = aClass.getSuperclass();


  2) You can generate the "_" canonical metamodel classes using the following line, eclipse integration is in progress.
http://wiki.eclipse.org/UserGuide/JPA/Using_the_Canonical_Model_Generator_%28ELUG%29
  Here is an example generation of "_" java files without corresponding compiled class files (-proc:only) - placed at the cmd root
  Depending on your IDE, you will be able to automatically generate these as long as the services file is in your classpath.

javac -processor org.eclipse.persistence.internal.jpa.modelgen.CanonicalModelProcessor -proc:only -classpath lib/eclipselink.jar;lib/javax.persistence_2.0_preview.jar;punit src/org/eclipse/persistence/testing/models/jpa/metamodel/*.java


  thank you
  /michael
 Diego Coronel wrote:
Hi,
 Im trying JPA2 using glassfish V3 final, Weld, and im getting this error:
Caused by: java.lang.NullPointerException         at
org.eclipse.persistence.internal.jpa.metamodel.MetamodelImpl.initialize(MetamodelImpl.java:370)        at
org.eclipse.persistence.internal.jpa.metamodel.MetamodelImpl.<init>(MetamodelImpl.java:101)        at
org.eclipse.persistence.internal.jpa.metamodel.MetamodelImpl.<init>(MetamodelImpl.java:120)        at
org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.getMetamodel(EntityManagerSetupImpl.java:1939)        at
org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.getMetamodel(EntityManagerFactoryImpl.java:472)        at
org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.getCriteriaBuilder(EntityManagerFactoryImpl.java:456)        at
com.sun.enterprise.container.common.impl.EntityManagerFactoryWrapper.getCriteriaBuilder(EntityManagerFactoryWrapper.java:109)        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)         at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)        at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)        at java.lang.reflect.Method.invoke(Method.java:597)         at
org.jboss.weld.bean.proxy.ClientProxyMethodHandler.invoke(ClientProxyMethodHandler.java:113)        at
javax.persistence.EntityManagerFactory_$$_javassist_32.getCriteriaBuilder(EntityManagerFactory_$$_javassist_32.java)        at
com.fpf.model.security.dao.UsuarioDAO.getUsuario(UsuarioDAO.java:28)        at
com.fpf.web.security.login.Authenticator.isLogged(Authenticator.java:48)        at
com.fpf.web.security.login.Authenticator.authenticate(Authenticator.java:37)        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)         at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)        at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)        at java.lang.reflect.Method.invoke(Method.java:597)         at
org.jboss.weld.bean.proxy.ClientProxyMethodHandler.invoke(ClientProxyMethodHandler.java:113)        at
com.fpf.web.security.login.Authenticator_$$_javassist_31.authenticate(Authenticator_$$_javassist_31.java)        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)         at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)        at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)        at java.lang.reflect.Method.invoke(Method.java:597)         at com.sun.el.parser.AstValue.invoke(AstValue.java:234)         at
com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:297)        at
org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:43)        at
org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:72)        at
com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:98)        at
javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
I have created by myself the entity and the metaModel
@StaticMetamodel( Usuario.class ) public abstract class Usuario_ {
       public static volatile SingularAttribute<Usuario, Integer> id;         public static volatile SingularAttribute<Usuario, String> nome;         public static volatile SingularAttribute<Usuario, String> password;         public static volatile SingularAttribute<Usuario, String> login;         public static volatile SingularAttribute<Usuario, Status> status;         }
but its not working, so, what could be wrong ?? is there any docs for
configure eclipse to auto generate these metamodels ?? ty
sry about english.
 

_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users

_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users


Back to the top