JPA2 Reverse Engineering Tool
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
- Start the MP console (<MP_HOME>/bin/start-console.(cmd/sh). How to use the console.
- Fill the connection information to the datamodel
- Fill the model general information (model, package names, version primary key policy, scope).
- Choose JPA2 target
- 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.



[...] 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 [...]
Looks great, will definitely try it.
Thanks for sharing !
Isn’t it what netbeans has already build in?
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 .
[...] 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 [...]
Great article!! I liked it..
[...] JPA2 Reverse Engineering Tool (Cristian) • Simplest possible POJO inje tion example with Java EE 6 (Adam Bien) • [...]
[...] JPA2 Reverse Engineering Tool (Cristian) • Simplest possible POJO inje tion example with Java EE 6 (Adam Bien) • [...]
[...] JPA2 Reverse Engineering Tool (Cristian) • Simplest possible POJO inje tion example with Java EE 6 (Adam Bien) • [...]