Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 352506 - Ability to dynamically create a new persistence unit
Summary: Ability to dynamically create a new persistence unit
Status: ASSIGNED
Alias: None
Product: z_Archived
Classification: Eclipse Foundation
Component: Eclipselink (show other bugs)
Version: unspecified   Edit
Hardware: PC Windows XP
: P3 enhancement with 5 votes (vote)
Target Milestone: ---   Edit
Assignee: Nobody - feel free to take it CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-07-19 16:39 EDT by Michael Keith CLA
Modified: 2022-06-09 10:33 EDT (History)
12 users (show)

See Also:


Attachments
Proposed changes (689.08 KB, patch)
2013-09-09 10:04 EDT, Guy Pelletier CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Michael Keith CLA 2011-07-19 16:39:38 EDT
Some people would like to be able to dynamically create persistence units, and I don't think there is any reason why EL couldn't do this. By passing in all of the info as properties an EMF might be created as in the following example:

map.put("org.eclipse.persistence.punit.mode", "Create");
map.put("org.eclipse.persistence.punit.name", "NewPU");
map.put("org.eclipse.persistence.punit.transactionType", "RESOURCE-LOCAL");
map.put("org.eclipse.persistence.punit.entities", 
        "myModel.EntityA, myModel.EntityB, myModel.EntityC");
map.put("javax.persistence.jdbc.url", ...);
...
map.put("javax.persistence.jdbc.password", ...);
emf = Persistence.createEntityManagerFactory(null, map);

There would need to be some kind of flag (e.g. mode) to indicate we should not go looking for a p-unit of the given name and that it is not an error when it is not there. In fact it should be an error if one did already exist with that name.

Of course there are some assumptions, such as mapping using annotations, and the classes not having been already loaded in order to weave, and so on, but we already support a bare bones persistence.xml so why not just allow the unit name and others to be supplied as well?
Comment 1 Gunnar Wagenknecht CLA 2011-07-21 14:42:47 EDT
May I susgest that instead of providing just the class names one can put the real class objects of the entities as collection into the map? This would solve some class loading issues. EL wouldn't have to deal with ClassLoaders because the entity classes are already there.

Although, I have to admit that the whole thing about weaving at runtime and adding fragments to bundles (for the sake of additional imports) is still a bit of magic to me. Thus it may not be possible. But I'm wondering, if one passes the class objects of entities into EL in order to create a PU, is it still necessary to create the fragment?
Comment 2 Gunnar Wagenknecht CLA 2011-07-21 14:51:07 EDT
Ok, scratch my last comment. I think I now realized how weaving works. You are actually replacing the class when it is loaded. Thus, the ClassLoader gets the the modified bytes before it actually loads the class. Thus, you need to add the dynamic imports to the bundle.

Does that support use the weaving hook provided by OSGi?
Comment 3 Michael Keith CLA 2011-07-21 16:10:19 EDT
> Does that support use the weaving hook provided by OSGi?

Am actually in the process right now of changing the weaving to use the new weaving hooks. (Was waiting for the standard to become final, and the latest Equinox release to be done).
Comment 4 Eduard Bartsch CLA 2011-07-26 12:07:01 EDT
Will there be a spec/concept document that describes the proposal? 

I have some questions for the concept:

- What will be the lifecycle of the dynamically created PU? Will it stay as long as Java VM runs or will it be shut down when the EMF is closed?
- What about conflicting weaving settings for dynamic PUs? At the end, the same entity classes will be used in multiple PUs.  
- Would it be possible to specify only some settings and reuse others from a persistence.xml? This would come closer to our use case where we only want to dynamically override JDBC URL/user/pwd and not the other EclipseLink settings defined statically by the application.
Comment 5 Amit B CLA 2013-07-31 16:39:22 EDT
Any chance this is going to be implemented or already done ?

Thankyou.
Comment 6 Tom Ware CLA 2013-08-16 15:04:59 EDT
There is no API-based way to create a persistence unit, but there is a way to specify a persistence.xml file at bootstrap time.

Using that, you could dyanmically build up a persistence.xml with references to anything legal in EclipseLink (including our dynamic classes) and give us a reference to that at bootstrap time using a URL

'not quite an API-based dynamic bootstrap, but not too far off.
Comment 7 Guy Pelletier CLA 2013-09-09 10:04:13 EDT
Created attachment 235304 [details]
Proposed changes
Comment 8 Eclipse Webmaster CLA 2022-06-09 10:33:01 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink