How to create a hibernate mapping XML file automatically ?

How to create a hibernate mapping XML file automatically using hibernate tools?

 

In hibernate ORM (Object Relational Mapping) we have to use either XML or annotations to map the Java POJO [Plain Old Java Objects] with table entities.

 

Everyone is thinking that mapping using XML is very difficult, but it is very simple in eclipse.

 

Prerequisite: 

Hibernate tools should be installed in your eclipse.

Don’t you have idea on how to install hibernate tools in eclipse ? Don’t worry. Just follow the steps given in the below link and get it installed.

Installing Hibernate tools in Eclipse Juno

 

Now create a TestProject in Eclipse and create a POJO like this,

 

Author.java:

[java]

public class Author {

private int authorID;

private String authorName;

private String siteName;

private String sitePurpose;

public String getSiteName() {
return siteName;
}

public void setSiteName(String siteName) {
this.siteName = siteName;
}

public String getSitePurpose() {
return sitePurpose;
}

public void setSitePurpose(String sitePurpose) {
this.sitePurpose = sitePurpose;
}

public int getAuthorID() {
return authorID;
}

public void setAuthorID(int authorID) {
this.authorID = authorID;
}

public String getAuthorName() {
return authorName;
}

public void setAuthorName(String authorName) {
this.authorName = authorName;
}

}

[/java]

 

 

Now we are going to create a hibernate mapping file automatically using the hibernate tools which we installed in eclipse above.

 

Rightclick on the src folder or project name and search for hibernate then follow the below steps

 

Select Hibernate XML Mapping file (hbm.xml) and click Next.

How to create hibernate mapping XML file automatically using hibernate tools

 

 

Select your package and class here,

 

How to create hibernate mapping XML file automatically using hibernate tools

 

 

Class name is Author, so by default hibernate mapping xml file name will be Author.hbm.xml,

 

How to create hibernate mapping XML file automatically using hibernate tools

 

Hibernate mapping xml file preview:
How to create hibernate mapping XML file automatically using hibernate tools

 

 

Hibernate generated XML file [author.hbm.xml]

[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 Sep 21, 2015 6:08:21 PM by Hibernate Tools 3.4.0.CR1 –>
<hibernate-mapping>
<class name="in.javadomain.Author" table="AUTHOR">
<id name="authorID" type="int">
<column name="AUTHORID" />
<generator class="assigned" />
</id>
<property name="authorName" type="java.lang.String">
<column name="AUTHORNAME" />
</property>
<property name="siteName" type="java.lang.String">
<column name="SITENAME" />
</property>
<property name="sitePurpose" type="java.lang.String">
<column name="SITEPURPOSE" />
</property>
</class>
</hibernate-mapping>

[/xml]

 

 

Hope you are clear.

 

Feel free to share your comments/suggestions in the below comments area.

 

 

 

 

 

 

Leave a Reply