Java, Code Generation, and Persistence Frameworks - java

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.

Related

Is it possible to add new JPA/Spring entity in runtime to the running Java web application

I am trying to create web application that allow its users to create new forms and tables (many ERP applications have such feature). It is clear about generating and saving HTML forms and it is clear about generating new tables in database as well. But what about entities, e.g. Spring #Entity and #Repository classes.
One can try to compile then on the fly and save into the web application deployment directory, that should be possible. But is there need to update some kind of internal Spring registry of existing beans, repositories and controllers. Does Java JPA have such registry as well?
Is it possible to do such kind of thing? It is clear that it is possible to do this in php, e.g. to dynamically update yii framework application, because there is no compiled code (except, maybe, cached code) and with each new request the available paths are scanned anew. But how this happens in Enterprise Java and Spring applications?
Maybe I should look for Groovy Grails or Scala Play - they may be more dynamic languages.
More "object java storages" compatible with JPA (ObjectDB and Co) promise smoother, more natural migration with extra fields and classes. I haven't personal opinion.
EDIT: good perspective has, I think, philosophy ActiveRecord and similar. Few solutions in Java are ready. I have tried such solutions with partial success, but my project was not too dynamic (classes were stable) and we switched to clear JPA.
some ERP applications (in different languages f.e.C,Java.C#) have an idea "kernel class and additional fields", ie. Customer with all typical fields plus "Preffered color" (usually implemented in extra hidden tables). This is OK if they don't need new relations
Eclipselink has similar concept 'extra fields' in JPA area (not strict standard JPA but extension) https://wiki.eclipse.org/EclipseLink/Examples/JPA/Dynamic
At project level, seems to be OK enter non-critical data
3 Ist hard to imagine create high count of dynamic important/central classes (tables) without radically redesigned application (proverbially version 2.0 ;) )
Sometimes I try stop and restart EMFactory with different Persistence Units (in Tomcat environment) with Hibernate and Eslipselink, but not use in production. Reset & start seems be ok, this is like rupture and new life of JPA engine. Its more like application 2.0 than small patch.
Creating multiple gemmini table sets (for different companies in the same database) is good in Hibertate and Eslipselink (prefix before table name), few dedicated lines by start, normal clean JPA use. Tested, all OK.
Once again, adding single/few tables is poorly reworked in the community.
It is an interesting discussion to "glue" together common JPA modules (code+JPA), this is done often by OSGI programmers.
Theoretically so it has to module give your tables, strongly connected in the same PU.
At the ERP application level it can be anything from "add CRM module" to "kernel + sales".
I'm an interested spectator, but i have not seen any success.
If you look in the google integrate persistence unit from parts, composite persistence unit, many programers try to reach such target.
It is like a fishing rod rather than fish (as the old adage goes).
Persistence Unit conception isn't friendly for such ideas (and is blocked in standard JPA).

Any fast way to take mySQL table and create Hibernate beans

The firm I just started at has to take about 200 MySQL tables and make Java, Hibernate Beans from them. We are working on Spring MVC Web projects. Does anyone know of any tool or product that will credit the Java code for us from mySQL tables>
Eclipse (and Hibernate Tools) has a lot of functionality for auto-generating code. I assume the other IDE's do as well. Walk-through: http://www.wikihow.com/Generate-Hibernate-Pojo-Classes-from-DB-Tables

How to generate model from database

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.

Hibernate Tools - DAO generation

I am using the eclipse hibernate tools plug-in to reverse engineer my database.
I spent my whole day looking for how to force the DAO generator to use HQL/Criteria with the session factory, transaction, etc.
Right now, if I have the table TABLE in my database, I obtain the DAO class TableHome. This class uses the persistence EntityManager.
I found tutorials using an older version of hibernate tools generating TableDAO instead, and this is what I exactly need.
Thank you very much.
Whether you're using Eclipse or not is unclear but if you are, maybe check if the Console Configuration used by the Code Generation module is not configured for JPA:
alt text http://docs.jboss.org/tools/2.1.0.Beta1/hibernatetools/html/images/plugins/plugins_2.png
See also
Section 3.4. Creating a Hibernate Console Configuration
Section 3.5. Reverse Engineering and Code Generation
Use Salto-DB Eclipse plugin. It comes with all the stuff.

Databases and Java

I am starting out writing java code and interacting with databases for my "nextbigthing" project. Can someone direct me towards the best way to deal with adding/updating tables/records to databases? Here is my problem. There is too much repitition when it comes to DB code in java. I have to create the tables first (I use mysql). I then create classes in Java for each table. Then I create a AddRow, DeleteRow, UpdateRow and Search* depending on my need. For every table, every need creating this huge ass sql statement and the classes all seems like a huge waste of my time. There has to be a better, easier, more efficient way of doing things. Is there something out there that I do not know that will let me just tell Java what the table is and it automatically generate the queries and execute them for me? Its simple SQL that can be auto generated if it knows the column names and DB table inter dependencies. Seems like a very reasonable thing to have.
Check out Hibernate - a standard Java ORM solution.
User hibernate for mapping your classes to Database.
Set its hbm2ddl.auto to update to avoid writing DDL yourself. But note that this is not the most optimal way to take it to production.
Consider using Hibernate:
https://www.hibernate.org/
It can create java classes with regular CRUD methods from existing database schema.
Of course there is a much better way !
You really want to learn some bits of Java EE, and in particular JPA for database access.
For a complete crash course on Java EE, check out the Sun the Java EE 5 tutorial.
http://java.sun.com/javaee/5/docs/tutorial/doc/
Part 4 - Enterprise Beans
Part 5 - Persistence (JPA)
Then you want to try Hibernate (for instance) which has an implementation of JPA.
This is for Java 5 or later.
If you are still in Java 2, you might want to try Hibernate or iBatis.
You can also try iBatis, if you want control over SQL. Else JPA is good.
You can also try using Seam Framework. It has good reverse-engineering tools.
There is also torque (http://db.apache.org/torque/) which I personally prefer because it's simpler, and does exactly what I need.
With torque I can define a database with mysql(Well I use Postgresql, but Mysql is supported too) and Torque can then query the database and then generate java classes for each table in the database. With Torque you can then query the database and get back Java objects of the correct type.
It supports where clauses (Either with a Criteria object or you can write the sql yourself) and joins.
It also support foreign keys, so if you got a User table and a House table, where a user can own 0 or more houses, there will be a getHouses() method on the user object which will give you the list of House objects the user own.
To get a first look at the kind of code you can write, take a look at
http://db.apache.org/torque/releases/torque-3.3/tutorial/step5.html which contains examples which show how to load/save/query data with torque. (All the classes used in this example are auto-generated based on the database definition).
Or, if Hibernate is too much, try Spring JDBC. It eliminates a lot of boilerplate code for you.
iBatis is another good choice, intermediate between Spring JDBC and Hibernate.
It's just a matter of using the right tools. Use an IDE with tools to autogenerate the one and other.
If you're using Eclipse for Java EE and decide to head to JPA, then I can recommend to take benefit of the builtin Dali plugin. There's a nice PDF tutorial out at Eclipse.org.
If you're using Eclipse for Java EE and decide to head to "good ol" Hibernate, then I can recommend to take benefit of the Hibernatetools plugin. There's good reference guide out at Hibernate.org.
Both tools are capable of reverse-engineering from a SQL table to fullworthy Javabeans/entities and/or mapping files. It really takes most of boilerplate pains away. The DAO pattern is slightly superflous when grabbing JPA. In case of Hibernate you can consider to use a Generic DAO.

Categories

Resources