- 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 { -
privateLong id; -
privateString title; -
privateDate date; -
publicEvent() {} -
publicLong getId() { -
returnid; -
} -
privatevoid setId(Long id) { -
//这里要注意,这个set方法是private的, -
//目的是不让普通的程序员使用它, -
//但是hibernate框架还是可以通过反射机制访问私有属性id -
this.id= id; -
} -
publicDate getDate() { -
returndate; -
} -
publicvoid setDate(Date date) { -
this.date= date; -
} -
publicString getTitle() { -
returntitle; -
} -
publicvoid setTitle(String title) { -
this.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"> -
<classname="Event" table="EVENTS"> -
<idname="id" column="EVENT_ID"> -
<generatorclass="native" /> -
</id> -
<propertyname="date" type="timestamp" column="EVENT_DATE" /> -
<propertyname="title" /> -
</class> - </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>
-
<session-factory> -
<!--数据库连接设置,根据具体情况来,特别是用户名和密码 --> -
<propertyname="connection.driver_class">com.mysql.jdbc.Driver</property> -
<propertyname="connection.url">jdbc:mysql://localhost:3306/hbtest</property> -
<propertyname="connection.username">root</property> -
<propertyname="connection.password">123456</property> -
<!--JDBC连接池(内置的) --> -
<propertyname="connection.pool_size">1</property> -
<!--SQL语句的方言 --> -
<propertyname="dialect">org.hibernate.dialect.HSQLDialect</property> -
<!--Enable Hibernate's automatic session context management --> -
<propertyname="current_session_context_class">thread</property> -
<!--Disable the second-level cache --> -
<propertyname="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> -
<!--Echo all executed SQL to stdout --> -
<propertyname="show_sql">true</property> -
<!--Drop and re-create the database schema on startup --> -
<propertyname="hbm2ddl.auto">create</property> -
<mappingresource="mapping/Event.hbm.xml"/> -
</session-factory> - </hibernate-configuration>
- package
util; - import
org.hibernate.*; - import
org.hibernate.cfg.*; - public
class HibernateUtil { -
privatestatic final SessionFactory sessionFactory; -
static{ -
try{ -
//Create the SessionFactory from hibernate.cfg.xml -
newsessionFactory = Configuration().configure().buildSessionFactory(); -
catch} (Throwable ex) { -
//Make sure you log the exception, as it might be swallowed -
"InitialSystem.err.println( SessionFactory creation failed." + ex); -
thrownew ExceptionInInitializerEr ror(ex); -
} -
} -
publicstatic SessionFactory getSessionFactory() { -
returnsessionFactory; -
} - }
- package
events; -
import
org.hibernate.Session; -
import
java.util.Date; -
import
util.HibernateUtil; -
import
org.hibernate.tutorial.domain.Event; -
public
class EventManager { -
publicstatic void main(String[] args) { -
newEventManager mgr = EventManager(); -
"Mymgr.createAndStoreEvent( Event" ,new Date()); -
HibernateUtil.getSessionFactory().close(); -
} -
privatevoid createAndStoreEvent(String title, Date theDate) { -
Session session = HibernateUtil.getSessionFactory().getCurrentSession(); -
session.beginTransaction(); -
//建立一个Event对象并进行赋值 -
newEvent theEvent = Event(); -
theEvent.setTitle(title); -
theEvent.setDate(theDate); -
//将该对象写入数据库 -
session.save(theEvent); -
session.getTransaction().commit(); -
} - }