How can I generate java class files that are based on mysql database tables?
I have seen some velocity templating examples but nothing that reads a mysql db table and generates a class file from it.
The key is getting the table schema information and looping through the columns etc.
You should use Telosys, a lightweight code generator based on Velocity. It does exactly what you need.
This generator is able to connect to your database to create a "Database model" then you just have to apply templates on this model to generate your code (typically DAO, CRUD Screens, etc).
It can be used to generate a Java persistence layer with any kind technology ( JDBC, JPA, etc )
See http://www.telosys.org/ and its "Database model" http://www.telosys.org/models.html
Some ready to use templates are available on GitHub. See : http://www.telosys.org/templates.html,
https://github.com/telosys-templates-v3
Related
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.
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
Teiid is a data virtualization system that allows applications to use data from multiple, heterogenous data stores.
We know SQL in RDBMS has such a feature: produce a view which including columns from different tables.
Does Teiid has the same feature when integrate data from different data source.
For example, there is a flat file data source with a schema (id, book_name), and a rdbms data source with a schema (id, price). Is there any solution to make a integration schema(id, book_name, price) in Teiid?
Teiid has the ability to create views manually using DDL allowing you to bring together columns from different tables/data sources.
For a small example have a look here: http://teiid.blogspot.com/2012/03/dynamic-vdbs-are-back-with-punch.html
Currently I have been doing all views manually on dynamic VDBs, there is also the Teiid Designer (eclipse plugin) that can make the creation of views much easier.
Thanks
Yes, you can combine the data from any number of sources. Yes, your example of flat file and RDBMS is one of the first quick start examples. See https://docs.jboss.org/author/display/teiidexamples/Data+Federation+Example
I have 2 data models with different amount of tables in MySQL, but both designed for the same purpose.
I need to have mechanism which will migrate data from model #1 to model #2. It can be stored procedure, a set of SQL-scripts, or Java-code. It would be best to create mappings visually (e.g. drag from Table1M1.field1 to Table1M2.field5). Is there any tool for this exists?
MySQL Workbench has a Database Migration module. Check it out.
I'm doing a Java software-project at my university that mainly is about storing data-sets (management of software tests).
The first thing I thought of was a simple SQL DB, however the necessary DB scheme is not available for now (let's say the project is stupid but there's no choice).
Is a persistency framework like Hibernate able to store data internally (for example in XML) and to convert this XML into decent SQL later?
My intention is to use the additional abstraction layer of a framework like Hibernate to save work, because it might have conversion functions. I know that Hibernate can generate class files from SQL, but I'm not too sure whether it needs a DB at every point during development. Using a XML Scheme for now and converting it into SQL later maybe an idea :)
You can persist XML with hibernate into a relational DB, but you cannot use XML directly as a storage engine. Why not simply store you're data into a relational db from the start - you'll create some schema yourself and you'll adapt it to the actual one when you receive it.
I would recommand using a lightweight DB such as HSQLDB instead.