I'm trying to understand how to best generate and synchronize domain model POJO's from my database using Hibernate. Right now the process I managed to build is the following:
- Build the ER schema on the database
- Have an hibernate.reveng.xml file containing the elements (one for each table)
- Use JBoss tools on eclipse to run a code generation configuration where I set the target package and location, the aforementioned reveng.xml file and get generated POJO's, mapping files and hibernate.cfg.xml files
But this has a lot of problems:
- I cannot map common fields (ID, created by, modified by, etc..) to a particular base entity.
- I have to manage a lot of mapping files (doesn't seem to generate a single one)
- I cannot generate a basePojo and have my extended one so that my modifications on the POJO's aren't overriden by the next code generation.
- I cannot fine tune the output location of the generated artifacts (mappings, .cfg and Pojos) they all go into the same base folder (POJO's are placed according to the package name I set)
Is it possible to "tell" the generator to map the common table fields to the same classe (createdBy, ModifiedBy, ID, etc...) ?
I'm questioning if this approach makes sense at all? Should I be managing my POJO's by hand ? I don't mind that but some help managing the mapping files (.hbm.xml) would help a lot.
Or should I find some way to go "code first", ie. write the POJO's and then generate the schema ? I'm a bit used to the .NET's entity framework and I feel quite lost on what's the "proper" way to build the persistence layer in Java/Hibernate
Thank you
The Telosys Tools code generator is probably the solution for you.
It uses an existing database to generate any kind of source file for each entity (database table), typically POJO, DTO, DAO, web pages, etc... When the database schema change you just have to regenerate.
For more information see the web site : https://sites.google.com/site/telosystools/ and the tutorials : https://sites.google.com/site/telosystutorial/
All the templates are free and customizable, for Hibernate POJO you can use the JPA templates (https://github.com/telosys-tools/persistence-jpa-TT210-R2) and adapt them if necessary