hibernate版本:3.3.2
文件目录:
*.hbm.xml配置文件:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.zzl.Event" table="EVENTS">
<id name="id" column="EVENT_ID">
<generator class="native"/>
</id>
<property name="date" type="timestamp" column="EVENT_DATE"/>
<property name="title"/>
</class>
</hibernate-mapping>
hibernate.cfg.xml配置文件
<?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>
<!-- Database connection settings -->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@ip:unieap</property>
<property name="connection.username">tabuser</property>
<property name="connection.password">tabuser</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<!--
<property name="hbm2ddl.auto">create</property>
-->
<mapping resource="com/zzl/Event.hbm.xml"/>
</session-factory>
</hibernate-configuration>
查找方言超链接:http://kb.cnblogs.com/a/1587624/
实体类:
package com.zzl;
import java.util.Date;
public class Event
{
private Long id;
private String title;
private Date date;
public Event(){}
public Long getId()
{
return id;
}
private void setId(Long id)
{
this.id = id;
}
public String getTitle()
{
return title;
}
public void setTitle(String title)
{
this.title = title;
}
public Date getDate()
{
return date;
}
public void setDate(Date date)
{
this.date = date;
}
}
辅助类:
package com.util;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil
{
private static final SessionFactory sessionFactory;
static {
try {
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
主类:
package com.app;
import java.util.Date;
import org.hibernate.Session;
import com.util.HibernateUtil;
import com.zzl.Event;
public class App
{
public static void main(String[] args)
{
Session session=HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Event event=new Event();
event.setTitle("zzl");
event.setDate(new Date());
session.save(event);
session.getTransaction().commit();
HibernateUtil.getSessionFactory().close();
}
}