Generate JPA compatible POJO's from DB2 schema (similar to middlegen+hbm2java)? - java

I want to automatically generate POJO's corresponding to a set of DB/2 tables to test if JPA will work well for a new application.
In ancient history I used middlegen to create Hibernate mappings which then hbm2java could create POJOs from, and searching mostly revealed outdated information. I was wondering if middlegen is still the tool to use (and if so, where is its current homepage) even for JPA, or if a new toolset has arisen for JPA.
So, how do I generate POJO's from my DB/2 schema?

I used Dali to automatically generate pojo entities from the database schema. Not sure how, or even if, it works programmatically though ...

Related

Is it possible to automatically generate Hibernate mappings for Java classes?

I'd like to automatically generate Hibernate mappings for some Java classes I have. Basically, I'd like all objects in the class to have the equivalent of the #OneToOne annotation, and all collections to have the equivalent of the #OneToMany annotation by default. I'd like to be able to fine-tune this later, but 99% of my data works this way, and it would take a very long time to go through all the classes and add the annotations manually.
Is this possible?
edit: Sorry, I think omitted something very important: I'd actually like to generate these default annotations from classes, not from a database. I would still have to design about 40 tables if I were to start with the database, but I already have a package containing all those classes. Is it possible to generate the proper mappings based on existing Java classes?
Yes, in eclipse there is a plugin called hibernate tools from jboss, which can generate mapping from a existing database.
You can generate all hibernate files both xml and annotations variant.
Just Google Hibernate Tools to know how to use it.
Thanks !!
If you are creating new classes then you can create the database structure first and then create classes using database. In eclipse, hibernate provides Hibernate code generation feature. You can use that.

Designing Hibernate classes with tables

I'm new to hibernate and trying to understand what is the correct way to implement and use it.
Creates tables and then use a tool to generate Hibernate classes
or
Build the classes and create the tables
Our's is a legacy application, so the tables structure is already there..We are just creating the Hibernate classes and using them...
Can someone let me know which is correct and effecient way?
You have automated tools for both sides.
create the classes from a legacy database:
you should use this if you have a runnig database already.netbeans have one of these.
create the tables and relations from entity and embedded clasess:
this way you have to use anotations or a xml file to describe the relations betwen clases and other relational stuff and set table generation strategy to create in the persistence unit the tables will be created automatically usigng the names of the classes for the table and the names of the fields for the columns.
If you already have the database use the tool to generate entity classes
If you use Eclipse then here is a tool to generate POJOs from existing tables:
step by step auto code generation using eclipse plugin
If you are using Intellij check out Intellij Java Persistence API & Hibernate

Which is the best tool Generate Hibernate map from MySQL DB

I am using hibernate in my servlet to access the database.DB contains lot of foreign keys, composite keys and backward references..
Can you suggest me some tools to do reverse engineer the MySQL DB to Object oriented file in java so that I can use it with hibernate.
I agree with Cris,
The version 0.5.5 of Minuteproject 4 JPA2 generates also for composite key a class annotated with #Embeddable. The entity class has the #EmbeddedId as entity with the reference to the Embeddable class. For the foreign keys part of the composite key, it generates the associated #MapsId.
Meanwhile the JPA2 templates do not cover yet the composite foreign key pattern.
Hope it helped.
IDEs usually provide such generating of entities.
For example, if you use Netbeans, just go File/New/Persistence/Entity Classes from Database within a Java SE/Java EE-Project to generate JPA entities.
By the way, it's a good idea to use standard JPA(2) mechanisms where possible, in order to not being dependant on a specific O/R-Mapper (such as Hibernate) or database.
Have a look at JBoss Hibernate Tools which allows you to reverse engineer existing databases.
The best one is MinuteProject in my opinion !
Check it on :
http://javacodesamples.wordpress.com/2011/02/04/jpa2-reverse-engineering-tool/
http://www.dzone.com/links/jpa2_reverseengineering_with_minuteproject_pragma.html
http://javacodesamples.wordpress.com/2010/09/04/minute-project-episode-1-the-productivity-provider/
It has also a Spring + Hibernate track beside the JPA so you can used that for you hibernate needs.
We use Hibernate Tools to generate *.hbm.xml files with the help of a hibernate.reveng.xml file. And then, we apply Hibernate Synchronizer plugin of Eclipse to generate Entities and DAOs using *.hbm.xml files.
hibernate.reveng.xml fill the parts where auto generation cannot guess what you have in your mind.
Hibernate Synchronizer has the main advantage of creating base classes for your generated entities/daos. You add your custom properties/methods to subclasses (again automatically generated) of the base classes. The advantage here, is if you make changes to your database and regenerate, the changes you make (to subclasses) rest untouched.
Whatever tool you choose pay attention to build a system where code generation is easy and painless, not only for the first time but also during advanced phases of the project. If not you'll lose all the advantages.

JPA/Hibernate support for migration?

I'm currently working on a desktop application using JPA/Hibernate to persist data in a H2 database. I'm curious what my options are if I need to make changes to the database schema in the future for some reason. Maybe I'll have to introduce new entities, remove them or just change the types of properties in an entity.
Is there support in JPA/Hibernate to do this?
Would I have to manually script a solution?
I usually let Hibernate generate the DDL during development and then create a manual SQL migration script when deploying to the test server (which I later use for UAT and live servers as well).
The DDL generation in Hibernate does not offer support for data migration at all, if you only do as much as adding a non-null field, DDL generation cannot help you.
I have yet to find any truely useful migration abstraction to help with this.
There are a number of libraries (have a look at this SO question for examples), but when you're doing something like splitting an existing entity into a hierarchy using joined inheritance, you're always back to plain SQL.
Maybe I'll have to introduce new entities, remove them or just change the types of properties in an entity.
I don't have any experience with it but Liquibase provides some Hibernate Integration and can compare your mappings against a database and generate the appropriate change log:
The LiquiBase-Hibernate integration records the database changes required by your current Hibernate mapping to a change log file which you can then inspect and modify as needed before executing.
Still looking for an opportunity to play with it and find some answers to my pending questions:
does it work when using annotations?
does it require an hibernate.cfg.xml file (although this wouldn't be a big impediment)?
Update: Ok, both questions are covered by Nathan Voxland in this response and the answers are:
yes it works when using annotations
yes it requires an hibernate.cfg.xml (for now)
There are two options:
db-to-hibernate - mirror DB changes to your entities manually. This means your DB is "leading"
hibernate-to-db - either use hibernate.hbm2ddl.auto=update, or manually change the DB after changing your entity - here your object model is "leading"

generate java domain objects from database table

may i know in eclipse, is there any feature that will auto generate domain objects with all table relationship properly mapped in class?
can provide me with some reference articles on this?
You can use something like Hibernate to accomplish this
This plugin set for Eclipse called Hibernate Tools for Eclipse and ANT will do most of the work for you.
In particular it will do Reverse Engineering: The most powerful feature of Hibernate Tools is a database reverse engineering tool that can generate domain model classes and Hibernate mapping files, annotated EJB3 entity beans, HTML documentation or even an entire JBoss Seam application in seconds!
Telosys code generator does this kind of job.
It's an Eclipse plugin, it uses the database schema to create a light model
that is used to generate the Java code.
There are some predefined templates available on GitHub (for JPA, POJO, Documentation, Spring MVC, etc )
See http://www.telosys.org
and http://marketplace.eclipse.org/content/telosys-tools
Templates : https://github.com/telosys-templates-v3
Articles about code generation with Telosys :
https://modeling-languages.com/telosys-tools-the-concept-of-lightweight-model-for-code-generation/
https://dzone.com/articles/telosys-a-code-generation-tool-by-laurent-guerin
You can use Hibernate Tools 3.0.0.GA either via Eclipse or ANT to auto-generate your hibernate domain entities directly from your database tables.
See tutorial here :
http://docs.jboss.org/tools/3.0.0.GA/en/hibernatetools/html_single/index.html
I have a solution for you i.e to create auto generate domain objects with all table relationship properly mapped in class ...Try Dal4j yes you can find it in sourceforge.net/p/dal4j/wiki/ DAL4j is a Command Line and Framework tool that can be used to reverse engineer a MySQL or SQLServer database schema into a set of JPA Entity Beans.
DAL4j can be useful for scenarios where there is an existing database schema but a technology other that JPA is used by applications to interact with the database. DAL4j can provide an easy way to migrate your code base from other technologies such as JDBC or Hibernate to JPA.
The beans generated can be 1 or two types: Simple or Framework. Simple beans are standard pojo classes managed by your application using JPA semantics. Framework generated pojos use the DAL4j framework DAO generic to simplify CRUD operations.
DAL4j provides optional hooks to allow you integrate encryption/decryption of data fields that must be encrypted in the database.
Last, DAL4j provides a set of Generic classes that can be used to simplify creation of Session Beans which perform CRUD operations using generated Entities.
I think you will find this article feasible....
You want an object relational mapping of which Hibernate is the most popular for Java. The hibernate tools are typically better for taking annotated classes and using them to generate a schema, as opposed to vice versa, which is what you sound like you're doing. I suspect you'll be doing a lot of hand-annotating if you're working with a legacy DB schema.
if you use grails, you can generate domain objects with GRAG http://sourceforge.net/projects/grag
I use eclipse for java development, but when it comes to generating domain entities I use Net beans.
Create an EJB module, and then right click and generate entities.
You need to set up the database also you can select the tables you want visually.
Regards
Lyju
It feels like another rather common question that people always run into.
The link below links to a blog detailed enough for me to learn how to generate entities from database schema the first time.
http://shengwangi.blogspot.com/2014/12/how-to-create-java-classes-from-tables.html
Just in case, the following link refers to eclipse help page. This link should never expire:
http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.jpt.doc.user%2Ftasks021.htm
I downloaded JBoss and failed to understand how it works. I think the plugin that I used is Hibernate Tools but I am not sure as I did not install any new plugin for this purpose. I am using Eclipse Luna for EE.
Hope this helps.
I got so tired of manually coding this kind of stuff so I made a tool to generate models, dao, and dao implementation from a schema. It's oriented towards spring boot and only tested on MySQL, but for those that don't want to use Hibernate and just want to work with jdbc/sql and JdbcTemplate, or just want POJOs with getter/setters generated for tables, then this could perhaps be something to kick off the coding.
Called Jassd (Java Automated Spring Source-code for Databases generator), I'm "jazzed" to introduce this tool: https://github.com/aforslund/jassd

Categories