Monday, September 15, 2008

Hibernate XDoclet Class From Table Generator

Still remember Hibernate XDoclet? In this post, we will create an AppGen in AppFuse. Imagine if we have a hundred of columns in our table in database and we have must to write a model of that class, it's fields, it's getter and setter one by one. So, here we start. In this example, our table (person) just have 3 columns.


Create folder util in src folder. In Eclipse, create source folder that refer to src/util. Create a package in that source folder named com.ndung.gen. For the first step, we must prepare our model template for class.template, field.template, getter.template, and also setter.template. We also have to prepare our jdbc properties to connect to our database.

class.template


field.template


getter.template


setter.template


jdbc.properties

After that, we create two classes named TableInspector.java and ClassGenerator.java. TableInspector is used to inspect data type in each columns in table and map it in to Java class type. In another hand, ClassGenerator is used to write a Java model class file based on our template above.

TableInspector.java


ClassGenerator.java

Now, we create a main Java class named HibernateXDocletClassFromTableGenerator.java.


Run that class above and refresh our project. Look in folder src/dao in package com.ndung.model. There is a model class named Person.java. We must change XDoclet attribute for id from hibernate.property to become hibernate.id.

Person.java

No comments: