Hibernate getting started

Getting started with Hibernate:

Getting started with Hibernate 4.1.8 on Eclipse Juno with Hibernate Tools 3.4.0.

My recommendation is to use those as well. It is also not part of this post to install the mysql database.

Download Hibernate 4 here and JDBC driver here.

 

Description:

We have got a house and a person. A Person can “live” in one house. A house can have many people living in it.

 

Setting up the project:

Create a new Java Project which is called DuckoutHibernate.

Create a Java Project Duckout.de

Add the following libs to your classpath. (You just downloaded these libs)

 

Now we are prepared to use Hibernate…

We will have the following project structure:

Hibernate Getting Started Project Structure

 

All .java files can be created like normal Java classes, to create the .hbm.xml files I used Hibernate Tools 3.4.0 in Eclipse.

Start creating all the Java classes:

de.duckout.bl.HouseManager

Manages the persistence methods for class House.

package de.duckout.bl;

import org.hibernate.Session;

import de.duckout.db.House;
import de.duckout.sf.HibernateUtil;

public class HouseManager {

public void saveHouse(House house) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();

session.beginTransaction();
session.save(house);
session.getTransaction().commit();
}
}

de.duckout.bl.PersonManager

Similar to the other Manager but for the Person class.

package de.duckout.bl;

import org.hibernate.Session;

import de.duckout.db.Person;
import de.duckout.sf.HibernateUtil;

public class PersonManager {

public void savePerson(Person person) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();

session.beginTransaction();
session.save(person);
session.getTransaction().commit();
}

}

de.duckout.db.House

A Java bean with information about the house.

package de.duckout.db;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

public class House implements Serializable {

private static final long serialVersionUID = 1L;

private int id;

private String street;
private int number;

private int zip;
private String city;
private String country;

private Set persons = new HashSet();

public House() {

}

public House(String street, int number, String city, String country,
Set persons) {
this.street = street;
this.number = number;
this.city = city;
this.country = country;
this.persons = persons;
}

public int getId() {
return id;
}

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

public String getStreet() {
return street;
}

public void setStreet(String street) {
this.street = street;
}

public int getNumber() {
return number;
}

public void setNumber(int number) {
this.number = number;
}

public int getZip() {
return zip;
}

public void setZip(int zip) {
this.zip = zip;
}

public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}

public String getCountry() {
return country;
}

public void setCountry(String country) {
this.country = country;
}

public Set getPersons() {
return persons;
}

public void setPersons(Set persons) {
this.persons = persons;
}

public String toString() {
return street + number;
}
}

de.duckout.db.Person

Also a Java bean with information about the Person

package de.duckout.db;

import java.io.Serializable;

public class Person implements Serializable{

private static final long serialVersionUID = 1L;

private int id;

private String lastname;
private String firstname;
private int age;
private House house;

public Person(){

}

public Person(String lastname, String firstname, int age, House house){
this.lastname = lastname;
this.firstname = firstname;
this.age = age;
this.house = house;
}

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}

public String toString(){
return lastname;
}

public House getHouse() {
return house;
}

public void setHouse(House house) {
this.house = house;
}

}

de.duckout.sf.HibernateUtil

Handles the conection to the database. Is used by the “manager classes”.


package de.duckout.sf;

import org.hibernate.cfg.*;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.SessionFactory;

public class HibernateUtil {

private static final SessionFactory sessionFactory;
private static final ServiceRegistry serviceRegistry;

static {
try {
// Create the SessionFactory from hibernate.cfg.xml
Configuration configuration = new Configuration();
configuration.configure();
serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);

System.out.println("Initial SessionFactory creation");
} catch (Throwable ex) {

System.out.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}

public static SessionFactory getSessionFactory() {
return sessionFactory;
}

}

 

Now that we have our beans you can create the .hbm.xml files. Therefore you can use the Hibernate Tools like this:

(Right click on the project -> New -> Other… -> Hibernate -> …)

 

The .hbm.xml files should have the following XML Code:

House.hbm.xml

<!--?xml version="1.0"?-->

<!-- Generated 19.11.2012 20:30:59 by Hibernate Tools 3.4.0.CR1 -->










 

Person.hbm.xml

<!--?xml version="1.0"?-->

<!-- Generated 19.11.2012 20:30:59 by Hibernate Tools 3.4.0.CR1 -->







To get a connection to the database and to handle the usage of it we still have to create a hibernate.cfg.xml file.

database URL: localhost:3306/DuckoutDeDB
database user: root
passwort: root

Then the file must look similar to this coding (there are some parameter which are not necessary):

<!--?xml version="1.0" encoding="UTF-8"?-->



<!-- Database connection settings -->
com.mysql.jdbc.Driver jdbc:mysql://localhost:3306/DuckoutDeDB root root

<!-- JDBC connection pool (use the built-in) -->
1

<!-- SQL dialect -->
org.hibernate.dialect.MySQLDialect

<!-- Echo all executed SQL to stdout -->
true

threadtruecreate

<!-- Mapping files -->

 

The last point is to write a MainTest class to test the database connection.

de.duckout.test.TestMain

package de.duckout.test;

import de.duckout.bl.HouseManager;
import de.duckout.bl.PersonManager;
import de.duckout.db.House;
import de.duckout.db.Person;

public class TestMain {

/**
* @param args
*/
public static void main(String[] args) {

// Create a house
House house = new House();
house.setCity("Karlsruhe");
house.setCountry("Germany");
house.setNumber(999999);
house.setStreet("Kaiserallee");
house.setZip(76131);

// Create a person
Person max = new Person();
max.setLastname("Musterfrau");
max.setFirstname("Maxi");
max.setAge(22);
max.setHouse(house);

HouseManager houseManager = new HouseManager();
houseManager.saveHouse(house);

PersonManager personManager = new PersonManager();
personManager.savePerson(max);

}

}

Posted on by Monte in Java Leave a comment

Add a Comment