Get started with building hibernate using maven (1), mavenhibernate

Source: Internet
Author: User

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) { = date;}public void setId(int 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"  "" >

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"        "">

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.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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.