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.
Related
In Grails I am creating "domains", i.e. classes with some fields, which are automatically mapped onto database.
Is it possible to achieve the same (or similar) functionality in separate Java or Groovy project?
I wish it to be compatible with separate Grails-controlled database, i.e. for example, to write desktop application to populate database.
Grails mappings are based on Hibernate ORM. hibernate is independent ORM rom map objects to DB. I think it will help you. See Hibernate docs
I want to use Hibernate 4 to work with the database. I use this configuration to map tables in database with Java class:
<mapping class="test.table1"></mapping>
In my Java class I define compatible fields with the table. But now I want to generate the compatible fields automatically. Is there any tool to do it?
I'm using Java 1.7 and MySQL.
If you just want to generate some Java classes based on the schema defined in your database why don’t you use a code generator designed for this kind of job?
For example you could use Telosys, it’s a very handy tool that can create a “database model” in a few minutes by connecting to an relational DB and then use it to generate any kind of code and especially Java classes.
The advantage is that you can adapt the templates if necessary (and if you want you can generate more than the domain classes).
For more information see the web site http://www.telosys.org/ and read this article https://modeling-languages.com/telosys-tools-the-concept-of-lightweight-model-for-code-generation/
You can use Hibernate Tools Reverse Engineer, which inspects the database and generates the domain model classes from the database table definition.
I need to add a new datasource to my grails project wich uses the musicbrainz postgresql database. http://musicbrainz.org/doc/MusicBrainz_Database
I found a project on github where the data bindings are ready to use for a spring project:
https://github.com/lastfm/musicbrainz-data
Am i able to use these data bindings in a grails 2.2.3 project? If yes, how can i do this? (because there is no hibernate xml as needed by grails (regarding to the grails documentation: Hibernate Mapped Domain Classes))
I don't think it will be. Just setup the additional datasource and model out the tables or objects you need.
How do you access two databases in Grails
Once you do that, you can use all of the GORM methods and dynamic finders to get your data. Plus validation criteria, transactions, etc. Unless there is some very specialized criteria that make it necessary to bypass GORM, I would suggest leveraging it.
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
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.