How to generate orm mapping classes from sql schema in Java - 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 :)

Related

Hibernate Domain Object Generation

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

Alternatives to DDLUtils from apache

I would like to know which alternatives exist to replace DDL utils from Apache.
I ask this because ddlutils project seams to be Dead, and also it does not support H2 Databases. I've searched for it, and I found suggestions like liquidbase or flyway.
My problem is: These frameworks run when project starts and change DB structure based on some XML files. They are really designed for Database Migration.
What I want is a framework to CREATE/ALTER Tables in Runtime, in a high abstraction level., i.e. supportting at least Mysql, Sqlserver, oracle, and H2.
For example I could tell to the engine that I want to create a table with a Field AGE with Type Number, and the framework would rephrase to:
create table MY( id bigint(20))
create table MY(id bigint)
create table MY (id, number)
depending on the underlying db engine.
Any suggestions?
I could see there is a patch for ddlutils, for it to support H2. However I wasn't able to patch my svn checkout...
Any help will be appreciated.
thanks in advance
rui
I know this is an old thread, but wanted to give a definitive answer.
Yes, DdlUtils is dead, hasn't seen an update in 2 years now.
However, it looks like the guys might have switched over to https://www.symmetricds.org. Their repo is https://github.com/JumpMind/symmetric-ds.
As soon as you scratch away at the surface, you'll find that the core of DdlUtils is still in there (even has some of the old Apache copyright notices).
Class names have changed, APIs have changed so there is not a 1-to-1 mapping, but it is getting regular updates and includes H2 and other database support. Honestly I'd rather be getting those things instead of keeping the old APIs.
You're not going to find a guide on using Symmetric DS in the same way as the old DdlUtils doco, but there is enough in the code that you should be able to piece it together.
Take a look on jOOQ it is very useful in generating DDL (and DML too)
create.createTable("table")
.column("column1", INTEGER)
.column("column2", VARCHAR(10).nullable(false))
.constraints(
constraint("pk").primaryKey("column1"),
constraint("uk").unique("column2"),
constraint("fk").foreignKey("column2").references("some_other_table"),
constraint("ck").check(field(name("column2")).like("A%"))
)
.execute();
This looks promising: https://bitbucket.org/aragot/play-sql-dialects/src
At least as a start.
Mogwai ERD designer might help though they do not formally support H2 but you could put H2 into compatibility mode with one of the supported DB systems: https://sourceforge.net/p/mogwai

Using App Fuse to generate CRUD operations directly from database schema

I was told that one can generate CRUD operations directly form the database schema using app fuse. But I was unsuccessful in doing that using it and it look very long without any clue.
One possibility is to create the models and annotate it and create CRUDs and create database tables and also populate them with dummy variables. But is it possible to do it other way round.
I have been following this tutorial provided from App Fuse. Am I doing it wrong or is it possible.
Thanks
I would try using appfuse:gen-model:
http://static.appfuse.org/plugins/appfuse-maven-plugin/gen-model-mojo.html
Note, that AppFuse isn't great at creating relationships between classes, so you might have to do some work after it generates the code. You also might try searching the user mailing list archives:
http://appfuse.547863.n4.nabble.com/AppFuse-User-f547864.html

Mysql back up on request using hibernate

Is it possible to take the backup of mysql database using hibernate ? Share your ideas.
I am using hibernate mapping xml for pojo mapping with table.
AFAIK, Hibernate not providing features for backup or restore.
How ever you should consider to do it from command line, which you can execute through java.
Downloading MySQL dump from command line
Hibernate does not provide database backup functionality and here is why,
Hibernate is an OR mapping tool and primarily aimed as objectifying the database so that the code developers don't have to bother about databases interaction intricacies and never have to work with database directly.
Database backups are taken by DB administrators (and not developers) who very closely work with the databases and are familiar with tools and terminologies. Hence tools like hibernate do not have any role here to play.
Here follows some solutions
You can create hibernate with ant for restore/backup database This Link provide more information about that
Here follows another solution for backup/restore tables programatically using hibernate

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

Categories