I am new to EclipseLink. I am trying to generate orm mappings for a class during runtime and do mapping. Is it possible at all? I see examples where a class is generated during runtime but that doesn't fit my situation. thanks
1 Answers
It could be possible, depending on what you are trying to do and when. Persistence units are pretty static creations that should be known upfront - just like java classes themselves. So if you are not using Dynamic entities, why wouldn't you know upfront that the class should be apart of the persistence unit up front?
While it is not a great idea, you could create a static persistence unit and specify that it use a customizer as described here http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Advanced_JPA_Development/Customizers with which you could add in descriptors or mappings to the persistence unit. The customizer is only run once though, during initialization. So if you wanted to make changes later on, you would need to refresh the persistence unit using the refreshMetadata on the EntityManagerFactory to have it reload the persistence unit. Running EntityManagers will not be affected by the changes.
Using the EMF refreshMetadata, you could also use a MetadataRepository to pick up different or extended ORM.xml files for your entities - so you could incorporate changes made to the xml instead of using a customizer. This is described somewhat here: http://www.eclipse.org/eclipselink/documentation/2.5/solutions/extensible001.htm#CIAIJHAG

- 20,138
- 2
- 29
- 43
-
Hi Chris thanks for your reply. I am trying to store an object that is going to be created during runtime (known only during runtime). So Customizers may not be correct solutions. Even 'Extensible entities' may not fit as I can't pre-define the number of extra columns in table as it may change. So, is there a solution for such problem with EclipseLink? thanks Gopi – PGK Apr 15 '13 at 06:33
-
The last link was not to suggest extensible entities, but to use the same API to reload the metadata. In this way you can implement a repository that adds in your class to a running persistence unit. – Chris Apr 15 '13 at 11:54
-
Chris, thanks bunch. Finally made it working. Appreciate your time and help. – PGK Apr 16 '13 at 21:37