Thursday, April 17, 2014

JOOQ Reverse Engineering Using Maven Generator

Introduction : 

JOOQ is used to write type safe SQL queries using JAVA. Some time it is difficult to write SQL queries for Java Developer, because Java Developer is familiar with Object Oriented World and with its own language syntax. SQL is a different language and developers need to learn new language for deal with data base using queries. For the sake of learning new language syntax and new structure, The JOOQ provide a way to use queries using Java Programming Syntax. There are lots of rich ORM tools which also provide the way to implement queries using Java Programming Syntax, these queries called CRITERIA queries. But these ORM tools have some Limitations , so if the programmer only need to apply types safety queries, the JOOQ is the best way to do this. There are lots of things that JOOQ provide. For JOOQ detail go to JOOQ Home Page. JOOQ also provide some classes or function that are familiar for SQL developer like for select query JOOQ provide select method in class. 

JOOQ also provide the reverse engineering for generate Java POJO's or Entities from existing table in database. They provide several approaches to use generator to generate Entities. In this POST we discuss about Maven Generator for generate Entities.  

Step 1:

Add the Generator plugin in Maven pom.xml. 

 3.3.1



 
  org.jooq
  jooq
  ${jooq-version}
 

 
  org.jooq
  jooq-meta
  ${jooq-version}
 

 
  org.jooq
  jooq-codegen
  ${jooq-version}
 




 
 org.jooq
 jooq-codegen-maven
 ${jooq-version}

 
 
   
   
   generate
  
  
 

 
  
   mysql
   mysql-connector-java
   5.1.30
  
 

 
 
  
  
   com.mysql.jdbc.Driver
   jdbc:mysql://localhost/jooq_test
   root
   root
  

  

  
   org.jooq.util.DefaultGenerator
   
    org.jooq.util.mysql.MySQLDatabase
    .*
    
    jooq_test
   
   
    com.the13star.entities
    src/main/java
   
  


For JOOQ we need some dependencies to add in the pom.xml file. But the generator plugin also need some dependencies for plugin use only. That dependencies are declare in the generator plugin. If we already declare dependencies in dependency block and not declare in plugin the generator will not run. We again need to declare dependencies in the generator plugin, because plugin use its individual dependencies for run like MySQL dependency in the above code. If maven provide the way to use dependencies of dependency block in plugin please discuss.
In this we use DefaultGenerator for generate Entities, you can also use your custom generator.

Step 2

For run the generator use Maven install command from eclipse or command line, after generator run, there are three packages structures are automatically create in your project. 
The three pakcages are 
1. com.the13star.dbmetadata: According to its name it is used to represent meta information of database table. Like Kyes.java contain information regarding primary key's etc. 
2.  com.the13star.dbmetadata.tables: This package contain the information of db tables like. Field in table, types of column etc. One java file represent one table. according to above example there is only one table in DB. 
3.  com.the13star.dbmetadata.tables.records: This package represent the record of table or represent the one row in table. We will also said that, this is our Java POJO or Entity file. 

No comments:

Post a Comment