Hibernate Domain Object Generation - java

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

Related

Need to extract Java classes from Oracle SQL Developer Data Modeler model

I have a couple of pretty complex Logical Models in Oracle's Data Modeler (each one has a .dmd file and its corresponding folder structure) and I need to create POJOs from those entities.
I could go entity by entity and code the classes by hand but that would take an incredible amount of time.
Is there any easy way to export the entities and their relations to my Java Project?
I tried to export the model to a CSV file but that didn't even work. No file was created. I think it didn't work because it tried to export the Relational Model bit from the structure (which is empty) and not the Logical Model. But since this is the first time I'm seeing Oracle's Data Modeler I couldn't be certain that was it

Hibernate : from table to class

As we know, Hibernate allows you to persist a class into a table.
But, can we do the contrary ? Can we directly create a class from an available table ? Can we directly create objects from the lines of an available table ?
Thank you everyone,
You can generate entity classes from database tables at design/development time with the help of Hibernate Tools. Here is a basic example of it. Before creating any object of an entity, its class must exists.
From the documentation:
several wizards are provided with the Hibernate Eclipse tools. You can use a wizard to quickly generate Hibernate configuration (cfg.xml) files, or to reverse engineer an existing database schema into POJO source files and Hibernate mapping files. The reverse engineering wizard supports customizable templates.
Creating objects from rows in a database table is the primary job of an ORM. So of course that is possible too.

Convert classes relationships in Java into database schema

What is the best tool/framework to convert classes into database schema automatically without XML mapping files?
My problem is this, I have around 20 classes with different relationships (association, inner classes, etc) to each others and I want to convert all that into a database tables to save all these data along with its relationships.
I tried to see some tutorials to Hibernate and found out that it requires building a mapping XML which is going to be very painful to my case.
Any framework to automate this?
Alternatively, you can annotate your classes instead of using XML and then generate the schema from your entities. Anyway, I strongly recommend writing the classes in a way that is easy to use with an OR mapper. It would have been best to design your model for the use of Hibernate beforehand.

Migrating Data accross different DB Schema

I want to migrate my data from one DB to other using Java. Both DBs have different schema structure. I might also need to define some mapping / validation rule. Can anyone please guide me about any strategy, framework or any opensource project.
Thanks
Isn't in this case I have to create all the POJO to match the both schema (even by auto generating). Is there any way to avoid this thing i.e. giving schema mapping and generating POJO on fly in memory ?
Any idea?
Thanks
Yes, you need an Extract-Transform-Load (ETL) tool.
Here are some open source choices:
http://www.google.com/search?gcx=w&sourceid=chrome&ie=UTF-8&q=open+source+etl
ETL is generally used for this as in duffymo's answer.. you could also try ORM tools for this:
There is the Torque project.. http://db.apache.org/torque/
Read the data from your existing schema into java objects, then set them into the other objects for the other schema and then save them into the database. I am pretty sure hibernate also can be used, although I havent used hibernate per se. It works on the same way as torque..

How to generate orm mapping classes from sql schema in Java

I have an existing sql schema file for db. Is it possible to generate and re-generate when needed DAO's entities and all other required helper/client classes to access it? I don't mind what will it be -- hibernate, other jpa or something else.
Asuming you/others are still looking for a solution:
I just got the same problem and got it working in Eclipse (slightly different) as follows:
created JPA Project and downloaded & added user library in the wizard
Also wanted to give a schema-sql-file as input but instead found a way to take an actual db as input. (That was surely much easier for the developers of the tool to process than parsing proprietary sql-script-files)
To do that "rightclick" you jpa project an there "new/other/jpa/entities from tables"
In the following Wizard you have to create a db-connection to the db whose schema you want to get as jpa-annotated POJOs (IMHO It's very intuitive..but you may ask if there is a problem)
After finishing all jpa-classes are generated from the db...saved me from a lot of dummy work :)

Categories