成熟丰满熟妇高潮XXXXX,人妻无码AV中文系列久久兔费 ,国产精品一国产精品,国精品午夜福利视频不卡麻豆

您好,歡迎來到九壹網(wǎng)。
搜索
您的當(dāng)前位置:首頁Hibernate核心API

Hibernate核心API

來源:九壹網(wǎng)

五、核心API Configuration A) AnnotationConfiguration B) 進(jìn)行配置信息的管理 C) 用來產(chǎn)生SessionFactory D) 可以在configure方法中指定hibernate配置文件 E) 只需要關(guān)注一個方法:buildSessionFactory() 1、configure()方法【本文來自鴻網(wǎng)互聯(lián) (http://ww

五、核心API

Configuration

A) AnnotationConfiguration

B) 進(jìn)行配置信息的管理

C) 用來產(chǎn)生SessionFactory

D) 可以在configure方法中指定hibernate配置文件

E) 只需要關(guān)注一個方法:buildSessionFactory()

1、configure()方法【本文來自鴻網(wǎng)互聯(lián) (http://www.68idc.cn)】有一個重載的方法configure(String str),用于指定配置文件的路徑。

2、SessionFactory可以用于產(chǎn)生session,如調(diào)用其getCurrentSession()方法

3、SessionFactory需要注意2個方法:openSession()和getCurrentSession()

openSession()永遠(yuǎn)是用于打開新的session,getCurrentSession()是如果當(dāng)前環(huán)境有sesion,則使用當(dāng)前上下文的session,如果沒有,則會打開新的session,但是session一旦提交,再次拿的就是,就是新的session。openSession()需要手動close,getOpenSession()是在事務(wù)提交的時候,自動close.

所謂上下文的session,是在hibernate.cfg.xml中,配置的thread。其取值有如下:jta|thread|managed|custom.Class

getCurrentSession()的作用:界定事務(wù)邊界

Hibernate中對象的3種狀態(tài):Transient,Persistence,Detached

三種狀態(tài)的關(guān)鍵區(qū)分:

A.有沒有ID

B.ID在數(shù)據(jù)庫中有沒有

C.在內(nèi)存中有沒有

三種狀態(tài):

A、Trainsient:內(nèi)存中一個對象,沒有ID,緩存中沒有

B、Persistent:內(nèi)存中有,緩存中有,數(shù)據(jù)庫中有(ID)

C、Detached:內(nèi)存有,緩存沒有,數(shù)據(jù)庫有

Session:

管理一個數(shù)據(jù)庫的任務(wù)單元。主要方法有:

1)save()

save()方法執(zhí)行前,對象是Transient狀態(tài),save()方法執(zhí)行后,對象變?yōu)镻ersistent狀態(tài),Session對象執(zhí)行完commit()方法之后,對象變?yōu)镈etached狀態(tài)。

2)delete()

delete()方法可以刪除Persistent狀態(tài)和Detached狀態(tài)的對象,不能刪除Transient

3)Update

A)用來更新detached狀態(tài)對象,更新完成后轉(zhuǎn)為persistent狀態(tài)對象

B)更新transient狀態(tài)對象會報錯

C)更新自己設(shè)定id的transient狀態(tài)對象不會出錯(數(shù)據(jù)庫中必須先有對應(yīng)的記錄)

D)更新部分更改的字段(當(dāng)更新persistent狀態(tài)的對象時,會發(fā)現(xiàn)sql語句中,更新了所有的字段,如果只想更新做了修改的字段,可以使用如下方式)

a)在對應(yīng)的get()方法上加@Column(updatable=false)或者在xml文件標(biāo)簽中增加update=”false”

b)在xml中,在標(biāo)簽中增加如下dynamic-update="true"

c)跨session時想要實現(xiàn)部分字段更新,,可以使用Session的merge()方法

d)使用HQL(EJBQL)語句

4)saveOrUpdate()

根據(jù)具體情況使用save()或者update()方法

5)load()

6)get()

7)get()和load()的區(qū)別

兩者都可以將一條數(shù)據(jù)從數(shù)據(jù)庫中讀出來,轉(zhuǎn)化成一個對象。

兩者的區(qū)別在于:

A)load()方法返回的是代理對象,等到真正用到對象的內(nèi)容時才才發(fā)出sql語句

B)get()方法直接從數(shù)據(jù)庫中加載,不會存在延遲。

C)不存在對應(yīng)記錄的時候,兩者表現(xiàn)不同。原因:兩者一個會發(fā)出sql語句,一個不會發(fā)出sql語句。

8)clear()

無論是get()或load()方法,都會首先查找緩存(一級緩存),如果沒有,才會去數(shù)據(jù)庫中查找,如果已經(jīng)存在,則不會去數(shù)據(jù)庫中查找clear()方法可以強(qiáng)制清除session緩存

9)flush()

強(qiáng)制讓緩存中的數(shù)據(jù)和數(shù)據(jù)庫中的數(shù)據(jù)做同步

小實驗:

package com.zgy.hibernate.model;







import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.AnnotationConfiguration;

import org.hibernate.tool.hbm2ddl.SchemaExport;



import org.junit.AfterClass;

import org.junit.BeforeClass;

import org.junit.Test;



public class HibernateCoreAPITest {



public static SessionFactory sf = null;

@BeforeClass

public static void beforeClass(){

sf = new AnnotationConfiguration().configure().buildSessionFactory();

}

@Test

public void testSave() {

//teacher是Transient狀態(tài)

Teacher teacher = new Teacher();

teacher.setName("張三");

teacher.setAddress("北京");



Session session = sf.getCurrentSession();

session.beginTransaction();



//teacher是Persistent狀態(tài)

session.save(teacher);



//teacher是Detached狀態(tài)

session.getTransaction().commit();



}

/*



@Test

public void testDelete() {

//teacher是Transient狀態(tài)

Teacher teacher = new Teacher();

teacher.setName("李四");



Session session = sf.getCurrentSession();

session.beginTransaction();

//這里進(jìn)行刪除,是刪除不掉的,因為此時對象沒有ID



session.save(teacher);

//執(zhí)行完save()方法后,teacher是Persistent狀態(tài),這時可以刪除對象





session.getTransaction().commit();

//執(zhí)行完commit()后,teacher是Detached狀態(tài),這里刪除teacher對象也是可以的

Session session2 = sf.getCurrentSession();

session2.beginTransaction();

session2.delete(teacher);

session2.getTransaction().commit();



}

*/

@Test

public void testLoad() {

Session session = sf.getCurrentSession();

session.beginTransaction();

Teacher t = (Teacher)session.load(Teacher.class, 1);

System.out.println(t.getClass());

session.getTransaction().commit();

//System.out.println(t.getName());

}



@Test

public void testGet() {

Session session = sf.getCurrentSession();

session.beginTransaction();

Teacher t = (Teacher)session.get(Teacher.class, 1);

System.out.println(t.getClass());

session.getTransaction().commit();

//System.out.println(t.getName());

}



@Test

public void testUpdate1() {

Session session = sf.getCurrentSession();

session.beginTransaction();

Teacher t = (Teacher)session.get(Teacher.class, 1);

session.getTransaction().commit();



t.setName("張小三");



Session session2 = sf.getCurrentSession();

session2.beginTransaction();

session2.update(t);

session2.getTransaction().commit();

}



@Test

public void testUpdate2() {

Teacher t = new Teacher();

t.setName("張小三");

t.setId(1);

Session session2 = sf.getCurrentSession();

session2.beginTransaction();

session2.update(t);

session2.getTransaction().commit();

}



@Test

public void testUpdate3() {



Session session = sf.getCurrentSession();

session.beginTransaction();

Teacher t = (Teacher)session.get(Teacher.class, 1);

t.setName("張三三");

session.getTransaction().commit();

}



@Test

public void testSaveOrUppdate() {

Teacher t = new Teacher();

t.setName("李四");

t.setAddress("天津");

Session session = sf.getCurrentSession();

session.beginTransaction();

session.saveOrUpdate(t);

session.getTransaction().commit();



t.setName("李四四");

Session session2 = sf.getCurrentSession();

session2.beginTransaction();

session2.saveOrUpdate(t);

session2.getTransaction().commit();



}





@Test

public void testClear() {

Session session = sf.getCurrentSession();

session.beginTransaction();

Teacher t = (Teacher)session.load(Teacher.class, 1);

System.out.println(t.getName());

//使用clear()方法清楚緩存

session.clear();



Teacher t2 = (Teacher)session.load(Teacher.class, 1);

System.out.println(t2.getName());

session.getTransaction().commit();

System.out.println(t.getName());

}



@Test

public void testFlush() {

Session session = sf.getCurrentSession();

session.beginTransaction();

Teacher t = (Teacher)session.load(Teacher.class, 1);

t.setName("ttt");

//加入flush()方法后,會導(dǎo)致兩次update操作,如果不加入,則在下面的場景下,只會有一次update

session.flush();

t.setName("ttttt");

session.getTransaction().commit();

}



@AfterClass

public static void afterClass(){

sf.close();

}

}

Copyright ? 2019- 91gzw.com 版權(quán)所有 湘ICP備2023023988號-2

違法及侵權(quán)請聯(lián)系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市萬商天勤律師事務(wù)所王興未律師提供法律服務(wù)