Hibernate (Part 2): configuration and use
On May 25,2022 by Tom RoutleyHibernate configuration in a Java application
Install JDK 1.4 or JDK 1.5.
Download version 3 from hibernaterg
Reference the Hibernate jars in the classpath.
Define the Hibernate configuration file (hibernatefg.xml):
This file must be placed in the directory containing the source code of the JAVA application (recommended).
Below is an example of hibernatefg.xml file with necessary comments:
/p>
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"[http://hibernaterg/dtd/hibernate-configuration-3.0.dtd">
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 (hibernatefg.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;
thiitre = titre;
thiame = name;
thiemise = remise;
thia = ca;
}
/** Les getters et setters**/
public Long getIdClient()
{
return thilientId;
}
public void setIdClient (Long clientId) {
thilientId = clientId;
}
public String getTitre() {
return thiitre;
}
public void setTitre(String titre) {
thiitre = titre;
}
public String getNom() {
return thiom;
}
public void setNom(String nom) {
thiom = nom;
}
public BigDecimal getRemise() {
return thiemise;
}
public void setRemise(BigDecimal remise) {
thiemise = remise;
}
public BigDecimal getCa() {
return thia;
}
public void setCa(BigDecimal ca) {
thia = ca;
}
}
Client.hbm.xml mapping files:
/p>
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernaterg/dtd/hibernate-mapping-3.0.dtd" >
name="[Nom_Package].Client" table="CLIENT" entity-name="Client" > @hibernatelass table="CLIENT" name="idClient" type="java.lang.Long" column="ID_CLIENT" > Id du client @hibernate.id generator-class="assigned" type="java.lang.Long" column="CLIENT_ID" name="titre" type="java.lang.String" column="TITRE" length="10" > Titre du client @hibernate.property column="TITRE" length="10" name="name" type="java.lang.String" column="NAME" length="30" > Nom du client @hibernate.property column="NAME" length="30" name="remise" type="javaath.BigDecimal" column="REMISE" length="19" > Remise du client @hibernate.property column="REMISE" length="19" name="ca" type="javaath.BigDecimal" column="CA" length="19" > Chiffre Affaire
The primary key is defined in the
composite-id>
Saving a new object in the database:
The example below uses the saveOrUpdate () method of the Hibernate session.
org.hibernate.Session sess = sessFacpenSession(); Client c = new Client(); c.setName("John"); p.setTitre("Mr"); Transaction tx = sess.beginTransaction(); sess.saveOrUpdate(p); ommit(); seslose();
Article Recommendations
Latest articles
Popular Articles
Archives
- November 2024
- October 2024
- September 2024
- August 2024
- July 2024
- June 2024
- May 2024
- April 2024
- March 2024
- February 2024
- January 2024
- December 2023
- November 2023
- October 2023
- September 2023
- August 2023
- July 2023
- June 2023
- May 2023
- April 2023
- March 2023
- February 2023
- January 2023
- December 2022
- November 2022
- October 2022
- September 2022
- August 2022
- July 2022
- June 2022
- May 2022
- April 2022
- March 2022
- February 2022
- January 2022
- December 2021
- November 2021
- October 2021
- September 2021
- August 2021
- July 2021
- January 2021
Leave a Reply