您的位置:首页 > 其它

笔记 01_传智播客hibernate教程_hibernate介绍与动手入门体验

2009-07-01 22:08 671 查看
hibernate 解决的问题
模型不匹配(阻抗不匹配):对象模型和关模型的.
对象模型:继承,多态,关联。
关系模型:主键,外键。
解决方案:
1 jdbc手动转换
2 ORM (Object relation Mapping) ,hibernate只是其中一种。还有OJB,TopLink

快速入门步骤
1 建立domain对象 (ORM 中的 O)
package ntt.bhb.xxl.one.domains;
import java.util.Date;
public class User {
private Integer id;
private String name;
private Date birthday;
省去 set get 方法
}
2 导入jar包
除了 源码中的的jar 包外,还要导入mysql-connector-java-5.0.4-bin.jar
我用的是 hibernate 3.3.2 所以还要配置日志
现在的Hibernate使用了SLF4J作为日志机制。在运行时,需要进行动态的配置日志。
现在使用比较多的是Log4j,此时需要进入H_HOME,将H_HOME/project/etc下面的log4j.properties拷贝到 Hibernate工程的src目录下,在需要的情况下,可以对log4j.properties进行自定义的配置。
但是仅仅拷贝改为见还是不可以的,必须将以下两个类库拷贝到类路径中:
否则就会在控制台打印如下错误:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".(缺少slf4j-log4j12-1.5.2.jar)
Initial SessionFactory creation failed.java.lang.NoClassDefFoundError: org/apache/log4j/Level(缺少log4j-1.2.12.jar

3 配置映射文件 (ORM 中的 R和 M)
User.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 package="ntt.bhb.xxl.one.domains">
<class name="User">
<id name="id">
<generator class="native" />
</id>
<property name="name" />
<property name="birthday" />
</class>
</hibernate-mapping>

(可以利用源代码提供的例子,参考这些配置文件的写法 例如hibernate-distribution-3.3.2.GA-dist/hibernate-distribution-3.3.2.GA/project/tutorials/eg/src/main/resources/org/hibernate/auction 目录下 就有一个User.hbm.xml)

上面表示成黄色的内容保持一致

将domain对象中的属性和数据库表的字段 的映射关系,生成映射文件

4 配置文件 (管理 映射文件(hbm.xml))
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///myhibernate</property>
<!--mysql url是默认值,不改变的话,可以写成 jdbc:mysql:///db name的形式 -->
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!--因为 hibernate 要支持主流的数据库,数据之间存在差异,所以要指定方言-->
<property name="hbm2ddl.auto">create</property>
<!--
#hibernate.hbm2ddl.auto create-drop 測試
#hibernate.hbm2ddl.auto create 测试
#hibernate.hbm2ddl.auto update 升級
#hibernate.hbm2ddl.auto validate 運行
-->
<property name="hibernate.show_sql">true</property>
<mapping resource="ntt/bhb/xxl/one/domains/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
<!--
以上属相的设置值,可以参考hibernate.properties
(路径bernate-distribution-3.3.2.GA-dist/hibernate-distribution-3.3.2.GA/project/etc)
-->

5 编写测试代码
package ntt.bhb.xxl.one.test;

import java.util.Date;

import ntt.bhb.xxl.one.domains.User;

import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;

public class Base {

public static void main(String[] args) {

Configuration cfg = new Configuration();
cfg.configure();
SessionFactory sessionFactory = cfg.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();

User user = new User();
user.setBirthday(new Date());
user.setName("sbbaohongbin");

session.save(user);
transaction.commit();
session.close();

}
}
再调试的过程中总结一下几点
1 hibernate.cfg.xml 的hibernate.connection.url属性指定数据库
2 User.hbm.xml 文件指定表名 class name以及字段名和属性 property
<?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="ntt.bhb.xxl.one.domains">
<class name="User">
<id name="id">
<generator class="native" />
</id>
<property name="name" />
<property name="birthday" />
</class>
</hibernate-mapping>
3 因为 hibernate.cfg.xml 中
<property name="hbm2ddl.auto">create</property>
所以 会根据 User.hbm.xml的内容生成配置中设置的表。
4 User.hbm.xml中的 property name 值 必须是domain对象的属性的子集,否则会报出
Exception in thread "main" org.hibernate.InvalidMappingException:
Could not parse mapping document from resource ntt/bhb/xxl/one/domains/User.hbm.xml

例如: <property name="birthday" /> 改成 <property name="birthday222" />
birthday222 是domain对像里没有的属性(不是子集)

还有一点值得留意,事务的提交是和表的引擎相关的
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: