- create
table EVENTS ( - EVENT_ID
int(6) AUTO_INCREMENT, - EVENT_DATE
date, - TITLE
varchar( 20), - primary
key (EVENT_ID));
- package
org.hibernate.tutorial.domain; - //上面的这个包路径比较长,因为原例子中就是这样,我没有修改
- import
java.util.Date; - public
class Event { -
Long id; -
String title; -
Date date; -
Event() {} -
Long getId() { -
id; -
} -
void setId(Long id) { -
-
-
-
= id; -
} -
Date getDate() { -
date; -
} -
void setDate(Date date) { -
= date; -
} -
String getTitle() { -
title; -
} -
void setTitle(String title) { -
= title; -
} - }
- <?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"> - <hibernate-mapping
package="org.hibernate.tutorial.domain"> -
name="Event" table="EVENTS"> -
name="id" column="EVENT_ID"> -
class="native" /> -
-
name="date" type="timestamp" column="EVENT_DATE" /> -
name="title" /> -
- </hibernate-mapping>
- <?xml
version='1.0' encoding='utf-8'?> - <!DOCTYPE
hibernate-configuration PUBLIC -
"-//Hibernate/Hibernate Configuration DTD 3.0//EN" -
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> -
- <hibernate-configuration>
-
-
数据库连接设置,根据具体情况来,特别是用户名和密码 --> -
name="connection.driver_class">com.mysql.jdbc.Driver</property> -
name="connection.url">jdbc:mysql://localhost:3306/hbtest</property> -
name="connection.username">root</property> -
name="connection.password">123456</property> -
JDBC连接池(内置的) --> -
name="connection.pool_size">1</property> -
SQL语句的方言 --> -
name="dialect">org.hibernate.dialect.HSQLDialect</property> -
Enable Hibernate's automatic session context management --> -
name="current_session_context_class">thread</property> -
Disable the second-level cache --> -
name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> -
Echo all executed SQL to stdout --> -
name="show_sql">true</property> -
Drop and re-create the database schema on startup --> -
name="hbm2ddl.auto">create</property> -
resource="mapping/Event.hbm.xml"/> -
- </hibernate-configuration>
- package
util; - import
org.hibernate.*; - import
org.hibernate.cfg.*; - public
class HibernateUtil { -
static final SessionFactory sessionFactory; -
{ -
{ -
Create the SessionFactory from hibernate.cfg.xml -
sessionFactory = Configuration().configure().buildSessionFactory(); -
} (Throwable ex) { -
Make sure you log the exception, as it might be swallowed -
System.err.println( SessionFactory creation failed." + ex); -
new ExceptionInInitializerEr ror(ex); -
} -
} -
static SessionFactory getSessionFactory() { -
sessionFactory; -
} - }
- package
events; -
import
org.hibernate.Session; -
import
java.util.Date; -
import
util.HibernateUtil; -
import
org.hibernate.tutorial.domain.Event; -
public
class EventManager { -
static void main(String[] args) { -
EventManager mgr = EventManager(); -
mgr.createAndStoreEvent( Event" ,new Date()); -
HibernateUtil.getSessionFactory().close(); -
} -
void createAndStoreEvent(String title, Date theDate) { -
Session session = HibernateUtil.getSessionFactory().getCurrentSession(); -
session.beginTransaction(); -
-
Event theEvent = Event(); -
theEvent.setTitle(title); -
theEvent.setDate(theDate); -
-
session.save(theEvent); -
session.getTransaction().commit(); -
} - }