Skip to main content

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

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


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
>

Back to the top