---
title: "Hibernate配置和简单例子"
author: "Jameslyy"
date: "2007-09-28T16:50:33Z"
tags: ["HSQLDB", "Hibernate", "JDBC", "MySQL", "XML"]
slug: "hibernate"
draft: false
lastmod: "2007-09-28T16:50:33Z"
id: 127958
url: "https://jamesblog.iteye.com/blog/127958"
---

java 代码

01. import java.util.Date;
02. public class Event {
03. private Long id;
04. private String title;
05. private Date date;
06. public Event() {
07. }
08. public Long getId() {
09. return id;
10. }
11. public void setId(Long id) {
12. this.id = id;
13. }
14. public String getTitle() {
15. return title;
16. }
17. public void setTitle(String title) {
18. this.title = title;
19. }
20. public Date getDate() {
21. return date;
22. }
23. public void setDate(Date date) {
24. this.date = date;
25. }
26. }

**Event.hbm.xml**

xml 代码

**&lt;?xml version="1.0"?&gt;       &lt;!DOCTYPE hibernate-mapping PUBLIC                "-//Hibernate/Hibernate Mapping DTD 3.0//EN"                "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&gt;              &lt;hibernate-mapping&gt;           &lt;class name="com.james.business.modle.domain.Event" table="EVENTS"&gt;               &lt;id name="id" column="id"&gt;                   &lt;generator class="native" /&gt;               &lt;/id&gt;               &lt;property name="date" type="timestamp" column="datet" /&gt;               &lt;property name="title" /&gt;           &lt;/class&gt;       &lt;/hibernate-mapping&gt;**   

 

**java 代码**

**import org.hibernate.SessionFactory;    import org.hibernate.cfg.Configuration;       public class HibernateUtil {        private static final SessionFactory sessionFactory;           static {            try {                // Create the SessionFactory from hibernate.cfg.xml                sessionFactory = new Configuration().configure()                        .buildSessionFactory();            } catch (Throwable ex) {                // Make sure you log the exception, as it might be swallowed                System.err.println("Initial SessionFactory creation failed." + ex);                throw new ExceptionInInitializerError(ex);            }        }           public static SessionFactory getSessionFactory() {            return sessionFactory;        }       }**  

**java 代码**

**import java.util.Date;       import org.hibernate.Session;       import com.james.business.modle.domain.Event;       public class EventManager {        public static void main(String\[] args) {            EventManager mgr = new EventManager();               mgr.createAndStoreEvent("My Event", new Date());               HibernateUtil.getSessionFactory().close();        }           private void createAndStoreEvent(String title, Date theDate) {               Session session = HibernateUtil.getSessionFactory().getCurrentSession();               session.beginTransaction();               Event theEvent = new Event();            theEvent.setTitle(title);            theEvent.setDate(theDate);               session.save(theEvent);               session.getTransaction().commit();        }       }**  

**hibernate.cfg.xm**

**xml 代码**

**&lt;?xml version='1.0' encoding='utf-8'?&gt;       &lt;!DOCTYPE hibernate-configuration PUBLIC                "-//Hibernate/Hibernate Configuration DTD 3.0//EN"                "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt;              &lt;hibernate-configuration&gt;                  &lt;session-factory&gt;                      &lt;!-- Database connection settings --&gt;               &lt;!-- org.hsqldb.jdbcDriver --&gt;               &lt;property name="connection.driver\_class"&gt;                   com.mysql.jdbc.Driver                &lt;/property&gt;               &lt;!-- jdbc:hsqldb:hsql://localhost --&gt;               &lt;!-- ?useUnicode=true&amp;characterEncoding=utf-8&amp;autoReconnect=true --&gt;               &lt;property name="connection.url"&gt;                   jdbc:mysql://10.1.1.2:3306/test                &lt;/property&gt;               &lt;property name="connection.username"&gt;test&lt;/property&gt;               &lt;property name="connection.password*"&gt;*test*&lt;/*property&gt;                      &lt;!-- JDBC connection pool (use the built-in) --&gt;               &lt;property name="connection.pool\_size"&gt;1&lt;/property&gt;                      &lt;!-- SQL dialect --&gt;               &lt;!-- org.hibernate.dialect.HSQLDialect --&gt;               &lt;property name="dialect"&gt;                   org.hibernate.dialect.MySQL5InnoDBDialect                &lt;/property&gt;                      &lt;!-- Enable Hibernate's automatic session context management --&gt;               &lt;property name="current\_session\_context\_class"&gt;thread&lt;/property&gt;                      &lt;!-- Disable the second-level cache  --&gt;               &lt;property name="cache.provider\_class"&gt;                   org.hibernate.cache.NoCacheProvider                &lt;/property&gt;                      &lt;!-- Echo all executed SQL to stdout --&gt;               &lt;property name="show\_sql"&gt;true&lt;/property&gt;                      &lt;!-- Drop and re-create the database schema on startup --&gt;               &lt;property name="hbm2ddl.auto"&gt;create&lt;/property&gt;                      &lt;mapping resource="com/james/business/modle/domain/Event.hbm.xml" /&gt;                  &lt;/session-factory&gt;              &lt;/hibernate-configuration&gt;**