【Hibernate3.3复习知识点一】 - 配置hibernate环境(XML)。

官方在线参考文档:http://docs.jboss.org/hibernate/core/3.6/reference/zh-CN/html_single/

此复习笔记的对应文档:http://docs.jboss.org/hibernate/core/3.3/reference/zh-CN/html_single/  中间数字为版本信息。

 一、创建实体类

字段和数据库表中字段的一一对应。

Event.java

View Code
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 Date getDate() {
return date;
}

public void setDate(Date date) {
this.date = date;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}
}

 

二、创建映射文件(和实体类同包)

Event.hbm.xml

View Code
<?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 package="org.hibernate.tutorial.domain">

<class name="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
>


class name和table告诉hibernate实体类和表的对应关系。

id name和column是类属性和表字段的对应。 generator是主键的生成策略。

property配置的是其他的属性。  type指定映射的数据库字段类型。(如果在映射文件中没有设置 type 属性的话,Hibernate 会自己试着去确定正确的转换类型和它的映射类型。此例中date 属性,Hibernate 无法知道这个属性(java.util.Date 类型的)应该被映射成:SQL date,或 timestamp,还是 time 字段。在此例中,把这个属性映射成 timestamp 转换器,这样我们预留了日期和时间的全部信息。 )

 

数据库不区分大小写,所以当name和table相同时 table可以省略。同理 当name和column相同时 column可以省略

创建hibernate配置文件

hibernate.cfg.xml

View Code
<?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">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">1234</property>

<!-- JDBC connection pool (use the built-in) 连接池-->
<property name="connection.pool_size">1</property>

<!-- SQL dialect 方言-->
<property name="dialect">org.hibernate.dialect.MySQLDialect</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 控制台显示SQL语句-->
<property name="show_sql">true</property>

<!-- Drop and re-create the database schema on startup 生成建表语句HibernateMappingToDataDefinedLanguage-->
<property name="hbm2ddl.auto">update</property>

<!-- 将实体类映射成数据库字段 -->
<!-- 方法一 通过配置文件映射 -->
<mapping resource="com/bjsxt/hibernate/Event.hbm.xml"/>
<!-- 方法二 通过注解映射 (略 后面笔记记录)-->

</hibernate-configuration>

 

 创建启动辅助类

辅助类(helper class)来负责启动 Hibernate 和更方便地操作 org.hibernate.SessionFactory

HibernateUtil.java

View Code
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {

private static final SessionFactory sessionFactory = buildSessionFactory();

private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
return 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;
}

}

调用

View Code
import org.hibernate.Session;

import java.util.*;

import org.hibernate.tutorial.domain.Event;
import org.hibernate.tutorial.util.HibernateUtil;

public class EventManager {

public static void main(String[] args) {
 Event theEvent = new Event();
        theEvent.setTitle("My Event");
        theEvent.setDate(new Date());

Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();

session.save(theEvent);

session.getTransaction().commit();
}

}


通过save方法将实体对象的值存到数据库表中。

posted @ 2012-02-27 19:21  濤叔  阅读(1365)  评论(0编辑  收藏  举报