Java Code Samples

JPA2 Reverse Engineering Tool

Posted in Old stuff by Cristian on February 4, 2011

Not so long time ago I discovered a project and I posted an article related to it  (MinuteProject and it’s wiki page).

I took a look to see how things progressed there and I discovered a very very nice tool that does reverse engineering and creates a fully compatible JPA2 datamodel.

Prequisites

Have Java 6 setup.
Download MinuteProject last version.(this is a little bit big)

Step: Generation

  1. Start the MP console (<MP_HOME>/bin/start-console.(cmd/sh). How to use the console.
  2. Fill the connection information to the datamodel
  3. Fill the model general information (model, package names, version primary key policy, scope).
  4. Choose JPA2 target
  5. Click generate

I had a simple database called sales: customer,product, invoice and invoice_details.

This should look like this:




Generated code looks like this
package org.free.software.javastuff;

import java.sql.*;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import java.util.Set;
import java.util.HashSet;

import javax.persistence.*;

import org.free.software.javastuff.Invoice;

/**
 *
 * <p>Title: Customer</p>
 *
 * <p>Description: Domain Object describing a Customer entity</p>
 *
 */
@Entity (name="Customer")
@Table (name="customer")
public class Customer {

 @Id @Column(name="ID" )
 private Integer id;

 @Column(name="NAME",  length=200,  nullable=true,  unique=false)
 private String name;
 @Column(name="ADDRESS",  length=200,  nullable=true,  unique=false)
 private String address;
 @Column(name="EMAIL",  length=200,  nullable=true,  unique=false)
 private String email; 

 @OneToMany (targetEntity=org.free.software.javastuff.Invoice.class, fetch=FetchType.LAZY, mappedBy="custId", cascade=CascadeType.REMOVE)//, cascade=CascadeType.ALL)
 private Set <Invoice> invoiceCustIds = new HashSet(); 

 /**
 * Default constructor
 */
 public Customer() {
 }

 public Integer getId() {
 return id;
 }

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

 public String getName() {
 return name;
 }

 public void setName (String name) {
 this.name =  name;
 }
 public String getAddress() {
 return address;
 }

 public void setAddress (String address) {
 this.address =  address;
 }
 public String getEmail() {
 return email;
 }

 public void setEmail (String email) {
 this.email =  email;
 }

 public Set<Invoice> getInvoiceCustIds() {
 if (invoiceCustIds == null){
 invoiceCustIds = new HashSet();
 }
 return invoiceCustIds;
 }

 public void setInvoiceCustIds (Set<Invoice> invoiceCustIds) {
 this.invoiceCustIds = invoiceCustIds;
 }    

 public void addInvoiceCustId (Invoice invoice) {
 getInvoiceCustIds().add(invoice);
 }

}
package org.free.software.javastuff;

import java.sql.*;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import java.util.Set;
import java.util.HashSet;

import javax.persistence.*;

import org.free.software.javastuff.InvoiceDetails;

/**
 *
 * <p>Title: Product</p>
 *
 * <p>Description: Domain Object describing a Product entity</p>
 *
 */
@Entity (name="Product")
@Table (name="product")
public class Product {

 @Id @Column(name="ID" )
 private Integer id;

 @Column(name="NAME",  length=200,  nullable=true,  unique=false)
 private String name;
 @Column(name="VAT_PRC",    nullable=true,  unique=false)
 private Long vatPrc;
 @Column(name="PRICE",    nullable=true,  unique=false)
 private Long price; 

 @OneToMany (targetEntity=org.free.software.javastuff.InvoiceDetails.class, fetch=FetchType.LAZY, mappedBy="productId", cascade=CascadeType.REMOVE)//, cascade=CascadeType.ALL)
 private Set <InvoiceDetails> invoiceDetailsProductIds = new HashSet(); 

 /**
 * Default constructor
 */
 public Product() {
 }

 public Integer getId() {
 return id;
 }

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

 public String getName() {
 return name;
 }

 public void setName (String name) {
 this.name =  name;
 }
 public Long getVatPrc() {
 return vatPrc;
 }

 public void setVatPrc (Long vatPrc) {
 this.vatPrc =  vatPrc;
 }
 public Long getPrice() {
 return price;
 }

 public void setPrice (Long price) {
 this.price =  price;
 }

 public Set<InvoiceDetails> getInvoiceDetailsProductIds() {
 if (invoiceDetailsProductIds == null){
 invoiceDetailsProductIds = new HashSet();
 }
 return invoiceDetailsProductIds;
 }

 public void setInvoiceDetailsProductIds (Set<InvoiceDetails> invoiceDetailsProductIds) {
 this.invoiceDetailsProductIds = invoiceDetailsProductIds;
 }    

 public void addInvoiceDetailsProductId (InvoiceDetails invoiceDetails) {
 getInvoiceDetailsProductIds().add(invoiceDetails);
 }

}
package org.free.software.javastuff;

import java.sql.*;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import java.util.Set;
import java.util.HashSet;

import javax.persistence.*;

import org.free.software.javastuff.Invoice;
import org.free.software.javastuff.Product;

/**
 *
 * <p>Title: InvoiceDetails</p>
 *
 * <p>Description: Domain Object describing a InvoiceDetails entity</p>
 *
 */
@Entity (name="InvoiceDetails")
@Table (name="invoice_details")
public class InvoiceDetails {

 @Id @Column(name="ID" )
 private Integer id;

 @Column(name="QUANTITY",    nullable=true,  unique=false)
 private Long quantity;
 @Column(name="PRICE",    nullable=true,  unique=false)
 private Long price;

 @ManyToOne (fetch=FetchType.LAZY )
 @JoinColumn(name="INVOICE_ID",  nullable=true,  unique=false )
 private Invoice invoiceId;

 @ManyToOne (fetch=FetchType.LAZY )
 @JoinColumn(name="PRODUCT_ID",  nullable=true,  unique=false )
 private Product productId;

 /**
 * Default constructor
 */
 public InvoiceDetails() {
 }

 public Integer getId() {
 return id;
 }

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

 public Long getQuantity() {
 return quantity;
 }

 public void setQuantity (Long quantity) {
 this.quantity =  quantity;
 }
 public Long getPrice() {
 return price;
 }

 public void setPrice (Long price) {
 this.price =  price;
 }

 public Invoice getInvoiceId () {
 return invoiceId;
 }

 public void setInvoiceId (Invoice invoiceId) {
 this.invoiceId = invoiceId;//this.invoiceId = invoice;
 }
 public Product getProductId () {
 return productId;
 }

 public void setProductId (Product productId) {
 this.productId = productId;//this.productId = product;
 }

}
package org.free.software.javastuff;

import java.sql.*;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import java.util.Set;
import java.util.HashSet;

import javax.persistence.*;

import org.free.software.javastuff.InvoiceDetails;
import org.free.software.javastuff.Customer;

/**
 *
 * <p>Title: Invoice</p>
 *
 * <p>Description: Domain Object describing a Invoice entity</p>
 *
 */
@Entity (name="Invoice")
@Table (name="invoice")
public class Invoice {

 @Id @Column(name="ID" )
 private Integer id;

 @Column(name="NUMBER",  length=200,  nullable=true,  unique=false)
 private String number;
 @Column(name="DATE",    nullable=true,  unique=false)
 private Timestamp date; 

 @ManyToOne (fetch=FetchType.LAZY )
 @JoinColumn(name="CUST_ID",  nullable=true,  unique=false )
 private Customer custId; 

 @OneToMany (targetEntity=org.free.software.javastuff.InvoiceDetails.class, fetch=FetchType.LAZY, mappedBy="invoiceId", cascade=CascadeType.REMOVE)//, cascade=CascadeType.ALL)
 private Set <InvoiceDetails> invoiceDetailsInvoiceIds = new HashSet(); 

 /**
 * Default constructor
 */
 public Invoice() {
 }

 public Integer getId() {
 return id;
 }

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

 public String getNumber() {
 return number;
 }

 public void setNumber (String number) {
 this.number =  number;
 }
 public Timestamp getDate() {
 return date;
 }

 public void setDate (Timestamp date) {
 this.date =  date;
 }

 public Customer getCustId () {
 return custId;
 }

 public void setCustId (Customer custId) {
 this.custId = custId;//this.custId = customer;
 }

 public Set<InvoiceDetails> getInvoiceDetailsInvoiceIds() {
 if (invoiceDetailsInvoiceIds == null){
 invoiceDetailsInvoiceIds = new HashSet();
 }
 return invoiceDetailsInvoiceIds;
 }

 public void setInvoiceDetailsInvoiceIds (Set<InvoiceDetails> invoiceDetailsInvoiceIds) {
 this.invoiceDetailsInvoiceIds = invoiceDetailsInvoiceIds;
 }    

 public void addInvoiceDetailsInvoiceId (InvoiceDetails invoiceDetails) {
 getInvoiceDetailsInvoiceIds().add(invoiceDetails);
 }

}

That's all. If you like just use it.

About these ads
Tagged with:

9 Responses

Subscribe to comments with RSS.

  1. JPA2 Reverse Engineering Tool said, on February 4, 2011 at 3:42 pm

    [...] there and I discovered a very very nice tool that does reverse engineering and creates a… [full post] Cristian Java Code Samples 0 0 0 0 0 [04 Feb [...]

  2. Kenneth said, on February 5, 2011 at 9:40 am

    Looks great, will definitely try it.
    Thanks for sharing !

  3. kris said, on February 5, 2011 at 12:15 pm

    Isn’t it what netbeans has already build in?

  4. Cristian said, on February 5, 2011 at 12:41 pm

    There is something in netbeans but i say that is not so evolved.
    I tried it no so long ago and for example on the product from invoice line there was mapped
    an int not an product entity .

  5. [...] This post was mentioned on Twitter by Miloš Jovanović and Christophe Ribeiro, cristian.chiovari. cristian.chiovari said: RT @DZone "JPA2 Reverse Engineering Tool" http://dzone.com/IfCS [...]

  6. Sandeep said, on February 9, 2011 at 10:25 am

    Great article!! I liked it..

  7. [...] JPA2 Reverse Engineering Tool (Cristian) • Simplest possible POJO inje tion example with Java EE 6 (Adam Bien) • [...]

  8. GlassFish Tips and Links #22 | Dark Blog said, on February 13, 2011 at 8:11 pm

    [...] JPA2 Reverse Engineering Tool (Cristian) • Simplest possible POJO inje tion example with Java EE 6 (Adam Bien) • [...]

  9. GlassFish Tips and Links #22 said, on February 18, 2011 at 2:46 pm

    [...] JPA2 Reverse Engineering Tool (Cristian) • Simplest possible POJO inje tion example with Java EE 6 (Adam Bien) • [...]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: