Hibernate Configuration File

Hibernate configuration file (hibernate.cfg.xml) have information used for making database connections. The mapping details for mapping the domain objects to the database tables are also a part of Hibernate configuration file. This is used to have details for connections with database and can be changed without change in any source file.

Here is the code of 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:9999/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=”/bhanuApp/hibernate/Tutorial.hbm.xml”/>
</session-factory>
</hibernate-configuration>

Place hibernate.cfg.xml file in the source directory e.g. “X:Struts-Hibernatesrcjavasrc”

The <mapping resource=”> tag is used to specify the mapping file:
<mapping resource=”/bhanuApp/net/dao/hibernate/BhanuTutorial.hbm.xml”/>

Code of BhanuTutorial.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=”bhanuApp.net.dao.hibernate.BhanuTutorial” table=”Bhanututorials”
>

<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=”url”
type=”java.lang.String”
column=”url”
not-null=”true”
length=”100″
/>
</class>
</hibernate-mapping>

Place Tutorial.hbm.xml file in the source directory e.g. “C:Struts-HibernatesrcjavabhanuAppnetdaohibernate

POJO Object

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

package bhanuApp.net.dao.hibernate;

import java.io.Serializable;


public class BhanuTutorial implements Serializable {

  /** identifier field */
  private Integer id;

  /** persistent field */
  private String shortdesc;

  /** persistent field */
  private String longdesc;

  /** persistent field */
  private String url;

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

  /** 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 getUrl() {
  return this.url;
  }

  public void setUrl(String url) {
  this.url = url;
  }

}