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對像裏沒有的屬性(不是子集)
還有一點值得留意,事務的提交是和表的引擎相關的