hibernate 第一篇

好記性不如爛筆頭,今天照着視頻敲出第一個hibernate程序。

首先,date屬性不能是string型的,要是Date類的,import的包不能是java.util.Date而應該是java.sql.Date,id屬性是Integer,改過類型的屬性在所有文件裏都要一一更改參數類型;

hbm(hibernate mapping)裏面如果id屬性設置成了generator class="native",那數據庫裏面id要設置成自動遞增;

測試類test裏面import的包應選org.hibernate開頭的,比如

import static org.junit.Assert.*;

import java.sql.Date;

import org.hibernate.cfg.Configuration;

import org.hibernate.service.ServiceRegistry;

import org.hibernate.service.ServiceRegistryBuilder;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.Transaction;

import org.junit.Test;

不能完全按書上抄,書上的跟不上更新,//sessionFactory=configuration.buildSessionFactory();這個方法只在hibernate4.0以下有效,4.0以上應該用

ServiceRegistry serviceRegistry=new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();

sessionFactory=configuration.buildSessionFactory(serviceRegistry);

數據庫中必須有表已經定義好,因爲最後Juint運行出來是一條insert語句。

發佈了30 篇原創文章 · 獲贊 24 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章