Frameworks| Hibernate| Struts| JSF| JavaFX| Ajax| Spring| DOJO| JDO| iBatis| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
Writing Hibernate Configuration Files 
 

In the previous section we completed the database setup and created required table and populated with the data. In this section we will write required hibernate configuration files.

 

Writing Hibernate Configuration Files

                         

In the previous section we completed the database setup and created required table and populated with the data. In this section we will write required hibernate configuration files.

For this tutorial we need following Hibernate configuration files:

Hibernate Configuration File

Hibernate configuration file (hibernate.cfg.xml) is used to provide the information which is necessary for making database connections. The mapping details for mapping the domain objects to the database tables are also a part of Hibernate configuration file. 

Here is the code of our Hibernate Configuration File:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/struts-hibernate</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- Mapping files -->
<mapping resource="/roseindia/net/dao/hibernate/Tutorial.hbm.xml"/>
</session-factory>
</hibernate-configuration> 

 

Place hibernate.cfg.xml file in the source directory e.g. "C:\Struts-Hibernate-Integration\code\src\java"

The <mapping resource=''> tag is used to specify the mapping file:

<mapping resource="/roseindia/net/dao/hibernate/Tutorial.hbm.xml"/>

Code of Tutorial.hbm.xml:

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping auto-import="true" default-lazy="false">

<class 
name="roseindia.net.dao.hibernate.Tutorial" 
table="tutorials"
>

<id
name="id"
type="java.lang.Integer"
column="id"
>
<generator class="increment" />
</id>

<property
name="shortdesc"
type="java.lang.String"
column="shortdesc"
not-null="true"
length="50"
/>
<property
name="longdesc"
type="java.lang.String"
column="longdesc"
not-null="true"
length="250"
/>
<property
name="pageurl"
type="java.lang.String"
column="pageurl"
not-null="true"
length="100"
/>


</class>
</hibernate-mapping>

Place Tutorial.hbm.xml file in the source directory e.g. "C:\Struts-Hibernate-Integration\code\src\java\roseindia\net\dao\hibernate\

POJO Object


Here is the code of Java Bean object (Tutorial.java) used to store and retrieve the data from database.

package roseindia.net.dao.hibernate;

import java.io.Serializable;


public class Tutorial implements Serializable {

    /** identifier field */
    private Integer id;

    /** persistent field */
    private String shortdesc;

    /** persistent field */
    private String longdesc;

    /** persistent field */
    private String pageurl;

    /** full constructor */
    public Tutorial(Integer id, String shortdesc, String longdesc, String pageurl) {
        this.id = id;
        this.shortdesc = shortdesc;
        this.longdesc = longdesc;
        this.pageurl = pageurl;
    }

    /** default constructor */
    public Tutorial() {
    }

    public Integer getId() {
        return this.id;
    }

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

    public String getShortdesc() {
        return this.shortdesc;
    }

    public void setShortdesc(String shortdesc) {
        this.shortdesc = shortdesc;
    }

    public String getLongdesc() {
        return this.longdesc;
    }

    public void setLongdesc(String longdesc) {
        this.longdesc = longdesc;
    }

    public String getPageurl() {
        return this.pageurl;
    }

    public void setPageurl(String pageurl) {
        this.pageurl = pageurl;
    }

}

In this section we have created all the Hibernate related stuffs.

                         

» View all related tutorials
Related Tags: c apache http hibernate deployment com file j2ee ant development ui process build application script software struts io cgi download

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

5 comments so far (
post your own) View All Comments Latest 10 Comments:

i usually get SAX parse exception even the file is properly placed i also donot find any problem in validation with dtd. is there any soln. a warning i also get about to use log4j

Posted by sudhansu on Monday, 06.25.07 @ 10:55am | #20126

The <generator class = "increment"> in the <id> element tells hibernate to generate the primary key "id" by incrementing by 1.

Thanks
Deepak

Posted by Deepak Kumar on Friday, 04.6.07 @ 12:23pm | #13457

Should the POJO class always be Serializable? Why?
And what are the use of default constructors in the POJO class?

Posted by prosenjit on Friday, 04.6.07 @ 10:04am | #13448

In the Tutorial.hbm.xml file, what is the use of <generator class = "increment"> in the <id> element?

Posted by prosenjit on Friday, 04.6.07 @ 10:03am | #13447

Hi i'm trying to implement the above example but i got fatal exception . first of all i couldnt able to create sessionfactory. null pointer exception occured at the creating of sessionfactory. pls do advise me. Thanks

Posted by sarath koiloth ramath on Wednesday, 01.3.07 @ 12:26pm | #2528

Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.