openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Pinaki Poddar <>
Subject Re: How to find entity belong which EntityManger?
Date Mon, 13 Jun 2011 20:25:52 GMT

   MetaModel is not static. Every persistence unit (i.e.
EntityManagerFactory) is associated with a MetaModel.

   My guess is you are referring to "static metamodel" as the canonical
metamodel classes generated at compile time. 

  These two, however, are very different concepts.   

Let us discuss the later first. Assume that you have @Entity classe P
defined in a persistent unit U1. 

When you compile, you can activate annotation processing as follows:

$ javac -Aopenjpa.metamodel=true 

This will activate an OpenJPA-specific Annotation Processor and that
processor will generate source code for which would be available
alongside will also be compiled to P_.class.

Class P_ is called static canonical metamodel for class P. 
'Static' because all variables in are static. Best is to take a look

P_.class provides a means to write Criteria query that a Java compiler can
verify for correctness. A more detailed description and usage of Criteria
query can be found at [1].

If you are not using Criteria query, then these canonical metamodel classes
are irrelevant.

Now let us talk about metamodel per se. Every persistent unit has a
metamodel during runtime, irrespective of whether you have generated
'canonical metamodel classes' or not during compile time.

You obtain a reference to it as you have done in your code

But then your interpretation of its content is incorrect.
-------------- your code ------------  
Set&lt;EntityType&lt;?&gt;> entities = emDB2.getMetamodel().getEntities();
Set&lt;EntityType&lt;?&gt;> entities2 =
// contains method returns false
if (entities2.contains(fnDO)) {
em = emSqlServer;
} else if (entities.contains(fnDO)) {
em = emDB2;

The element of the set 'entities' or 'entities2' are EntityType (you may
think of them as persistent equivalent of java.lang.Class). 

'fnDO', on the other hand, is an instance of a persistent entity (if I have
guessed it right). No wonder that the entities.contains(fnDO) returns false.   

A method like this may work (I am writing this in an email, so no warranty;)

  * Affirms if the class of the given instance belongs to the given
boolean contains(Metamodel m, Object instance) {
   Set&lt;EntityType&lt;?&gt;> pTypes = m.getEntities();
   for (EntityType<?> pType : pTypes) {
        if (instance.getClass() == pType.getJavaType()) 
              return true;
   return false;

View this message in context:
Sent from the OpenJPA Users mailing list archive at

View raw message