Hibernate 3: When Night Strikes an Example Arrives

So, it’s 10:00 at night the day that this blog post is due for that class I’ve told you all about before. I think this is the perfect time to create an untested Hibernate project example for you all. Does that sound good to you, GREAT! Let’s get this started. Just so everyone knows we are taking some of the code from the demo on http://www.tutorialspoint.com/hibernate/hibernate_examples.htm to help us so we don’t have to type everything. However we are going to change this for something a bit more internet culture related for some fun.
First we need a database for something so let’s build one based on memes. The internet loves memes so well first build a database for them.

After that we need a table to house a type of meme, so let’s go for cats, almost everyone and their grandmother loves cats.

 name VARCHAR(20) default NULL,
 lols VARCHAR(20) default NULL,

Great now let’s create a Plain Old Java Object (POJO) for these cat memes.
public class CatMemes {
 private String name; 
 private String lols; 
 public CatMemes() {}
 public CatMemes(String name, String lols) {
 this.name = name;
 this.lols = lols;
 public String getName() {
 return name;
 public void setName( String meme_name ) {
 this.name = meme_name;
 public String getLols() {
 return lols;
 public void setLols( String lol_s) {
 this.lols = lol_s;

There, that wasn’t so hard now was it? So we now need the configuration file that will connect us with our database. Don’t forget to call it hibernate.cfg.xml.
<?xml version=”1.0" encoding=”utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM 

 <property name=”hibernate.dialect”>
 <property name=”hibernate.connection.driver_class”>

<! — Assume test is the database name →
 <property name=”hibernate.connection.url”>
 <property name=”hibernate.connection.username”>
 <property name=”hibernate.connection.password”>

<! — List of XML mapping files →
 <mapping resource=”CatMemes.hbm.xml”/>


And again we have now the third thing we need. Next let’s create the map that will be our path. Unfortunately we don’t get a song every time we call upon us, oh well. Another reminder to name this something like CatMemes.hbm.xml. CatMemes can be replaced by whatever your project is called.

<?xml version=”1.0" encoding=”utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
 “-//Hibernate/Hibernate Mapping DTD//EN”

 <class name=”CatMemes” table=”CATS”>
 <meta attribute=”class-description”>
 This class contains the cat meme name and the lols it contains. 
 <id name=”name” type=”VARCHAR” column=”name”>
 <generator class=”native”/>
 <property name=”name” column=”name” type=”string”/>
 <property name=”lols” column=”lols” type=”string”/>

Now after all that we need to create an application that will read our cat memes out to us. Just making a note here that all I’m doing for this demo is reading the data, however the demo on tutorial points allows you to delete, update and add data.
import java.util.List; 
import java.util.Date;
import java.util.Iterator; 
import org.hibernate.HibernateException; 
import org.hibernate.Session; 
import org.hibernate.Transaction;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class ManageCatMemes {
 private static SessionFactory factory; 
 public static void main(String[] args) {
 factory = new Configuration().configure().buildSessionFactory();
 }catch (Throwable ex) { 
 System.err.println(“Failed to create sessionFactory object.” + ex);
 throw new ExceptionInInitializerError(ex); 
 ManageCatMemes MCM = new ManageCatMemes();

/* Add a few Cat Memes in the database */
 String longCat = MCM.add(“Long Cat”, “So many Lols”);
 String cheeseBurgerCat = MCM.add(“Cheese Burger Cat”, “Hungry Lols”);
 String keyBoardCat = MCM.add(“Keyboard Cat”, “Musical Lols”);

/* List down all the Cat Memes */

/* Method to READ all the Cat Memes */
 public void listCats( ){
 Session session = factory.openSession();
 Transaction tx = null;
 tx = session.beginTransaction();
 List catMemes = session.createQuery(“FROM CATS”).list(); 
 for (Iterator iterator = 
 catMemes.iterator(); iterator.hasNext();){
 CatMemes catMemes= (CatMemes) iterator.next(); 
 System.out.println(“Name: “ + catMemes.getName()); 
 System.out.println(“Lols: “ + catMemes.getLols()); ()); 
 }catch (HibernateException e) {
 if (tx!=null) tx.rollback();
 }finally {
And there we have it our project is complete now the cat memes shall be listed and in our database. Though I haven’t tested this right now I will and give an update when I do and if I doesn’t work will tell you why and how to fix it. Until then you can download Hibernate ORM here and the tutorial I got a lot of information and code from was here. Again thanks for reading.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.