Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [dali-dev] Re: help with Dali 2.0 model migration - how do I now get entities?

Hi Tom,
I was purposefully vague about the helper API :) We really just haven't decided how much helper API we should have. We decided to continue cleaning up the current provisional api and then making decisions for things like this. There are a lot of options and some of the could just cause more confusion than they solve. JpaProject.persistentTypes(), JpaProject.entities(), jpaProject.javaEntities(), jpaProject.ormEntities(), jpaProject.mappedSuperclasses(), jpaProject.typeMappings(mappingKey: String) etc. And what does jpaProject.javaEntities() return, just the entities that are referenced(or implied) in the persistence-unit as class elements? Or should it return the ones that are referenced as a result of being listed in the orm.xml? Do we remove duplicates, like if it's listed as a class as well as in the orm.xml? Just a lot of questions to answer before we decide on the API. Of course any suggestions/contributions are welcome :)

I see an internal way for you to get the emf Resource. Could you enter a bug explaining your use case and then we can add this to the provisional api? Currently, the best way I am seeing is to call OrmXml/PersistenceXml.resource() will return you the IFile. Then call JpaProject.jpaFile(IFile). Then ((OrmResourceModel) JpaFile.resourceModel()).resource() will return you the OrmResource which you can call save(null) on. I *think* this should work by asking OrmXml to return the OrmResource, but that's a bit confusing since we already have a method resource() that is returning the IFile.

Just today I saw issues in the latest WTP code related to calling eResource().save(null) in our tests. I'll let you know if I uncover anything while looking at this, it doesn't seem to be causing problems in our UI, but sounds like it would definitely cause you problems with your use cases.

Karen

Tom Mutdosch wrote:

Hi Karen,

Thanks for the pointers - that looks to give me what I need. Out of curiosity - you mentioned that you hadn't decided on helper API for this. eg: JpaProject.getPersistentTypes(). I thought that getting all entities like that might be somewhat common usage of the model -- or is there specifically no API as getting the entity persistent types is considered somewhat of a little-used used case?

The only other issue I'm running into in migrating to the new codebase is being able to save the persistence.xml and orm.xml files via the model. I have code that gets those Dali models and adds properties to them (such as a default schema in the orm.xml, some properties in the persistence.xml). I found previously that if I did not save those models explicitly, the changes were not being persisted (most evident in the event that those files were not even open in an editor). I had been using the EMF eResource to save the models. For example, previously I was doing: PersistenceXmlRootContentNode pXMLRoot = (PersistenceXmlRootContentNode) persistenceXmlFiles.next().getContent();
Persistence persistence = pXMLRoot.getPersistence();
// make some changes to the persistence
persistence.eResource().save(*null*);

and:
XmlRootContentNode ormXMLRoot = (XmlRootContentNode) ormXMLFile.getContent();
EntityMappings entityMappings = ormXMLRoot.getEntityMappings();
// make some changes to orm xml model
entityMappings.eResource().save(*null*);

In the 2.0 codebase there are no longer any underlying eResource exposed. Is there a similar mechanism to force the underlying model files to save now? I see there's something like PersistenceUnit.update(...). Not sure if that's something I could use or not. Thanks for any help.

Thanks.
Tom


Inactive hide details for Karen Moore ---03/18/2008 02:23:24 PM---I will try sending this to the dali-dev mailing list, we did Karen Moore ---03/18/2008 02:23:24 PM---I will try sending this to the dali-dev mailing list, we did not receive

                        *Karen Moore <karen.moore@xxxxxxxxxx>*

                        03/18/2008 02:13 PM

	

To
	
Thomas F Mutdosch/Durham/IBM@IBMUS

cc
	
neil.hauge@xxxxxxxxxx, "General Dali EJB ORM developer discussion." <dali-dev@xxxxxxxxxxx>

Subject
	
Re: help with Dali 2.0 model migration - how do I now get entities?

	


I will try sending this to the dali-dev mailing list, we did not receive
it there

Hi Tom,

We have not decided about helper api to do this for you, but here is
code that should give you the same result as before.  It will give you
all the java entities  in your persistence unit, i've commented out some
code that would return those from the orm.xml files.

   public static List<PersistentType> entities(JpaProject jpaProject) {
       List<PersistentType> entities = new ArrayList<PersistentType>();
       //this is a place where our provisional api needs to change, I
had to cast to an internal class.
       GenericRootContextNode rootContext = ((GenericRootContextNode)
jpaProject.rootContext());
//You'll want null checks in here in cases of persistence.xml
file not being complete
       //Also, we only support 1 persistenceUnit in the implementation,
you should verify there is at least one
       PersistenceUnit persistenceUnit =
rootContext.persistenceXml().getPersistence().persistenceUnits().next();
for (Iterator<ClassRef> classRefs = persistenceUnit.classRefs();
classRefs.hasNext();) {
           ClassRef classRef = classRefs.next();
           if (classRef.getJavaPersistentType() != null) { //null if
there is no java class with this name)
               if (classRef.getJavaPersistentType().mappingKey() ==
MappingKeys.ENTITY_TYPE_MAPPING_KEY) {
                   entities.add(classRef.getJavaPersistentType());
               }
           }
       }
       //to get entities from orm.xml files
//        for (Iterator<MappingFileRef> mappingFiles =
persistenceUnit.mappingFileRefs(); mappingFiles.hasNext();) {
//            MappingFileRef mappingFileRef = mappingFiles.next();
// //null checks needed here for OrmXml as well as EntityMappings
//            EntityMappings entityMappings =
mappingFileRef.getOrmXml().getEntityMappings();
//            for (Iterator<OrmPersistentType> persistentTypes =
entityMappings.ormPersistentTypes(); persistentTypes.hasNext();) {
//                OrmPersistentType ormPersistentType =
persistentTypes.next();
//                if (ormPersistentType.mappingKey() ==
MappingKeys.ENTITY_TYPE_MAPPING_KEY) {
//                    entities.add(ormPersistentType);
//                }
//            }
//        }
return entities;
   }



Thomas F Mutdosch wrote:

> Hi guys,
>
> I tried sending this to the dali-dev list this morning but I don't see
> it in the archives yet, so not sure if it went through. I was just
> wondering if I could get a couple pointers on getting all of the
> entities in a project with the latest Dali 2.0 code. Sorry to bother
> you directly - I'm just trying to get our build back in a compilable
> state at the moment :)
>
> --
> I am migrating to the new Dali 2.0 models, and am having trouble
> figuring out how to get the entities, and persistence and orm files
> from a JpaProject now.  Here is the previous code that I was using to
> get these resources.  Is there a doc detailing these changes, or could
> someone just help me with the new mechanism to do this?  Thanks for
> any help.
>
> Here's how I was getting all entities previously. I couldn't find a
> direct mapping to get all of the PersistentTypes from the JpaProject now.
>     public static List<IPersistentType> getEntities( IJpaProject
> jpaProject ) {
> List<IPersistentType> entities = new ArrayList<IPersistentType>();
>        Iterator<IJpaFile> iter = jpaProject.jpaFiles();
>        while ( iter.hasNext() ) {
>            IJpaFile jpaFile = iter.next();
>            if ( jpaFile.getContentId().equals(
> JavaJpaFileContentProvider.instance().contentType()) ) {
>                JpaCompilationUnit jcu =
> (JpaCompilationUnit)jpaFile.getContent();
>                EList<JavaPersistentType> list = jcu.getTypes();
>                for ( JavaPersistentType jpType : list ) {
>                    if ( jpType.getMappingKey() !=
> IMappingKeys.NULL_TYPE_MAPPING_KEY
>                                &&  jpType.getMappingKey().equals(
> IMappingKeys.ENTITY_TYPE_MAPPING_KEY )) {
>                        entities.add(jpType);
>                    }
>                }
>            }
>        }
>        return entities;
>    }
>
>
> And here's how I was similary getting the persistence.xml file(s)
> using the old code:
>        for( Iterator<IJpaFile> persistenceXmlFiles =
> project.jpaFiles(PersistenceXmlJpaFileContentProvider.instance().contentType());
>
>            persistenceXmlFiles.hasNext();) {
>            PersistenceXmlRootContentNode pXMLRoot =
> (PersistenceXmlRootContentNode) persistenceXmlFiles.next().getContent();
>            Persistence persistence = pXMLRoot.getPersistence();
>            if ( persistence != null ) {
>                EList pUnits = persistence.getPersistenceUnits();
>                try {
>                    PersistenceUnit unit = (PersistenceUnit)
> pUnits.get(0);
>                    puName = unit.getName();
>                }
>                catch (RuntimeException e) {
>                }
>            }
>            break;
>        }
>
> and the same for orm.xml...
> Thanks
> Tom
>
> Thanks.
> -Tom Mutdosch
> Rational Application Developer
> Web Tooling - Software Developer
> 919-254-9841 t/l:444-9841
>

------------------------------------------------------------------------

_______________________________________________
dali-dev mailing list
dali-dev@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/dali-dev


Back to the top