Connecting our Class(say x) to DataBase using Hibernate



In this file the mapping from our class Honey to the database table honey is configured.

Create a Test Client

Create a Java Class ?TestClient? in the package ?de.laliluna.example?.

Add the following source code. It includes methods to create entries in the database, to update and to list them.


/**
* Test application for example
* @author Sebastian Hennebrueder
* created Jan 16, 2006
* copyright 2006 by http://www.laliluna.de
*/

package de.laliluna.example;

import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import de.laliluna.hibernate.InitSessionFactory;

public class TestExample {

private static Logger log =Logger.getLogger(TestExample.class);
/**
* @param args
*/
public static void main(String[] args) {
Honey forestHoney = new Honey();
forestHoney.setName(“forest honey”);
forestHoney.setTaste(“very sweet”);
Honey countryHoney = new Honey();
countryHoney.setName(“country honey”);
countryHoney.setTaste(“tasty”);
createHoney(forestHoney);
createHoney(countryHoney);
// our instances have a primary key now:
log.debug(forestHoney);
log.debug(countryHoney);
listHoney();
deleteHoney(forestHoney);
listHoney();

}

private static void listHoney() {
Transaction tx = null;
Session session = InitSessionFactory.getInstance().getCurrentSession();
try {
tx = session.beginTransaction();
List honeys = session.createQuery(“select h from Honey as h”)
.list();
for (Iterator iter = honeys.iterator(); iter.hasNext();) {
Honey element = (Honey) iter.next();
log.debug(element);
}
tx.commit();
} catch (HibernateException e) {
e.printStackTrace();
if (tx != null && tx.isActive())
tx.rollback();

}
}

private static void deleteHoney(Honey honey) {
Transaction tx = null;
Session session = InitSessionFactory.getInstance().getCurrentSession();
try {
tx = session.beginTransaction();
session.delete(honey);
tx.commit();
} catch (HibernateException e) {
e.printStackTrace();
if (tx != null && tx.isActive())
tx.rollback();
}
}

private static void createHoney(Honey honey) {
Transaction tx = null;
Session session = InitSessionFactory.getInstance().getCurrentSession();
try {
tx = session.beginTransaction();
session.save(honey);
tx.commit();
} catch (HibernateException e) {
e.printStackTrace();
if (tx != null && tx.isActive())
tx.rollback();
}
}
}

Congratulations. You have finished your first steps in the Hibernate world.

We wanted to give you a fast entry in the Hibernate world. There are many more complex topics and better implementation to use Hibernate. For example, the opening and closing of the session in each method is not a good practice. A session can be reused during which saves a lot of time.

When you want to learn more about best practices have a look at our seminars or other tutorials.
Copyright and disclaimer

This tutorial is copyright of Sebastian Hennebrueder, laliluna.de. You may download a tutorial for your own personal use but not redistribute it. You must not remove or modify this copyright notice.

The tutorial is provided as is. I do not give any warranty or guaranty any fitness for a particular purpose. In no event shall I be liable to any party for direct, indirect, special, incidental, or consequential damages, including lost profits, arising out of the use of this tutorial, even if I has been advised of the possibility of such damage.

Random Posts

  • Cookie Examples
  • Life Cycle of Thread
  • Creating a Text file using Java
  • Write a short notes on TOMCAT
  • Thread example for Runnable Interface

Post a Comment