Click here to Skip to main content
15,879,490 members
Articles / Programming Languages / Java

Auto Generation Of POJO Classes

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
27 Apr 2019CPOL3 min read 9.3K   87   1  
Generation of POJO Classes is explained in this article

Introduction

POJO is a very basic and common concept among Java developers. It stands for Plain Old Java Object and contains getter and setter methods. Although these classes are very common, writing them on a code editor or IDE takes a long time. In this article, I am going to show how to shorten this process using a source code generator.

Background

Source code generation is not a new concept; however, it is not widely known among developers yet. The idea is to automatically generate similar and repetitive parts of source code instead of writing them manually in order to increase efficiency. There are source code generation tools you can find on the internet currently and some of them are really effective. The working principles; however, can differ. For this project, I used Zontroy as code generator which is a free tool. In order to clearly explain how to generate my classes automatically, I also need to explain the working principles of Zontroy briefly. But first things first. In order to use the generator, you should make your database ready beforehand. Because the tool needs to use entity information from your database. Anyway, after you download the tool, you should create a project. Then, you need to choose how you want to generate your code. There are different options here and the tool provides three unique file types for this purpose. You choose one and add it to your project, and you are ready to go. You should write your code on the code editor embedded in the tool. You can use any programming language you want, but you also need to use zontroy language to modify your code. It is a very simple language and its main purpose is to define which parts of your code will change according to different entities of the database. When you are ready, the program generates the same code as modified for each entity you chose. Then, you can add the generated code to your project.

Database

I put the sample database I used for this project below:

Image 1

(The database is provided from http://www.dofactory.com/sql/sample-database. MSSQL and Oracle scripts provided in the attachment.)

Sample POJO Class

As my database is ready, I can write my POJO Class in a normal manner now. I need to have this piece of code for all of my database entities. The code is shown below:

(Before coding, I chose zontroy repeating file type because I want to have separate code files for all my entities. I won’t go in detail about Zontroy file types as it is not our subject now.)

Java
// 
// Sample.java
//

import java.util.*;
import java.text.*;

public class Sample {
  
    private int id;
    
    public int getId() {
        return id;   
    } 

    public void setId(int id) {
        this.id = id;   
    }   
}

Code Modification

This is the part I use Zontroy language in order to specify which parts of my code will change according to my database entities. The modified code looks like this:

Java
import java.util.*;
import java.text.*;

public class [[[table.name]]] {
   
    for(((item:table.columns))){{{       
    private [[[item.targetType]]] [[[item.targetName]]];
    }}}
  
    for(((item:table.columns))){{{       
    public [[[item.targetType]]] get[[[item.targetName]]]() {
        return [[[item.targetName]]];   
    } 

    public void set[[[item.targetName]]]([[[item.targetType]]] [[[item.targetName]]]) {      
        this.[[[item.targetName]]] = [[[item.targetName]]];   
    }   
    
    }}}
}

What I did was defining class name as table name with the following line which is in the code I wrote:

Java
public class [[[table.name]]] {

Similarly, I defined private variable for each column by writing the following part:

Java
for(((item:table.columns))){{{       
    private [[[item.targetType]]] [[[item.targetName]]];
    }}}

The following section is where I wrote getter and setter methods for each column in a for loop.

Return type of the getter method is the targetType of column.

Columns have data type and target type. Data type is the original type in the database such as varchar2, nvarchar, datetime, etc. Target type is the type of column in programming language. Similarly, targetName keeps the user-defined name of column.

Java
for(((item:table.columns))){{{       
    public [[[item.targetType]]] get[[[item.targetName]]]() {
        return [[[item.targetName]]];   
    } 

    public void set[[[item.targetName]]]([[[item.targetType]]] [[[item.targetName]]]) {      
        this.[[[item.targetName]]] = [[[item.targetName]]];   
    }   
    
    }}}

Result

When I finish writing my code, I click generate button. I provide examples of generated code for several classes.

Supplier.java

Java
import java.util.*;
import java.text.*;

public class Supplier {
   
    private int Id;
    private String CompanyName;
    private String ContactName;
    private String ContactTitle;
    private String City;
    private String Country;
    private String Phone;
    private String Fax;
  
    public int getId() {
        return Id;   
    } 

    public void setId(int Id) {      
        this.Id = Id;   
    }   
    
    public String getCompanyName() {
        return CompanyName;   
    } 

    public void setCompanyName(String CompanyName) {      
        this.CompanyName = CompanyName;   
    }   
    
    public String getContactName() {
        return ContactName;   
    } 

    public void setContactName(String ContactName) {      
        this.ContactName = ContactName;   
    }   
    
    public String getContactTitle() {
        return ContactTitle;   
    } 

    public void setContactTitle(String ContactTitle) {      
        this.ContactTitle = ContactTitle;   
    }   
    
    public String getCity() {
        return City;   
    } 

    public void setCity(String City) {      
        this.City = City;   
    }   
    
    public String getCountry() {
        return Country;   
    } 

    public void setCountry(String Country) {      
        this.Country = Country;   
    }   
    
    public String getPhone() {
        return Phone;   
    } 

    public void setPhone(String Phone) {      
        this.Phone = Phone;   
    }   
    
    public String getFax() {
        return Fax;   
    } 

    public void setFax(String Fax) {      
        this.Fax = Fax;   
    }   
    
}

Product.java

Java
import java.util.*;
import java.text.*;

public class Product {
   
    private int Id;
    private String ProductName;
    private int SupplierId;
    private double UnitPrice;
    private String Package;
    private Boolean IsDiscontinued;
  
    public int getId() {
        return Id;   
    } 

    public void setId(int Id) {      
        this.Id = Id;   
    }   
    
    public String getProductName() {
        return ProductName;   
    } 

    public void setProductName(String ProductName) {      
        this.ProductName = ProductName;   
    }   
    
    public int getSupplierId() {
        return SupplierId;   
    } 

    public void setSupplierId(int SupplierId) {      
        this.SupplierId = SupplierId;   
    }   
    
    public double getUnitPrice() {
        return UnitPrice;   
    } 

    public void setUnitPrice(double UnitPrice) {      
        this.UnitPrice = UnitPrice;   
    }   
    
    public String getPackage() {
        return Package;   
    } 

    public void setPackage(String Package) {      
        this.Package = Package;   
    }   
    
    public Boolean getIsDiscontinued() {
        return IsDiscontinued;   
    } 

    public void setIsDiscontinued(Boolean IsDiscontinued) {      
        this.IsDiscontinued = IsDiscontinued;   
    }   
    
}

Customer.java

Java
import java.util.*;
import java.text.*;

public class Customer {
   
    private int Id;
    private String FirstName;
    private String LastName;
    private String City;
    private String Country;
    private String Phone;
  
    public int getId() {
        return Id;   
    } 

    public void setId(int Id) {      
        this.Id = Id;   
    }   
    
    public String getFirstName() {
        return FirstName;   
    } 

    public void setFirstName(String FirstName) {      
        this.FirstName = FirstName;   
    }   
    
    public String getLastName() {
        return LastName;   
    } 

    public void setLastName(String LastName) {      
        this.LastName = LastName;   
    }   
    
    public String getCity() {
        return City;   
    } 

    public void setCity(String City) {      
        this.City = City;   
    }   
    
    public String getCountry() {
        return Country;   
    } 

    public void setCountry(String Country) {      
        this.Country = Country;   
    }   
    
    public String getPhone() {
        return Phone;   
    } 

    public void setPhone(String Phone) {      
        this.Phone = Phone;   
    }   
    
}

Order.java

Java
import java.util.*;
import java.text.*;

public class Order {
   
    private int Id;
    private Date OrderDate;
    private String OrderNumber;
    private int CustomerId;
    private double TotalAmount;
  
    public int getId() {
        return Id;   
    } 

    public void setId(int Id) {      
        this.Id = Id;   
    }   
   
    public Date getOrderDate() {
        return OrderDate;   
    } 

    public void setOrderDate(Date OrderDate) {      
        this.OrderDate = OrderDate;   
    }   
   
    public String getOrderNumber() {
        return OrderNumber;   
    } 

    public void setOrderNumber(String OrderNumber) {      
        this.OrderNumber = OrderNumber;   
    }   
   
    public int getCustomerId() {
        return CustomerId;   
    } 

    public void setCustomerId(int CustomerId) {      
        this.CustomerId = CustomerId;   
    }   
  
    public double getTotalAmount() {
        return TotalAmount;   
    } 

    public void setTotalAmount(double TotalAmount) {      
        this.TotalAmount = TotalAmount;   
    }   
}  

OrderItem.java

Java
import java.util.*;
import java.text.*;

public class OrderItem {
   
    private int Id;
    private int OrderId;
    private int ProductId;
    private double UnitPrice;
    private int Quantity;
  
    public int getId() {
        return Id;   
    } 

    public void setId(int Id) {      
        this.Id = Id;   
    }   
    
    public int getOrderId() {
        return OrderId;   
    } 

    public void setOrderId(int OrderId) {      
        this.OrderId = OrderId;   
    }   
    
    public int getProductId() {
        return ProductId;   
    } 

    public void setProductId(int ProductId) {      
        this.ProductId = ProductId;   
    }   
    
    public double getUnitPrice() {
        return UnitPrice;   
    } 

    public void setUnitPrice(double UnitPrice) {      
        this.UnitPrice = UnitPrice;   
    }   
    
    public int getQuantity() {
        return Quantity;   
    } 

    public void setQuantity(int Quantity) {      
        this.Quantity = Quantity;   
    }   
    
}

Points of Interest

Java Beans can be easily generated like this. I believe code generators will be used widely in software development in about 5 years. I recommend you to use a code generation tool in order to improve your efficiency and save time as a developer.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Turkey Turkey
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --