搜索
首页数据库mysql教程Hibernate核心API

Hibernate核心API

Jun 07, 2016 pm 04:13 PM
apiconfighibernate核心

五、核心API Configuration A) AnnotationConfiguration B) 进行配置信息的管理 C) 用来产生SessionFactory D) 可以在configure方法中指定hibernate配置文件 E) 只需要关注一个方法:buildSessionFactory() 1、configure()方法【本文来自鸿网互联 (http://ww

五、核心API

Configuration

A) AnnotationConfiguration

B) 进行配置信息的管理

C) 用来产生SessionFactory

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

E) 只需要关注一个方法:buildSessionFactory()

 

1、configure()方法【本文来自鸿网互联 (http://www.68idc.cn)】有一个重载的方法configure(String str),用于指定配置文件的路径。

2、SessionFactory可以用于产生session,如调用其getCurrentSession()方法

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

openSession()永远是用于打开新的session,getCurrentSession()是如果当前环境有sesion,则使用当前上下文的session,如果没有,则会打开新的session,但是session一旦提交,再次拿的就是,就是新的session。openSession()需要手动close,getOpenSession()是在事务提交的时候,自动close.

 

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

 

getCurrentSession()的作用:界定事务边界

 

 

 

 

Hibernate中对象的3种状态:Transient,Persistence,Detached

三种状态的关键区分:

A.有没有ID

B.ID在数据库中有没有

C.在内存中有没有

三种状态:

A、Trainsient:内存中一个对象,没有ID,缓存中没有

B、Persistent:内存中有,缓存中有,数据库中有(ID)

C、Detached:内存有,缓存没有,数据库有

 

 

Session:

管理一个数据库的任务单元。主要方法有:

1)save()

save()方法执行前,对象是Transient状态,save()方法执行后,对象变为Persistent状态,Session对象执行完commit()方法之后,对象变为Detached状态。

2)delete()

delete()方法可以删除Persistent状态和Detached状态的对象,不能删除Transient

3)Update

A)用来更新detached状态对象,更新完成后转为persistent状态对象

B)更新transient状态对象会报错

C)更新自己设定id的transient状态对象不会出错(数据库中必须先有对应的记录)

D)更新部分更改的字段(当更新persistent状态的对象时,会发现sql语句中,更新了所有的字段,如果只想更新做了修改的字段,可以使用如下方式)

a)在对应的get()方法上加@Column(updatable=false)或者在xml文件标签中增加update=”false”

b)在xml中,在标签中增加如下dynamic-update="true"

c)跨session时想要实现部分字段更新,,可以使用Session的merge()方法

d)使用HQL(EJBQL)语句

4)saveOrUpdate()

根据具体情况使用save()或者update()方法

5)load()

6)get()

7)get()和load()的区别

两者都可以将一条数据从数据库中读出来,转化成一个对象。

两者的区别在于:

A)load()方法返回的是代理对象,等到真正用到对象的内容时才才发出sql语句

B)get()方法直接从数据库中加载,不会存在延迟。

C)不存在对应记录的时候,两者表现不同。原因:两者一个会发出sql语句,一个不会发出sql语句。

8)clear()

无论是get()或load()方法,都会首先查找缓存(一级缓存),如果没有,才会去数据库中查找,如果已经存在,则不会去数据库中查找clear()方法可以强制清除session缓存

9)flush()

强制让缓存中的数据和数据库中的数据做同步

 

小实验:

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状态

Teacher teacher = new Teacher();

teacher.setName("张三");

teacher.setAddress("北京");



Session session = sf.getCurrentSession();

session.beginTransaction();



//teacher是Persistent状态

session.save(teacher);



//teacher是Detached状态

session.getTransaction().commit();



}

/*



@Test

public void testDelete() {

//teacher是Transient状态

Teacher teacher = new Teacher();

teacher.setName("李四");



Session session = sf.getCurrentSession();

session.beginTransaction();

//这里进行删除,是删除不掉的,因为此时对象没有ID



session.save(teacher);

//执行完save()方法后,teacher是Persistent状态,这时可以删除对象





session.getTransaction().commit();

//执行完commit()后,teacher是Detached状态,这里删除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()方法后,会导致两次update操作,如果不加入,则在下面的场景下,只会有一次update

session.flush();

t.setName("ttttt");

session.getTransaction().commit();

}



@AfterClass

public static void afterClass(){

sf.close();

}

}

 

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
mysql blob:有什么限制吗?mysql blob:有什么限制吗?May 08, 2025 am 12:22 AM

mysqlblobshavelimits:tinyblob(255bytes),blob(65,535 bytes),中间布洛布(16,777,215个比例),andlongblob(4,294,967,967,295 bytes).tousebl观察性:1)考虑performance impactsandSandStorelargeblobsextern; 2)管理backbackupsandreplication carecration; 3)usepathsinst

MySQL:自动化用户创建的最佳工具是什么?MySQL:自动化用户创建的最佳工具是什么?May 08, 2025 am 12:22 AM

自动化在MySQL中创建用户的最佳工具和技术包括:1.MySQLWorkbench,适用于小型到中型环境,易于使用但资源消耗大;2.Ansible,适用于多服务器环境,简单但学习曲线陡峭;3.自定义Python脚本,灵活但需确保脚本安全性;4.Puppet和Chef,适用于大规模环境,复杂但可扩展。选择时需考虑规模、学习曲线和集成需求。

mysql:我可以在斑点内搜索吗?mysql:我可以在斑点内搜索吗?May 08, 2025 am 12:20 AM

是的,YouCansearchInIdeAblobInMysqlusingsPecificteChniques.1)转换theblobtoautf-8StringWithConvertFunctionWithConvertFunctionandSearchusiseLike.2)forCompresseBlyblobs,useuncompresseblobs,useuncompressbeforeconversion.3)acpperformance impperformance imperformance imptactsanddataEccoding.4)

MySQL字符串数据类型:综合指南MySQL字符串数据类型:综合指南May 08, 2025 am 12:14 AM

mysqloffersvariousStringDatatYpes:1)charforfixed Lengtth Strings,IdealforConsistLengthDatalikeCountryCodes; 2)varcharforvariable长度长,合适的forfieldslikenames; 3)texttypefesforepesforlargertext,forforlargertext,goodforforblogblogpostsbutcan impactcuctcuctcuctpercrance; 4)biland;

掌握mysql blobs:逐步教程掌握mysql blobs:逐步教程May 08, 2025 am 12:01 AM

TomasterMySQLBLOBs,followthesesteps:1)ChoosetheappropriateBLOBtype(TINYBLOB,BLOB,MEDIUMBLOB,LONGBLOB)basedondatasize.2)InsertdatausingLOAD_FILEforefficiency.3)Storefilereferencesinsteadoffilestoimproveperformance.4)UseDUMPFILEtoretrieveandsaveBLOBsco

MySQL中的BLOB数据类型:开发人员的详细概述MySQL中的BLOB数据类型:开发人员的详细概述May 07, 2025 pm 05:41 PM

blobdatatypesinmysqlareusedforvorvoringlargebinarydatalikeimagesoraudio.1)useblobtypes(tinyblobtolonglongblob)基于dondatasizeneeds。 2)库孔素pet petooptimize绩效。3)考虑Xternal Storage Forel Blob romana databasesizerIndimprovebackupe

如何将用户从命令行添加到MySQL如何将用户从命令行添加到MySQLMay 07, 2025 pm 05:01 PM

toadDuserStomySqlfromtheCommandline,loginasroot,thenusecreateuser'username'@'host'host'Indessifiedby'password'; tocreateanewuser.grantpermissionswithgrantprantallprivilegesondatabase

MySQL中有哪些不同的字符串数据类型?详细的概述MySQL中有哪些不同的字符串数据类型?详细的概述May 07, 2025 pm 03:33 PM

mySqlofferSeightStringDatateTypes:char,varchar,二进制,二进制,varbinary,blob,文本,枚举,枚举和set.1)长度,理想的forconsistentDatatalIkeCountryCodes.2)varcharisvariable长度,长度,效率foriforitifforiticforiticforiticforiticforiticforitic forvaryingdatalikename.3)

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。