Get started with building hibernate using maven (1), mavenhibernate
1: Create a maven project and introduce the dependencies required by hibernate.
<Dependency> <groupId> junit </groupId> <artifactId> junit </artifactId> <version> 3.8.1 </version> <scope> test </scope> </dependency> <dependency> <groupId> org. hibernate </groupId> <artifactId> hibernate-core </artifactId> <version> 3.6.5.Final </version> </dependency> <groupId> org. slf4j </groupId> <artifactId> slf4j-api </artifactId> <version> 1.6.1 </version> </dependency> <groupId> mysql </groupId> <artif ActId> mysql-connector-java </artifactId> <version> 5.1.6 </version> </dependency> <groupId> org. slf4j </groupId> <artifactId> slf4j-nop </artifactId> <version> 1.6.4 </version> </dependency> <! -- Add a dependsist --> <dependency> <groupId> dependsist </groupId> <artifactId> javassist </artifactId> <version> 3.11.0.GA </version> </dependency> <groupId> log4j </groupId> <artifactId> log4j </artifactId> <version> 1.2.16 </version> </dependency>
2: persistence of a class
package com.nerd.entity;import java.util.Date;public class Event {private int id;private Date date;private String title;public Date getDate() {return date;}public int getId() {return id;}public String getTitle() {return title;}public void setDate(Date date) {this.date = date;}public void setId(int id) {this.id = id;}public void setTitle(String title) {this.title = title;}}
3: How does hibernate store and load persistent class objects? In this case, we need to create a ing file (the file and the object class are placed under the same directory)
<?xml version="1.0" encoding='UTF-8'?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
4: hibernate configuration (under the resource Directory)
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
5. startup and helper classes
package com.nerd.util;import org.apache.log4j.Logger;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;public class HibernateUtil {private static final SessionFactory sessionFactory;private static final Logger LOGGER = Logger.getLogger(HibernateUtil.class);static{try {LOGGER.debug("HibernateUti.static - loading cofig");sessionFactory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();LOGGER.debug("HibernateUtil.static - end");} catch (Throwable ex) {LOGGER.error("HibernateUti error : ExceptionInInitializerError");throw new ExceptionInInitializerError(ex);}}public static SessionFactory getsSessionFactory(){return sessionFactory;}}
package com.nerd.test;import java.util.Date;import java.util.List;import org.hibernate.classic.Session;import com.nerd.entity.Event;import com.nerd.util.HibernateUtil;public class EventManger {public static void main(String[] args) {EventManger mgr = new EventManger();if(args[0].equals("insert")){mgr.createAndStoreEvent("my event", new Date());}else if(args[0].equals("list")){@SuppressWarnings("rawtypes")List list = mgr.listEvents();for(int i=0;i<list.size();i++){Event theEvent = (Event)list.get(i);System.out.println("Event "+theEvent.getTitle()+"date "+theEvent.getDate());}}HibernateUtil.getsSessionFactory().close();}private void createAndStoreEvent(String title,Date date){Session session = HibernateUtil.getsSessionFactory().getCurrentSession();session.beginTransaction();Event event = new Event();event.setDate(date);event.setTitle(title);session.save(event);session.getTransaction().commit();}@SuppressWarnings("rawtypes")private List listEvents(){Session session = HibernateUtil.getsSessionFactory().getCurrentSession();session.beginTransaction();List list = session.createQuery("from Event").list();session.getTransaction().commit();return list;}}
Such a simple hibernate instance is complete.