-
主鍵生產策略: increment //適用于代理主鍵,由hibernate以遞增方式生成 identity //適用于代理主鍵,由底層數據庫生成標識符 sequence //適用于代理主鍵,hibernate根據底層數據庫的序列生成標識符(要求底層數據庫支持序列) hilo //適用于代理主鍵,hibernate分局hight/low算法生成標識符 seqhilo //適用于代理主鍵,使用一個高/低位算法來高效生成long,short或者int類型的標識符 native //適用于代理主鍵,根據底層數據庫對自動生成的標識的方式,自動選擇identity,sequence或者hilo uuid.hex //適用于代理主鍵,hibernate采用128位的UUID算法生成標識符 uuid.string //適用于代理主鍵,UUID被編碼成16位字符長的字符串 assigned //適用于代理主鍵,由java程序負責生成標識符 foreign //適用于代理主鍵,使用另一個向關聯的對象的標識符查看全部
-
openSession與getCurrentSession的區別: (1)getCurrentSession在事務提交或者回滾之后會自動關閉,而openSesssion需要你手動關閉。如果使用openSession而沒有手動關閉,多次之后會導致連接池溢出! (2)openSession每次創建新的session對象,getCurrentSession使用現有的session對象 PS: 1.openSession 每次使用都是打開一個新的session,使用完需要調用close方法關閉session; 2.getCurrentSession 是獲取當前session對象,連續使用多次時,得到的session都是同一個對象,這就是與openSession的區別之一 ; 一般在實際開發中,往往使用getCurrentSession多,因為一般是處理同一個事務,所以在一般情況下比較少使用openSession;查看全部
-
如何獲得session對象? (1)openSessionion (2)getCurrentSession 如果使用getCurrentSession需要在hibernate.cfg.xml文件中進行配置: 如果是本地事務(jdbc事務) <property name="hibernate.current_session_context_class">thread</property> 如果是全局事務(jta事務) <property name="hibernate.current_session_context_class">jta</property>查看全部
-
hibernate對數據的操作都是封裝在事務中,并且默認是非自動提交的昂視,所以用session保存對象時,如果不開啟事務,并且手工提交事務,對象并不會真正保存在數據庫中。 如果想讓hibernate想jdbc那樣自動提交事務,必須調用session對象的doWork()方法,獲得jdbc的connection后,設置其為自動提交事務模式(注意:通常并不推薦這樣做) 使用doWork()方法自動提交事務時,session應該調用flush()方法查看全部
-
hibernate執行流程: 1創建Configuration 用來讀取Hibernate.cfg.xml配置文件 2通過Configuration創建SessionFactory 用來讀取User.hbm.xml文件 3通過SessionFactory創建Session 進行save delete update get 以及復雜的查詢操作 執行session時 需要將它封裝在事務Transaction中 結束之前要進行事務的提交commit session的關閉session.close()查看全部
-
通過Hibernate API編寫訪問數據庫 //創建配置對象 Configuration config=new Configuration().configure(); //創建服務注冊對象 ServiceRegistry serviceRegistry=new ServiceRegistryBuilder().applySettings(config.getProperties).buildServiceRegistry(); //創建會話工廠 sessionFactory=config.buildSessionFactory(serviceRegistry); //打開會話 session=sessionFactory.openSession(); //打開事務 transaction=session.beginTransaction(); 在destroy()方法內 transaction.commit();//提交事務 session.close();//關閉會話 sessionFactory.close();//關閉會話工廠查看全部
-
寫SQL的不便之處查看全部
-
1.ORM是一種面向對象編程的方法,用這種方法來避免寫數據庫底層語言sql語句,這樣有利于java的跨平臺,擴展。維護。而hiberenate是ORM的一種框架 2.hirbernate開發基本步驟: 編寫配置文檔hirbernate.cfg.xml文檔 編寫實體類 生成對應尸體類的映射文件并添加到配置文檔中 調用hirbernate api進行測試 3.什么是seesion hirbernate操作數據庫都要用session,調用session.api方法來操作 openssion()和getCurrentSession()區別: openssion每次都會創建一個新的對象,用完后要調用session.close()將其關閉,否則多次之后會出現溢出的現象。 而getCurrentSession是單例模式,每次創建的都是同一個對象,使用完后自動會銷毀。 4.單表操作方法 save delete update get load get()和load()區別: get獲取后馬上發送sql語句,返回一個實體 load獲取后進行緩存,調用都發送 返回一個實體代理對象查看全部
-
Hibernate單表操作 1.單一主鍵 <generator class="assigned|native" />//assigned為手工賦值方式,native為自動增長 2.基本類型 3.對象類型 4.組件屬性 <component name="address" class="hibernate001.Address"> <property name="postcode" column="POSTCODE"></property> <property name="phone" column="PHONE"></property> <property name="address" column="ADDRESS"></property> </component> 5.單表操作CRUD實例 (1)save (2)update (3)delete (4)get/load(查詢單個記錄) get與load區別: 1.get()調用后立即發出sql語句,并返回持久化對象;load()使用對象時才發出sql語句,返回的是代理對象 2.當查詢到數據為空時,get返回null,load拋出objectNotFound異常查看全部
-
Hibernate單表操作 1.單一主鍵 2.基本類型 3.對象類型 4.組件屬性 5.單表操作CRUD實例查看全部
-
單表操作CRUD實例 (1)save (2)update (3)delete (4)get/load(查詢單個記錄) get與load區別: 1.get()調用后立即發出sql語句,并返回持久化對象;load()使用對象時才發出sql語句,返回的是代理對象 2.當查詢到數據為空時,get返回null,load拋出objectNotFound異常查看全部
-
組件屬性 <component name="address" class="hibernate001.Address"> <property name="postcode" column="POSTCODE"></property> <property name="phone" column="PHONE"></property> <property name="address" column="ADDRESS"></property> </component>查看全部
-
@Test public void saveImage() throws Exception{ Student s=new Student(1,"李四","男",new Date(),"北京"); File f=new File("d:"+File.separator+"boy.jpg"); InputStream input=new FileInputStream(f); Blob image=Hibernate.getLobCreator(session).createBlob(input, input.available()); s.setImage(image); session.save(s); } @Test public void readImage() throws Exception{ Student s=(Student) session.get(Student.class, 1); Blob image=s.getImage(); InputStream input=image.getBinaryStream(); File f=new File("d:"+File.separator+"girl.jpg"); OutputStream output=new FileOutputStream(f); byte[] buff=new byte[input.available()]; input.read(buff); output.write(buff); output.close(); input.close(); }查看全部
-
hibernate執行流程: 1創建Configuration 用來讀取Hibernate.cfg.xml配置文件 2通過Configuration創建SessionFactory 用來讀取User.hbm.xml文件 3通過SessionFactory創建Session 進行save delete update get 以及復雜的查詢操作 執行session時 需要將它封裝在事務Transaction中 結束之前要進行事務的提交commit session的關閉session.close()查看全部
-
對象類型查看全部
舉報
0/150
提交
取消