简体   繁体   中英

JPA Metamodel and entity classes

Can I create the metamodel's entity classes at runtime or must I create them manually?

If I can create them at runtime (using EntityType I suppose) what are the advantages and disadvantages of this solution?

Ok, there was a little misterunderstanding... I thought that I should create N classes with name EntityClass_ for all my entity classes... Now I note that Netbeans creates the files EntityClass_.class when it build the project and I can reffer to them! For more informations, for activate this option in netbeans here it is the link at a web page that explain this option:

http://blogs.oracle.com/arungupta/entry/totd_148_jpa2_metamodel_classes

Ok, the problem now is: how I can choose the right answer? :) Sorry Seam but Piotr was more clear and specific....

Having a static metamodel only makes sense if it is generated at compile-time, because it's a compile-time safe technique to ensure that you are linking against properties that actually exist. There is no runtime advantage to using the static metamodel as opposed to dynamically looking up properties, it's entirely about avoiding programmer errors.

You can create a Metamodel by yourself or you can use Metamodel Generator to do that for you. This generator is specific to the JPA provider and, ie for EclipseLink it can be executed like this:

javac -processor org.eclipse.persistence.internal.jpa.modelgen.CanonicalModelProcessor 
      -proc:only 
      -classpath lib/*.jar;punit 
      *.java

However it is not executed at runtime but ie while building your application or as a separate process. There is no much sense to do it at runtime, as you need this (of course if you want to use it) metamodel while creating your queries, so in development phase.

Just to make this answer more complete - you can also access the Metamodel without specifying it, using EntityManager#getMetamodel() or EntityManagerFactory#getMetamodel() .

I would definitely opt for generating metamodel explicitly and using it while crafting Criteria API queries. It just makes it more clear, obvious and let you find errors during development phase.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM