Hibernate (Part 2): configuration and use

December 2016



Hibernate configuration in a Java application

  • Install JDK 1.4 or JDK 1.5.
  • Download version 3 from hibernate.org
  • Reference the Hibernate jars in the classpath.
  • Define the Hibernate configuration file (hibernate.cfg.xml):


This file must be placed in the directory containing the source code of the JAVA application (recommended).

Below is an example of hibernate.cfg.xml file with necessary comments:

<?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.url">jdbc:oracle:thin:@dbServer:1521:db</property>
<property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="hibernate.connection.username">dbusername</property>
<property name="hibernate.connection.password">dbpassword</property>


<!-- Configuration hibernate -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hibernate.use_outer_join">true</property>
<property name="hibernate.query.substitutions">1</property>
<property name="hibernate.connection.autocommit">false</property>
<property name="hibernate.jdbc.batch_size">50</property>
<property name="hibernate.jdbc.use_get_generated_keys">true</property>


<!-- Pool de connexion : ici C3P0 qui est déclaré -->
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="hibernate.c3p0.acquire_increment">3</property>
<property name="hibernate.c3p0.idle_test_period">180</property>
<property name="hibernate.c3p0.max_size">100</property>
<property name="hibernate.c3p0.min_size">10</property>
<property name="hibernate.c3p0.timeout">1000</property>

<!--désactiver la cache deuxième niveau -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>




<!--Liste des fichiers de Mapping hbm -->
<mapping resource="com/org/client/mapping/Client.hbm.xml"/>


</session-factory>
</hibernate-configuration>

Define the hbm mapping files:


This is a set of files that makes the mapping between the entity database and associated JAVA objects.
Full paths of all hbm mapping files are described in configuration file (hibernate.cfg.xml).

Using Hibernate in a Java application:


In this example we will use the Hibernate configuration file:
  • Declaration of the table (Oracle)

CREATE TABLE CLIENT
(
ID_Client NUMBER(10) NOT NULL,
TITRE VARCHAR2(10),
NOM VARCHAR2(30),
REMISE NUMBER(19,5),
CA NUMBER(19,5),
CONSTRAINT PK_CLIENT PRIMARY KEY (CLIENT_ID)
);
  • Declaration of JAVA classes



/** constructeur par défaut */
public Client() {
}

/** Constructeur complet **/
public Client(Long clientId, String titre, String nom, BigDecimal remise, BigDecimal ca)
{
this.idClient = idClient;
this.titre = titre;
this.name = name;
this.remise = remise;
this.ca = ca;
}

/** Les getters et setters**/
public Long getIdClient()
{
return this.clientId;
}
public void setIdClient (Long clientId) {
this.clientId = clientId;
}
public String getTitre() {
return this.titre;
}
public void setTitre(String titre) {
this.titre = titre;
}
public String getNom() {
return this.nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public BigDecimal getRemise() {
return this.remise;
}
public void setRemise(BigDecimal remise) {
this.remise = remise;
}
public BigDecimal getCa() {
return this.ca;
}
public void setCa(BigDecimal ca) {
this.ca = ca;
}
}
  • Client.hbm.xml mapping files:


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

<hibernate-mapping>
<class
name="[Nom_Package].Client"
table="CLIENT"
entity-name="Client"
>
<meta attribute="class-description" inherit="false">
@hibernate.class
table="CLIENT"
</meta>

<id
name="idClient"
type="java.lang.Long"
column="ID_CLIENT"
>
<meta attribute="field-description" inherit="false">
Id du client
</meta>

<meta attribute="field-description">
@hibernate.id
generator-class="assigned"
type="java.lang.Long"
column="CLIENT_ID"

</meta>
<generator class="increment" />
</id>

<property
name="titre"
type="java.lang.String"
column="TITRE"
length="10"
>
<meta attribute="field-description" inherit="false">
Titre du client
</meta>
<meta attribute="field-description">
@hibernate.property
column="TITRE"
length="10"
</meta>
</property>
<property
name="name"
type="java.lang.String"
column="NAME"
length="30"
>
<meta attribute="field-description" inherit="false">
Nom du client
</meta>
<meta attribute="field-description">
@hibernate.property
column="NAME"
length="30"
</meta>
</property>

<property
name="remise"
type="java.math.BigDecimal"
column="REMISE"
length="19"
>
<meta attribute="field-description" inherit="false">
Remise du client
</meta>
<meta attribute="field-description">
@hibernate.property
column="REMISE"
length="19"
</meta>
</property>
<property
name="ca"
type="java.math.BigDecimal"
column="CA"
length="19"
>
<meta attribute="field-description" inherit="false">
Chiffre Affaire
</meta>


</class>
</hibernate-mapping>


The primary key is defined in the <id> tag: in our example, it is based on a sequence. It is possible to define a composite key using the <composite-id> tag:
composite-id>
<key-property name="attribut1"/>
<key-property name="attribut2"/>
</composite-id>
  • Saving a new object in the database:


The example below uses the saveOrUpdate () method of the Hibernate session.

org.hibernate.Session sess = sessFact.openSession(); 
Client c = new Client(); 
c.setName("John"); 
p.setTitre("Mr"); 
Transaction tx = sess.beginTransaction(); 
sess.saveOrUpdate(p); 
tx.commit(); 
sess.close();

Related :

This document entitled « Hibernate (Part 2): configuration and use » from CCM (ccm.net) is made available under the Creative Commons license. You can copy, modify copies of this page, under the conditions stipulated by the license, as this note appears clearly.