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?

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