This Bugzilla instance is deprecated, and most Eclipse projects now use GitHub or Eclipse GitLab. Please see the deprecation plan for details.
Bug 303684 - EntityManagerFactory.getMetamodel() throws NPE if Server Session not logged in first
Summary: EntityManagerFactory.getMetamodel() throws NPE if Server Session not logged i...
Status: RESOLVED DUPLICATE of bug 322585
Alias: None
Product: z_Archived
Classification: Eclipse Foundation
Component: Eclipselink (show other bugs)
Version: unspecified   Edit
Hardware: All All
: P2 normal (vote)
Target Milestone: ---   Edit
Assignee: Nobody - feel free to take it CLA
QA Contact:
URL: http://wiki.eclipse.org/EclipseLink/D...
Whiteboard:
Keywords:
Depends on: 266912 297928 322585
Blocks:
  Show dependency tree
 
Reported: 2010-02-23 15:12 EST by Doug Clarke CLA
Modified: 2022-06-09 10:28 EDT (History)
3 users (show)

See Also:
michael.f.obrien: documentation+


Attachments
Option 7 (in use): Do a getServerSession that does a deploy/login that calls initializeDescriptors only for early emf.getMetamodel() users - em.getMetamodel() users do not login (3.28 KB, patch)
2010-09-03 11:29 EDT, Michael OBrien CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Doug Clarke CLA 2010-02-23 15:12:32 EST
Example:

EntityManagerFactory emf = Persistence.createEntityManagerFactory("employee");
emf.getMetamodel();


If you caus ethe underlying server session to be created and initialized then this succeeds.
Comment 1 Tom Ware CLA 2010-03-02 11:33:17 EST
Setting target and priority.  See the following page for a description of the meanings of these values:

http://wiki.eclipse.org/EclipseLink/Development/Bugs/Guidelines
Comment 2 Pierce Wetter CLA 2010-03-12 13:27:05 EST
FYI, the problem is because RelationalDescriptor has javaClassName set but not javaClass. 

One fix might be to override javaClass to lookup the class by name and set it if its not set.
Comment 3 Tom Ware CLA 2010-03-12 13:31:56 EST
Note: The solution here is not as simple as getting the javaClass from the className.  The step in login that allows this to work is descriptor initialization - all descriptors must be initialized to allow our model generation to work.
Comment 4 Pierce Wetter CLA 2010-03-26 18:15:09 EDT
Just found out this can be really annoying, because if you have an EJB that calls getCriteriaBuilder BEFORE any database connection (like its going to query the database), then you get an NPE
Comment 5 Michael OBrien CLA 2010-08-16 11:29:19 EDT
>This issue is a duplicate of bug 322585 and is related to bug 297928
- an entityManagerFactory.getMetamodel() is invalid before at least one entityManager.deploy()
- a refactor so that we can use the metamdoel on the EMF before creating an entityManager is being designed for bug 322585 in DI 106
http://wiki.eclipse.org/EclipseLink/Development/JPA_2.0/metamodel_api#DI_106:_20100810:_322585:Metamodel_initialization_via_EMF.getMetamodel.28.29_before_EM_deploy_results_in_an_invalid_Metamodel_because_of_UNINITIALIZED_descriptor.initializationStage
- Currently metamodel initialization is split into 2 parts (emf.predeploy() and em.deploy()
Comment 6 Michael OBrien CLA 2010-08-16 11:30:59 EDT
>This work will be done in bug # 322585

*** This bug has been marked as a duplicate of bug 322585 ***
Comment 7 Michael OBrien CLA 2010-08-18 08:55:26 EDT
>Fix Summary/Workaround
Essentially this fix in bug # 322585 just makes validation default to true
unless disabled
- validation is off by default
- login during emf predeploy is now on by default
>workaround is to enable validation in persistence.xml
The following property is named as though the persistence unit is "only" used
for validation.  
In fact it validates the persistence unit by doing a deploy just after the
predeploy - without waiting for client code to login - after that it behaves
normally.
This solves our issue where we may get a NPE on an unitialized descriptor's
javaClass (was null) set on a Metamodel ManagedType

     <property name="eclipselink.validation-only" value="True"/>
Comment 8 Michael OBrien CLA 2010-08-18 20:07:27 EDT
>Also note that by running getServerSession() or login() via deploy() in the predeploy() we get the proper classLoader
- we cannot just call getClassForClassName() - as we need to wait until we have the real classLoader not the JEE temporary classLoader
Comment 9 Michael OBrien CLA 2010-09-03 11:29:11 EDT
Created attachment 178166 [details]
Option 7 (in use): Do a getServerSession that does a deploy/login that calls initializeDescriptors only for early emf.getMetamodel() users - em.getMetamodel() users do not login
Comment 10 Michael OBrien CLA 2010-09-03 15:20:01 EDT
>See the fix for bug # 322585 in SVN rev# 8141 for 2.2 and 8142 for 2.1.2
https://fisheye2.atlassian.com/changelog/eclipselink/?cs=8141
We now do a preemptive DB login via getServerSession to force initializeDescriptors so that a MappedSuperclass javaClass is set on the descriptor when a client does an emf.getMetamodel() or emf.getCriteriaBuilder() before at least one EntityManager is created or logged in.
If a NullPointerException occurs on early post-predeploy but pre-deploy access to the Metamodel via an EntityManagerFactory.getMetamodel() or emf.getCriteriaBuilder() call before an initial login to the DB - then reopen this bug.
NPE in ManagedTypeImpl.getTypeClassFromAttributeOrMethodLevelAccessor
NPE in MetamodelImpl.initialize
Comment 11 Eclipse Webmaster CLA 2022-06-09 10:28:23 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink