Community
Participate
Working Groups
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); } ... ...
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); }
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
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.
I believe that the fix for https://bugs.eclipse.org/bugs/show_bug.cgi?id=271847 has addressed the root of this problem.
(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.
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)