Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 223197 - API for easily saving OrmXML and PersistentXML models
Summary: API for easily saving OrmXML and PersistentXML models
Status: RESOLVED FIXED
Alias: None
Product: Dali JPA Tools
Classification: WebTools
Component: Framework (show other bugs)
Version: 2.0   Edit
Hardware: PC Windows XP
: P3 enhancement (vote)
Target Milestone: 2.3   Edit
Assignee: Neil Hauge CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-03-19 11:04 EDT by Tom Mutdosch CLA
Modified: 2010-02-09 13:34 EST (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Tom Mutdosch CLA 2008-03-19 11:04:56 EDT
I have use cases where I am updating the OrmXml and PersistenceXml models with new values, and then want to save the model out.  I would like to request a nice API on the model to allow me to do this.  Currently I have to get a JPAFile on the resource and then get the resourceModel for that resource and save it out there:

// save resource out
IResource ormXMLFile = ormXml.resource();
JpaFile jpaFile = jpaProject.jpaFile((IFile)ormXMLFile);
OrmResource ormResource = ((OrmResourceModel) jpaFile.getResourceModel()).resource();
try {
	ormResource.save(null);
}

It would be nice to be able to call something like OrmXml.save().

Background on my use case:  I have a wizard that runs and updates your configuration files with user-specified schema information.  The orm.xml/persistence.xml are updated with the new schema: the orm.xml updated with the schema in the persistence-unit-defaults and a property in the persistence.xml, eg:  openjpa.jdbc.Schema="newschema"
These configuration files are likely not even open at that time, so I need to save the model contents to persist my changes; otherwise they are not saved.

Here's the current block of code that I am using to update the schema in the orm.xml:


PersistenceXml persistenceXml = ((BaseJpaContent) jpaProject.contextModel()).getPersistenceXml();
PersistenceUnit persistenceUnit =		persistenceXml.getPersistence().persistenceUnits().next();
for (Iterator<MappingFileRef> mappingFiles =	persistenceUnit.mappingFileRefs(); mappingFiles.hasNext();) {
    MappingFileRef mappingFileRef = mappingFiles.next();
	OrmXml ormXml = mappingFileRef.getOrmXml();
	if ( ormXml != null ) { 
		EntityMappings entityMappings =	ormXml.getEntityMappings();
		if ( entityMappings != null ) { 
            PersistenceUnitMetadata persistenceUnitMetadata = entityMappings.getPersistenceUnitMetadata(); // CHECKME: ensure these aren't null if they don't exist
			PersistenceUnitDefaults persistenceUnitDefaults = persistenceUnitMetadata.getPersistenceUnitDefaults();
			String schema = persistenceUnitDefaults.getSchema();
			if ( schema == null || !schema.equals( newSchemaName) ) {
				persistenceUnitDefaults.setSchema(newSchemaName);
				
				// save resource out
				IResource ormXMLFile = ormXml.resource();
				JpaFile jpaFile = jpaProject.jpaFile((IFile)ormXMLFile);
				OrmResource ormResource = ((OrmResourceModel) jpaFile.getResourceModel()).resource();
				try {
					ormResource.save(null);
				}
				...
				...
Comment 1 Paul Fullbright CLA 2008-03-20 11:23:23 EDT
I'd think we could add the ability to retrieve at least the resource from the context-level model.

Then at least you'd be down to this:

// save resource out
OrmResource ormResource = ormXml.eResource();
try {
        ormResource.save(null);
}
Comment 2 Karen Butzke CLA 2008-03-20 12:12:40 EDT
As of recent changes calling ormResource.save(null)  unloads the resource model. I am only assuming that this is not a bug, it *seems* to make sense, but it is certainly a change in how things have been working.

So, we would need a way to load the resource, make changes, and then save thus unloading the resource.  The resource is not unloaded if it is open in the UI when it is saved
Comment 3 Neil Hauge CLA 2008-03-27 15:42:24 EDT
We ran out of time to get this into M6.  Given that there is a reasonable workaround for this, I am thinking we should defer post 2.0.

Let me know if this causes problems for anyone.
Comment 4 Neil Hauge CLA 2009-06-02 11:28:45 EDT
I believe that the fix for https://bugs.eclipse.org/bugs/show_bug.cgi?id=271847 has addressed the root of this problem. 
Comment 5 Neil Hauge CLA 2009-06-02 11:30:49 EDT
(In reply to comment #4)
> I believe that the fix for https://bugs.eclipse.org/bugs/show_bug.cgi?id=271847
> has addressed the root of this problem. 
> 

Sorry...this comment was meant for bug 184470.
Comment 6 Neil Hauge CLA 2010-02-09 13:34:09 EST
Upon review it appears this enhancement is probably sufficiently resolved.  Given current API in 2.3 you could now do the following:

ormXml.getXmlResource().save(Collections.EMPTY_MAP)