I am specifically looking for JPA code generation technique
First, what are all the project could generate JPA compliant code? (Eg. HibernateTools)
Second, I
I've used Dali Persistence Eclipse Plugin, The tool is available for download via the Indigo Java EE SR1 update site.
After the plugin is installed, to make a reverse engineering of your DB, you need to create a new JPA project, set database connection, launch the automated download of JPA runtime (in my case Eclipse Link), then start the generation process.
During the code generation process you're asked to provide details on table mappings and generated classes. At the end of the generation the code is clean.
Project lombok seems allowing you to generate basic named queries, this is another approach using annotations and code generation at compile time.
See:
A guy over the hibernate forum seems using a traditionnal code generation approach with Hibernate Tools : https://forum.hibernate.org/viewtopic.php?f=9&t=962223&p=2315766&hilit=named+queries+generate#p2315766
I agree with cletus on the point that you cannot generate all named queries, but I guess we can imagine generating basic named queries such finders based on one or several fields of the object.
I also have difficulties understanding the question, but I'll try to rephrase:
When you say "JPA java code generation", I understand generating JPA annotated model classes from a supplied DB connection. Most frameworks often refer to this as reverse engineering.
Now you have two questions:
To answer to the first question:
I really like the Netbeans code generation, especially if you show the results to someone not familiar with JPA.
At the level of customization I can only share the experience I had with Hibernate Tools. Especially for reproducible results, try to use the ant-based tasks. You can easily add some targets to your build and code generation can be done at two levels:
With the templates you should be able to cover most of the corporate standards. Look into the pojo
directory of the hibernate-tools package. The easiest way to customize the code generation is to copy and adapt the templates and have them put before the hibernate-tools.jar in the ant task used to create the pojos.
As already pointed out in another comment, it might be difficult to modify the generated code afterwards. I hope the following tips can help you:
@MappedSuperclass
for classes which you may want to adapt in a manual step.Another efficient solution for JPA code generation is "Telosys Tools"
An Eclipse plugin working from an existing database ( "database firts" approach ) with customizable templates. This solution is more flexible than Dali thanks to its lightweight model and the Velocity templates (shared on GitHub )
See the web site : http://www.telosys.org/
The plugin on Eclipse Marketplace : http://marketplace.eclipse.org/content/telosys-tools
A brief description of the principle : http://labs.sogeti.com/code-generation-can-it-be-simple-and-pragmatic/
For JPA generation, use the JPA templates available on GitHub : https://github.com/telosys-templates-v3
Ok, basically you have things the wrong way arond: JPA is the generation tool
.
I say this because the only thing you could generate JPA entities from is SQL and the whole point of JPA is to do things the other way around. You define your object model first and, from that, you can generate your tables and queries.
For example, I've seen projects use Hibernate to define their entities and then they have an ant build script that creates the database from the Hibernate entity model.
JPA entity definitions--especially done with annotations--aren't exactly onerous. They really are your best option as the first thing to do rather than being the product of something else.
Besides, another tool won't help you write named queries, define the correct cascade options on relationships, etc. And if you had generated code, how would you handle modifying it afterwards?
It's just not the right way to go.
The ideal tool/eclipse plugin for jpa code reverse generation is Hibernate Tools. This has now been made a part of JBoss Tools. So, in your eclipse start off with installing JBoss Tools.
Then create a JPA Project. This project will act as the holder of all your code/configurations related to the reverse generation project. Installing the JBoss Tools first gives you the advantage that your Hibernate Configuration(part of Hibernate Tools) gets created along with your JPA project.
Next step would be to use the Hibernate Tools to actually reverse generate your JPA POJO entities corresponding to your database tables.
To understand the steps for JPA POJO reverse generation in detail you can have a look at the following tutorial...http://www.javabrahman.com/j2ee/how-to-do-reverse-code-generation-of-hibernatejpa-pojo-entities-using-jboss-tools-eclipse-plugin/
The above tutorial also has links to tutorials for creating a JPA Project and also for installing JBoss Tools in your eclipse installation both of which are pre-requisites for JPA POJO entities reverse code generation.