openjpa-users mailing list archives

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

   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 P.java, you can activate annotation processing as follows:

$ javac -Aopenjpa.metamodel=true P.java 

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

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

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
emf.getMetamodel().

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 =
emSqlServer.getMetamodel().getEntities();
   
// 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
metamodel.
**/
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;
}
  

-----
Pinaki 
--
View this message in context: http://openjpa.208410.n2.nabble.com/How-to-find-entity-belong-which-EntityManger-tp6470903p6471682.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Mime
View raw message