Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA java code generation

I also have difficulties understanding the question, but I'll try to rephrase:

  • You have a lot of data in a DB and want to access it via JPA
  • You don't want to manually write the classes to access the different DBs/tables
  • Currently all/most of your model classes are generated from within Eclipse
  • These models have JPA annotations
  • The model classes (or the annotations) are not according to corporate standards

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:

  1. What code generators can be recommended to generate JPA annotated classes?
  2. Is it possible to customize the output of these frameworks, and in which way?

To answer to the first question:

  • Hibernate Tools - already mentioned in the question, but I prefer this one
  • EclipseLink
  • Netbeans

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:

  • Configure exporters using properties
  • Provide custom freemarker templates

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:

  • Try to separate generated and customized source files in different folders.
  • Think about using @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


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.


Minuteproject is a generator tool and can generate JPA1/ JPA2 as well as hibernate ORM-like artifacts. It is based on reverse-engineering from the database. You can instruct the generator to apply convention for your java code that do not follow your DB convention, but the mapping will be correct. (Example strip DB name prefix; table starting with ADMIN_ such as ADMIN_ENVIRONMENT are Environment (w/out Admin) as java class) For the moment there 20+ conventions that help you reshape your model to be less DB look-and-feel by more Java-OO-friendly.

Another interesting feature is updatable-code enabling to modify both the generated code and your model AND aht the next generation your modifications will be kept! The generator makes the merge.

The templates are opensource and work with velocity, it is 'quite' easy to append a track to make specific one for your framework (ex security aspects... that are relevant to your organisation). You can scope your template to field level, entity (table or view), package (group of entities), model, application providing flexibility, and since the template of a track knows each other via metadata it is quite easy to reference then from other templates and to associate them by configuration with naming convention.