Reverse Engineer Hibernate Value Object Mappings hbm.xml Examples Step 17
Reverse Engineer your Hibernate Mappings
********************************************
If you already have the database built, you can reverse engineer to create your value objects and your hibernate mappings. This will save you time in creating each of your value objects and mappings manually.
Eclipse has some tools to accomplish this. I used the openlogic plugin.
You should visit this website: https://openlogic.com/packages/eclipseplugin-hibernate_tools to retrieve the plugin.
This website is https so you will have to bypass the security warning.
You should download the openlogic-eclipseplugin-hibernate_tools-3.2.1.GA-all-bin-1.zip
Make sure you have the mysql.jar file you in your lib directory with the eclipse build path pointing to it. The eclipse plugin will need this to connect to the database.
When you download the plugin, you should unzip it and copy all of the directories and jar files from the plugins directory to the eclipse plugin directory.
**************************************************
There are a couple of possible errors you can receive when using this plugin.
**************************************************
1) java.lang.ClassNotFoundException: org.eclipse.jface.viewers.BaseLabelProvider
This error is because you are using an older version of eclipse. You will need to upgrade to a more current version of eclipse to use this plugin.
2) java.lang.ClassNotFoundException: org/eclipse/ui/internal/util/SWTResourceUtil
This error is because you are using a version of Ganymede Eclipse which is missing this class. You will need to add this class by following these steps:
2a) You need to get this class from an older version of Eclipse. I used the eclipse version 3.2.2. You should download this version or another version and locate this file in the eclipse plugin directory. org.eclipse.ui.workbench*.jar.
2b) Unzip this jar file and remove the SWTResourceUtil.class file.
2c) Locate the org.eclipse.ui.workbench_*.jar in your current eclipse directory. You should change the extension from jar to zip and then unzip it. Drop the class in the org/eclipse/ui/internal/util folder. Then zip it back up and change the extension back to .jar and place the jar file back in the plugin directory of eclipse.
3) Missing sun.nio.cs.surrogate
This error means that you didn't copy all of the jar files from the plugin into eclipse.
***********************************************
After you have the plugin installed, then restart eclipse.
From my prior posts you should have already established a connection to the database using Hibernate.
After opening up eclipse, select windows->open perspective->other. Select Hibernate from the menu.
In the Hibernate Configuration window, select the right mouse button and select configuration.
Project: select the project you are working on.
Property file: If you are using a properties file, select it from here.
Configuration File: You need to select the configuration file that holds your database information.
Apply the changes.
Now we want to create your mappings directly from the database.
You should be able to select the database and see all the tables that you are using in your project from your configuration information in your hibernate mappings.
At the top menu you will see a drop down where you are able to select Hibernate Code Generation and Configuration. Select it to generate your mapping files.
On the Hibernate Code Generation link on the left, you should select the right mouse button and select new to open the configuration page.
In the Console Configuration, select hibernate.
Select what output directory you want to use. This is the directory where the mappings will be copied too. You should put them in the com.vo package which is the package holding your value objects.
Select the reverse enginner check box.
You should select the exporters tab and select the Hiberante XML Mappings check box and Domain code.
You should press apply and then run to generate your mappings.
If you were unable to get the reverse engineering to work, then below I have the files you will need for this tutorial. If you do not reverse engineer, you can create the mappings manually.
If you reverse engineer, you will need to make sure you update the java objects to be the correct packages. You should also add a serializable variable "private static final long serialVersionUID=123l;" to each value object class.
***************************
VO Objects and Mappings
***************************
Book.java
package com.vo;
// Generated Oct 21, 2008 7:43:57 PM by Hibernate Tools 3.2.1.GA
import java.util.HashSet;
import java.util.Set;
/**
* Book generated by hbm2java
*/
public class Book implements java.io.Serializable {
private static final long serialVersionUID=123l;
private long id;
private Bookcategory bookcategory;
private String bookName;
private String bookAuthor;
private String bookPublisher;
private Set userses = new HashSet(0);
public Book() {
}
public Book(long id) {
this.id = id;
}
public Book(long id, Bookcategory bookcategory, String bookName,
String bookAuthor, String bookPublisher, Set userses) {
this.id = id;
this.bookcategory = bookcategory;
this.bookName = bookName;
this.bookAuthor = bookAuthor;
this.bookPublisher = bookPublisher;
this.userses = userses;
}
public long getId() {
return this.id;
}
public void setId(long id) {
this.id = id;
}
public Bookcategory getBookcategory() {
return this.bookcategory;
}
public void setBookcategory(Bookcategory bookcategory) {
this.bookcategory = bookcategory;
}
public String getBookName() {
return this.bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getBookAuthor() {
return this.bookAuthor;
}
public void setBookAuthor(String bookAuthor) {
this.bookAuthor = bookAuthor;
}
public String getBookPublisher() {
return this.bookPublisher;
}
public void setBookPublisher(String bookPublisher) {
this.bookPublisher = bookPublisher;
}
public Set getUserses() {
return this.userses;
}
public void setUserses(Set userses) {
this.userses = userses;
}
}
Book.hbm.xml
package com.vo;
// Generated Oct 21, 2008 7:43:57 PM by Hibernate Tools 3.2.1.GA
import java.util.HashSet;
import java.util.Set;
/**
* Users generated by hbm2java
*/
public class Users implements java.io.Serializable {
private static final long serialVersionUID=123l;
private long id;
private String firstName;
private String lastName;
private String middleName;
private String email;
private Set books = new HashSet(0);
public Users() {
}
public Users(long id) {
this.id = id;
}
public Users(long id, String firstName, String lastName, String middleName,
String email, Set books) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.middleName = middleName;
this.email = email;
this.books = books;
}
public long getId() {
return this.id;
}
public void setId(long id) {
this.id = id;
}
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return this.lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getMiddleName() {
return this.middleName;
}
public void setMiddleName(String middleName) {
this.middleName = middleName;
}
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
public Set getBooks() {
return this.books;
}
public void setBooks(Set books) {
this.books = books;
}
}
package com.vo;
// Generated Oct 21, 2008 7:43:57 PM by Hibernate Tools 3.2.1.GA
import java.util.HashSet;
import java.util.Set;
/**
* Bookcategory generated by hbm2java
*/
public class Bookcategory implements java.io.Serializable {
private static final long serialVersionUID=123l;
private long id;
private String category;
private Set books = new HashSet(0);
public Bookcategory() {
}
public Bookcategory(long id) {
this.id = id;
}
public Bookcategory(long id, String category, Set books) {
this.id = id;
this.category = category;
this.books = books;
}
public long getId() {
return this.id;
}
public void setId(long id) {
this.id = id;
}
public String getCategory() {
return this.category;
}
public void setCategory(String category) {
this.category = category;
}
public Set getBooks() {
return this.books;
}
public void setBooks(Set books) {
this.books = books;
}
}
Bookcategory.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated Oct 21, 2008 7:44:22 PM by Hibernate Tools 3.2.1.GA --> <hibernate-mapping> <class name="Bookcategory" table="bookcategory" catalog="catalog"> <id name="id" type="long"> <column name="Id" /> <generator class="assigned" /> </id> <property name="category" type="string"> <column name="Category" length="50" /> </property> <set name="books" inverse="true"> <key> <column name="BookCategoryId" /> </key> <one-to-many class="Book" /> </set> </class> </hibernate-mapping>
After you have created all of your mappings, their are some possible issues that you will have to fix. See my next post for creating a DAO object and fixing any possible issues.
Go To Step 18

16 comments:
Hi Greg,
Tutorial is great. It has been very useful.
Thank you!!!
I'd wish you continue with tutorial adding more difficult steps.
Thanks David. I will be adding more information shortly.
Hi Greg,
I am using Ganymede Eclipse version, and having a problem with openlogic plugin. I am getting "java.lang.ClassNotFoundException: org/eclipse/ui/internal/util/SWTResourceUtil" error exactly as you mentioned.
I did the following change as you said. located the SWTResourceUtil.class(org.eclipse.ui.workbench_3.2.0.I20060605-1400.jar) from older eclipse version 3.2 and copied to org/eclipse/ui/internal/util/ in(org.eclipse.ui.workbench_3.4.1.M20080827-0800a.jar) my Ganymede version.
After doing this eclipse didn't work properly, even I couldn't restart, after the undone of the previous step again I got the "java.lang.ClassNotFoundException: org/eclipse/ui/internal/util/SWTResourceUtil" error as usual.
How can I resolve this, Please help me to solve this issue. Your tutorial is very useful to me, now I have stuck here.
Nirodha
Hi David. It actually took me a couple of times to resolve that issue and eventually I got it to work. I would suggest trying it on a new version of eclipse to see if you can get it to work so you don't screw up the one you are using. make sure you put it in the correct directory that the class belongs to. If you want to post your email address, I will send you the jar file that I got working. Therefore, you can just drop it in the directory and hopefully it will work.
Hi Greg,
Thanks for the reply.
Please send me that jar file to this email.
nirodha23@gmail.com
Thanks
Nirodha
I sent it. Let me know if this works. Make a backup of the prior jar file incase you need to undo.
Hi Greg,
I tried with your file, but it did not work. So I am planning to go for another eclipse version.
If you hv any other options in your mind pls do let me know.
Thanks
Nirodha
I downloaded a new version of eclipse and noticed that the file that I sent you wasn't there. Maybe I downloaded a different version of eclipse. If you see the file org.eclipse.ui.workbench_3.4.0.I20080606-1300.jar instead of the one I sent you, then you can use that one. The version of eclipse I am using is 3.4.0. Change the extension of that jar file to zip. Pull the needed class from the jar file I sent you. Locate the same directory in in the workbench_3.4.0 and drop that class into that jar file and change the extension back to .jar. That jar file should do the trick. Try that and see if it works. :)
Hi Greg,
I downloaded the eclipse 3.4.0(http://www.eclipse.org/downloads/download.php?file=/technology/epp/downloads/release/ganymede/R/eclipse-java-ganymede-win32.zip)-85.26 MB.
Configured the hibernate-eclipse openlogic plug-in as you explained, when I tried to use Hibernate I got that same error, then I located the org.eclipse.ui.workbench_3.4.0.I20080606-1300.jar file and unzipped it and placed the class file that you sent in the proper place and again zipped and changed the extension to jar. Then I replaced the jar with the old one. After that I was even unable to start eclipse IDE. I got a error log for that.
I will mail you old jar file, new jar file and the error log, if you find time please have a look and try to help me.
Thank You so much.
Nirodha
I am glad you got it to work. :)
Hi Greg,
I used reverse engineer to generate POJOs and hibernate xml mappings for the database. I was able to generate those files for all the tables except "userbook".
What is the reason for that?
When I try to run the existing setup I got following error, what we can learn from this?
org.hibernate.MappingException: An association from the table userbook refers to an unmapped class: Book
org.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:1285)
org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1203)
org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1319)
com.util.HibernateUtil.(HibernateUtil.java:26)
Nirodha
It probably means that you do not have your Book.hbm.xml file listed in your user.hibernate.cfg.xml file. That file needs to contain all of your mappings. It should have a mapped resource to Users.hbm.xml, Book.hbm.xml, and Bookcategory.hbm.xml. If you need to, you can have openlogic generate your user.hibernate.cfg.xml file. Check to make sure you have those mappings listed. Let me know if that is the issue.
Yes that's correct, you have explained it in your next step(18).
I got it resolved.
Thanks
great, thanks.
Thanks Greg, that was such a relief to get the revEng fix...
Despite the popularity of the concept of reverse engineering with sections of the engineering community, the legislative position restricts reverse engineering of hardware and makes reverse engineering of software legal only under very limited circumstances in Europe. In the US, slightly more freedom is available to reverse engineers, but great care is still needed to avoid infringement.
creatine
Post a Comment