How to generate model from database - java

I have an existing database. I need to generate the model classes in Java from it. Are there any tool/library that will allow me to do this. It will be of great help if it can emulate the entity relationships in the database into the model classes as well.
It is acceptable if the tool/library works with only one database vendor. I will create a database there and then generate the model.
Thanks in advance.
EDIT : I will probably use Hibernate as the ORM framework if I manage to generate the model.

The Hibernate Tools project (available as an Eclipse plug-in, as well as an Ant task) allows for "reverse-engineering" of database schemas into appropriate entity classes.
This project is also available in the JBoss Tools project.
The facility allows for reverse-engineering of the database metadata into a Hibernate configuration file. All artifacts (including the .java files) are generated from this config file.
You can control the nature of the reverse engineering process to suit your database structure. In other words, you can specify the schemas that you wish to have the tool reverse-engineer. You could also override the JDBC type mapping, apart from limiting the reverse-engineering process to a selected set of tables.
Obligatory link:
Screencast on Reverse engineering and code generation

Telosys does exactly this job
Let's have a look : http://www.telosys.org/

Minuteproject 4 JPA2 (http://minuteproject.wikispaces.com/JPA2) track does this task.
Minuteproject can be run from console or command line.
You can have a quick result by generating from the console where generate a maven project containing the JPA2 mapping classes in java or groovy.
If you use the command line then you need to fill an xml file that can contain additional customisation of your generated code such as packaging, enum, aliasing etc...
You can also try other track built on top JPA2 such as DAO with spring or EJB; REST; front end with Primefaces or Openxava; etc...

Hibernate has an Eclipse plugin Hibernate Tools http://www.hibernate.org/subprojects/tools.html that has reverse engineering capabilities.
See: http://docs.jboss.org/tools/3.2.0.GA/en/hibernatetools/html/plugins.html#refeng_codegen for more details on how to run and customize the reverse engineering process.

Related

Need a way to generate Java classes for Spring Boot

I’m trying to build an administration portal accessible via web starting from the informations inside a database using Spring Boot with MyBatis for accessing the database. I wanted to find a way to reuse the code that I’m building for the portal with any other type of database, so I was wondering if there was a way to automatically generate classes for my project starting from the informations in the database, for example table names and fields...
Thanks in advance!
Telosys code generator (http://www.telosys.org) does this kind of job.
It uses the database schema to create a lightweight model that is used to generate the code (Java or any other language).
For more information see : https://modeling-languages.com/telosys-tools-the-concept-of-lightweight-model-for-code-generation/
Everything is Open Source (tool and templates)
An option could be Jassd, which generates POJOs, Dao interfaces, and Dao implementation classes from a database. Use what you want from the output and discard the rest, the aim of this project was to not have to manually write getters and setters and have something to start from.
See the documentation and sample output at https://github.com/aforslund/jassd
(Note: I am the author, implemented this library to be able to get plain old Java objects generated and bootstrap som Dao/DaoImplementation classes using plain SQL and JDBCTemplate with Spring Boot).
If it is to switch db and reuse the code, simply changing these values in application.properties should help. If you have MySQL in development environment and postgres in production, that can also be handled. Check the following link for more details
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html
You are lucky! I just release cgV19 at https://github.com/carstenSpraener/cgV19
With it you could implement a model loader which connects to your database and reads the table information. It than provides this information to cgV19 as a meta model.
the next step is to implement a "cartridge" for cgV19 where you can generate java classes from that meta model.
if you try it out please give me feedback on you progress.
If you are going to use mybatis then MyBatis Generator is your best bet:
MyBatis Generator (MBG) is a code generator for MyBatis MyBatis and iBATIS. It will generate code for all versions of MyBatis, and versions of iBATIS after version 2.2.0. It will introspect a database table (or many tables) and will generate artifacts that can be used to access the table(s).
MyBatis Generator will generate:
Java POJOs that match the table structure.
MyBatis/iBATIS Compatible SQL Map XML Files. MBG generates SQL for simple CRUD functions on each table in a configuration.
Java client classes that make appropriate use of the above objects.

Using Hibernate reverse engineering creates mappingResources style but I need annotatedClasses

Using Hibernate reverse engineering creates mappingResources style but I need annotatedClasses.
Anyway to make "Hibernate reverse engineering" to make the code annotatedClasses style and not mappingResources style?
I am trying to take a large mySQL database and make java classes with annotated in them.
Please let me know if anyone has done this
You can achieve that with Hibernate Tools, an Eclipse plugin.
Take a look at this tutorial. Every steps are explains. When you are generating the .java files, you can choose between xml file and JPA annotations.
To generate annotations, check Generate EJB3 annotations and Use Java 5 syntax
First you need to add the annotations support to the dependencies of your project. It depends on the version of hibernate do you use. As for the hibernate 4 annotations already included in the core jar, however make sure hibernate-commons-annotations included also. Then the reverse engineering wizard will let you choose how do you want to map the generated classes.

How to create entity classes using database for all the existing tables?

I am having tables created by a DBA and I intend to have
entity classes for each table for further querying. Can any one suggest how to automatically create the entity classes using Eclipse?
If you use JPA for persistence, you can use "JPA tools" provided by Eclipse to generate java entity class from tables.
Here is the [tutorial][1]
[1]: http://shengwangi.blogspot.com/2014/12/how-to-create-java-classes-from-tables.html on my personal website.
You can generate classes using Hibernate Tools. There are two ways to generate these classes which is called as Reverse Engineering and Code Generation[dead link] -
You can use Hibernate Tools Eclipse Plugin.
You can write Ant Script using Hibernate tools jar.
You can try the Telosys code generator. It can help for this kind of job.
It allows to generate the Java (POJO) classes from any database
with the basic templates and you can also create your own
template (a Velocity file) to generate your DAO classes
For more information see https://www.telosys.org

How to generate JPA mapping file from the JPA annotated entity classes?

I am using openjpa runtime of JPA specification.
At development time I am using annotations to configure jpa entities.
At Integration, Pre-Production and and Production environment, I am using orm mapping files to configure entities. Please suggest a tool which can generate mapping files from jpa annotations, so that these mapping files can be manually edited for different environment.
If there is already a opensource maven-plugin; will be great.
I don't really know OpenJPA so there is maybe a better way to do this but one option would be to first generate the XML schema file from annotated entities using the Schema Tool and then the orm.xml file from the schema.xml using the Reverse Mapping Tool. Actually, this process is discussed in this thread.
I've checked the OpenJPA Maven Plugin but it doesn't seem to support the Reverse Mapping part (it only has a openjpa:schema goal that allows to Create a file which contains the schema mapping XML, the first required operation, but nothing for the second part). Extending the plugin to add the missing openjpa:reverse-mapping goal would thus require some development but it shouldn't be an hard task.
There is another option though. OpenJPA provides the following Ant tasks for both operations:
org.apache.openjpa.jdbc.ant.ReverseMappingToolTask
org.apache.openjpa.jdbc.ant.SchemaToolTask
So it should be possible to call them from Maven using the Maven AntRun Plugin. Check the documentation for more details on how to use them.

Java, Code Generation, and Persistence Frameworks

Are there any Java code generation persistence frameworks?
Something that is "database first" rather than object model first?
I know this is probably heresy in the Java world, I'd just like to know if any frameworks like this exist.
I'm more of a .NET guy these days. So on the .NET-side tools like .NET Tiers or CSLA come to mind.
sure, hibernate and netbeans for example can reverse engineering a database. You may be want to look at:
Hibernate Tools; site in maintenance
netbeans, with recent version of netbeans you can create JPA entities from a JDBC connection
My 2 cents.
The Apache Cayenne ORM framework is able to reverse engineer a database. See here.
I developed jOOQ to exactly suit your needs - so don't worry about the heresy :-)
Check out the examples page or an article on dzone illustrating my motivations of creating such a framework. The main advantages are:
It generates source code
It stays close to SQL by providing a SQL-like DSL in Java
It supports easy access vendor-specific features, such as UDT's, stored procedures
It supports all advanced SQL features, such as UNIONs, nested SELECTs, aliasing, etc
Note that jOOQ is NOT really an OR-mapper. It focuses on the relational datamodel from your RDBMS and doesn't give you the possibility to create your custom object-oriented domain model like Hibernate or JPA do. This is precisely for the reason you mentioned yourself: Something that is "database first" rather than object model first
Checkout EclipseLink project ( plugins for eclipse) , you can generate JPA models from database tables
DB Importer is an Eclipse plug-in that generates JPA classes from a database.
Disclaimer: I am the creator of DB Importer.
Telosys Tools is a free "database first" code generator
See : https://sites.google.com/site/telosystools/
It's an Eclipse plugin, the templates are customizable if necessary
It can generate the persistance layer (JPA) but also the CRUD Screen
( tutorials are here : https://sites.google.com/site/telosystutorial/ )
Many of the Java-based persistence tools include tools to generate code based upon an existing schema. Netbeans will generate JPA (including Session bean facades, if desired). Similarly, JBoss Tools for Eclipse will generate Hibernate JavaBeans for you based upon an existing schema.
I would recommend HiberObjects. It's an Eclipse plugin that can be used to model persistent objects or reverse engineer an existing database. The way it lets you set up unit tests is truly awesome. You can also modify how for instance DAO classes are generated: just modify a Groovy script and all DAO-classes are re-generated. I have encountered a few bugs, but the guy behind it has been very responsive and released fixes in short time. A description and user ratings/comments are found at Eclipse Plugin Central.

Categories

Resources