JPA java code generation

58,461

Solution 1

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:

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:

  • 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.

Solution 2

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

Solution 3

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.

Solution 4

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.

Solution 5

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.

Share:
58,461
iddqd
Author by

iddqd

Updated on February 23, 2020

Comments

  • iddqd
    iddqd about 4 years

    I am specifically looking for JPA code generation technique

    First, what are all the project could generate JPA compliant code? (Eg. HibernateTools)

    Second, I would also like to customize the code generation utility, as it has to compliant to our corporate standards.

    If not, what are all the framework available to generate java code using reflection? so I can write from scratch.

    Note: I used eclipse to generate JPA code and refactor it repeatedly to make it compliant.