Stefan,
JpaProject does not directly hold a collection of entities. Entities
can be added or removed from a number
of different places within a JpaProject:
- They can be listed in the persistence.xml file.
- They can be discovered automatically by Dali, when they are annotated.
- They can be contained in jar files listed in the
persistence.xml file.
- They can be listed in mapping files listed in the persistence.xml
file.
- They can be listed in the implied mapping file
(META-INF/orm.xml) even if it is not
listed in the persistence.xml file.
To reach these objects and listen to them appropriately,
you can use the
following methods:
To get a PersistenceUnit use these methods:
JpaProject.getRootContextNode() - this will always be present
JpaRootContextNode.getPersistenceXml() - this may null
JpaPersistenceXml.getPersistence() - this may null
Persistence.persistenceUnit() - this will either be empty or contain a
single persistence unit
(as Dali only supports a single persistence unit)
Once you have a PersistenceUnit, there a number of different objects
you need
to monitor for entities:
- the types listed in the persistence.xml file:
PersistenceUnit.specifiedClassRefs() - this may be empty
ClassRef.getJavaPersistentType() - this may be null
PersistentType.getMappingKey() - this will indicate
whether the persistent type is an entity
- the types discovered automatically:
PersistenceUnit.impliedClassRefs() - this may be empty
[see above]
- the types in jar files listed in the persistence.xml
file:
PersistenceUnit.jarFileRefs() - this may be empty
JarFileRef.getJarFile() - this maybe null
JarFile.javaPersistentTypes() - this may be empty
PersistentType.getMappingKey() -
this will indicate whether the persistent type is an entity
- the types in mapping files listed in the persistence.xml file:
PersistenceUnit.specifiedMappingFileRefs() - this may be
empty
MappingFileRef.getMappingFile() - this may be null, and you will
need to check whether it is an
instance of OrmXml then cast it
OrmXml.getRoot() - this may be null
EntityMappings.getPersistentTypes() - this may be empty
PersistentType.getMappingKey() - this will indicate whether the
persistent type is an entity
- the types in the default mapping file:
PersistenceUnit.getImpliedMappingFileRef() - this
may be empty
[see above]
So, you will need to listen to a number of objects' changes. For
example, to listen
for an entity being added or removed via the list of classes in the
persistence.xml file:
JpaProject added/removed:
JpaProjectManager.addCollectionListener(JpaProjectManager.JPA_PROJECTS_COLLECTION,
...)
[You can get the JpaProjectManager via the static method
JptCorePlugin.getJpaProjectManager().]
PersistenceXml added/removed:
JpaProject.getRootContextNode().addPropertyChangeListener(JpaRootContextNode.PERSISTENCE_XML_PROPERTY,
...)
Persistence added/removed:
PersistenceXml.addPropertyChangeListener(PersistenceXml.PERSISTENCE_PROPERTY,
...)
PersistenceUnit added/removed:
Persistence.addListChangeListener(Persistence.PERSISTENCE_UNITS_LIST,
...)
ClassRef added/removed:
PersistenceUnit.addListChangeListener(PersistenceUnit.SPECIFIED_CLASS_REFS_LIST,...)
PersistentType added/removed:
ClassRef.addPropertyChangeListener(ClassRef.JAVA_PERSISTENT_TYPE_PROPERTY,
...)
TypeMapping change:
PersistentType.addPropertyChangeListener(PersistentType.MAPPING_PROPERTY,
...)
Obviously, it is pretty complicated to listen for the adding/removing
of entities.
I'm not sure I've been exhaustive here, but it should get you started.
You might want to use some of our adapters to reduce any bugs and/or
listener leaks
(e.g. PropertyAspectAdapter).
Brian
On 06/23/10 10:51, Dimov, Stefan wrote:
I
have problems with listeners in Helios Dali. I don’t know what kind of
listener to add to org.eclipse.jpt.core.JpaProject in
order to detect adding and/or removing entities from this project.
I’ve tried CollectionChangeListener and ListChangeListener,
but seems that they both don’t work.
Can anyone give me a hint?
|