Home Database Mysql Tutorial Hibernate持久化对象的状态

Hibernate持久化对象的状态

Jun 07, 2016 pm 04:09 PM
hibernate object Endurance state angle

1. 站在持久化的角度 , Hibernate 把对象分为 4 种状态 : 持久化状态 , 临时状态 , 游离状态(脱管状态) , 删除状态.Session 的特定方法能使对象从一个状态转换到另一个状态 . 2. 临时状态(Transient) : 1). 在使用代理主键的情况下 , OID 通常为 null 2

1. 站在持久化的角度 , Hibernate 把对象分为 4 种状态 : 持久化状态, 临时状态, 游离状态(脱管状态) , 删除状态.Session 的特定方法能使对象从一个状态转换到另一个状态 .
2. 临时状态(Transient) : 1). 在使用代理主键的情况下 , OID 通常为 null 2). 不处于 Session 的缓存中 3). 在数据库中没有对应的记录
3. 持久化状态(也叫”托管” )( Persist)
1). OID 不为 null 2). 位于 Session 缓存中 3). 若在数据库中已经有和其对应的记录 , 持久化对象和数据库中的相关记录对应 4). Session 在 flush 缓存时 , 会根据持久化对象的属性变化 , 来同步更新数据库 5). 在同一个 Session 实例的缓存中 , 数据库表中的每条记录只对应唯一的持久化对象( Session 缓存对象的 OID 不能重复)
@Test public void testOID(){ News news2 = (News ) session.get (News.class , 1); System.out.println (news2 ); News news = new News (); news.setAuthor ("Oracle2" ); news.setContent ("Oracle DB" ); news.setDate (new Date ()); news.setTitle ("DB" ); news.setId (1 ); //抛出异常: org.hibernate.NonUniqueObjectException session.update (news ); }
4. 游离状态(也叫”脱管” ) (Detached ): 1). OID 不为 null 2). 不再处于 Session 缓存中 3). 一般情况需下 , 游离对象是由持久化对象转变过来的 , 因此在数据库中可能还存在与它对应的记录
5. 删除状态(Removed) 1). 在数据库中没有和其 OID 对应的记录 2). 不再处于 Session 缓存中 3). 一般情况下 , 应用程序不该再使用被删除的对象
6. 状态的转换图 \ \
7. get & load 的区别: 共同点: get、load 从数据表中加载一个对象 , 加载后即为持久化状态的对象 不同点: get:喎?http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vc3Ryb25nPiDBory0sunRr8r9vt2x7S4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VjdFN0YXRlRXhjZXB0aW9uCgo8YnI+CgoKOS4gc2F2ZU9yVXBkYXRlIDxzdHJvbmc+KCk6PC9zdHJvbmc+ILGjtOa78rj80MIKCjxpbWcgc3JjPQ=="http://www.2cto.com/uploadfile/2014/1107/20141107092134167.png" alt="\">
11. delete ():删除一个对象对应的记录。 1). 若只想删除指定的对象,设置 ID 即可。 2). 若数据表中没有对应的记录,则抛出异常: java.lang.IllegalArgumentException
12. Hibernate 调用原生的 JDBC API:
session.doWork(new Work() { @Override public void execute(Connection connection ) throws SQLException { //使用 JDBC 原生的 API } });

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Connection status in standby: Disconnected, reason: NIC Compliance Connection status in standby: Disconnected, reason: NIC Compliance Feb 19, 2024 pm 03:15 PM

"The connection status in the event log message shows Standby: Disconnected due to NIC compliance. This means that the system is in standby mode and the network interface card (NIC) has been disconnected. Although this is usually a network issue , but can also be caused by software and hardware conflicts. In the following discussion, we will explore how to solve this problem." What is the reason for standby connection disconnection? NIC compliance? If you see the "ConnectivityStatusinStandby:DisConnected,Reason:NICCompliance" message in Windows Event Viewer, this indicates that there may be a problem with your NIC or network interface controller. This situation is usually

Convert an array or object to a JSON string using PHP's json_encode() function Convert an array or object to a JSON string using PHP's json_encode() function Nov 03, 2023 pm 03:30 PM

JSON (JavaScriptObjectNotation) is a lightweight data exchange format that has become a common format for data exchange between web applications. PHP's json_encode() function can convert an array or object into a JSON string. This article will introduce how to use PHP's json_encode() function, including syntax, parameters, return values, and specific examples. Syntax The syntax of the json_encode() function is as follows: st

How to set Momo status How to set Momo status Mar 01, 2024 pm 12:10 PM

Momo, a well-known social platform, provides users with a wealth of functional services for their daily social interactions. On Momo, users can easily share their life status, make friends, chat, etc. Among them, the setting status function allows users to show their current mood and status to others, thereby attracting more people's attention and communication. So how to set your own Momo status? The following will give you a detailed introduction! How to set status on Momo? 1. Open Momo, click More in the lower right corner, find and click Daily Status. 2. Select the status. 3. The setting status will be displayed.

How to implement data caching and persistence in Vue How to implement data caching and persistence in Vue Oct 15, 2023 pm 01:06 PM

How to implement data caching and persistence in Vue In Vue, data caching and persistence is a common requirement. Caching data can improve application performance, while persistent data allows users to still see previously saved data after refreshing the page or reopening the application. The following will introduce how to cache and persist data through some common methods. Using Vuex to implement data caching Vuex is Vue's official state management library, which can be used to centrally manage the state of all components of the application. We can leverage Vuex

How to check server status How to check server status Oct 09, 2023 am 10:10 AM

Methods to view server status include command line tools, graphical interface tools, monitoring tools, log files, and remote management tools. Detailed introduction: 1. Use command line tools. On Linux or Unix servers, you can use command line tools to view the status of the server; 2. Use graphical interface tools. For server operating systems with graphical interfaces, you can use the graphics provided by the system. Use interface tools to view server status; 3. Use monitoring tools. You can use special monitoring tools to monitor server status in real time, etc.

How to convert MySQL query result array to object? How to convert MySQL query result array to object? Apr 29, 2024 pm 01:09 PM

Here's how to convert a MySQL query result array into an object: Create an empty object array. Loop through the resulting array and create a new object for each row. Use a foreach loop to assign the key-value pairs of each row to the corresponding properties of the new object. Adds a new object to the object array. Close the database connection.

Detailed explanation of the five states of Java threads and state transition rules Detailed explanation of the five states of Java threads and state transition rules Feb 19, 2024 pm 05:03 PM

In-depth understanding of the five states of Java threads and their conversion rules 1. Introduction to the five states of threads In Java, the life cycle of a thread can be divided into five different states, including new state (NEW), ready state (RUNNABLE), Running status (RUNNING), blocking status (BLOCKED) and termination status (TERMINATED). New state (NEW): When the thread object is created, it is in the new state. At this point, the thread object has allocated enough resources to perform the task

Use Python's __contains__() function to define the containment operation of an object Use Python's __contains__() function to define the containment operation of an object Aug 22, 2023 pm 04:23 PM

Use Python's __contains__() function to define the containment operation of an object. Python is a concise and powerful programming language that provides many powerful features to handle various types of data. One of them is to implement the containment operation of objects by defining the __contains__() function. This article will introduce how to use the __contains__() function to define the containment operation of an object, and give some sample code. The __contains__() function is Pytho

See all articles