views:

2162

answers:

4

I am specifically looking for JPA code generation technique

First, what are all the project could generate JPA complaint 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.

A: 

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.

cletus
+3  A: 

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.
Kariem
A: 

check out JPM2java, Its a code generator for JPA. The only catch is it does not generate JPA code from SQL files or table, you'd need a orm.xml file. If you are looking for a tool to generate code directly from tables, you may want to try Netbeans. It has options to generate JPA code directly from tables

+1  A: 

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.

snowflake