Session Description in hibernate

Source: Internet
Author: User
The transparent persistence of hibernate makes it very comfortable to use, and sometimes even forgets the existence of the database. My friends often cannot tell the differences between SAVE, saveorupdate, and update. Lock, merge, replicate, refresh, and evict do not even know what to do. In addition, there are many concepts about the lifecycle of object, but the differences between transient, persistent, and detached are not clear. I just know the popular names such as po and vo. In fact, these concepts are very simple, and hibernate's javadoc is clearly written. You only need to look at it to understand it. So I spent some time translating the javadoc part of the session in hibernate 3.1.2 to let those who forget its basic concepts because of hibernate's "Transparency" and then look at these concepts.


Org. hibernate
Interface session
All superinterfaces:
Serializable
All known subinterfaces:
Eventsource, session
All known implementing classes:
Sessionimpl
Public interface Session

Extends serializable

Java applicationsProgramAnd the main runtime interface between hibernate. It is the core abstract API class that abstracts the concept of persistent service.

SessionIs bound to a physical transaction (tansaction. (Long transactions may span multiple database transactions .)

SessionThe main function is to create, read, and delete ing object class instances. The instance may exist in the following three States:

Free Status (transient ):No persistence, noSessionAssociated
Persistent ):OnlySessionAssociated
Detached ):Persistence has been performed, but not with anySessionAssociated

Instances in the free status can be called by callingSave (),Persist ()OrSaveorupdate ()Method for persistence. Persistent instances can be called by callingDelete ()Changes to the Free State. PassGet ()OrLoad ()The instances obtained by the method are all persistent. Instances in the free status can be called by callingUpdate (), 0Saveorupdate (),Lock ()OrReplicate ()For persistence. Instances in the free or free state can be calledMerge ()Method to become a new persistent instance.

Save ()AndPersist ()SQLInsert,Delete ()SQLDelete, AndUpdate ()OrMerge ()SQLUpdate. PairPersistent)Instance modifications are detected during refresh and submission, which also causes SQLUpdate.Saveorupdate ()OrReplicate ()SQLInsertOrUpdate.

Its implementation is not necessarily thread-safe. Each thread/transaction should be fromSessionfactoryObtain your own session instance.

If its Persistent Object Class is serializableSessionThe instance is also serializable.

A typical transaction should use the following format:

 
Session sess = factory. opensession (); transaction TX; try {Tx = sess. begintransaction (); // do some work... TX. commit ();} catch (exception e) {If (TX! = NULL) Tx. rollback (); throw E;} finally {sess. Close ();}

IfSessionIf an exception is thrown, the transaction must be rolled back and the session will be discarded. After an exception occursSessionThe internal status of may be out of sync with the database.

 

 

Author:
Gavin king
See also:
Sessionfactory

Method Summary
Transaction Begintransaction()
Starts a work unit and returns the associatedTransaction)Object.
Void Cancelquery()
Terminate the current query.
Void Clear()
Completely clear this session.
Connection Close()
Stop thisSessionBy interrupting the JDBC connection and clearing it.
Connection Connection()
Obtain the JDBC connection of this session.

If this session uses an active collection release policy (for example, CMT-container controls the transaction environment), the current application is responsible for closing the connection to this call.

Boolean Contains(Object object)
Check whether the object instance matches the currentSessionAssociation (whether it is in the persistent State ).
Criteria Createcriteria(Class persistentclass)
Creates a newCriteriaInstance.
Criteria Createcriteria(Class persistentclass, string alias)
Creates a new object based on the given object class or its superclass.CriteriaInstance, and assign it (entity class) an alias.
Criteria Createcriteria(String entityname)
Creates a new object based on the name of the given object.CriteriaInstance.
Criteria Createcriteria(String entityname, string alias)
Creates a new object based on the name of the given object.CriteriaInstance, and assign it (entity class) an alias
Query Createfilter(Object collection, string querystring)
Creates a newQueryInstance.
Query Createquery(String querystring)
Creates a new hql query condition.QueryInstance.
Sqlquery Createsqlquery(String querystring)
Create a newSqlqueryInstance.
Void Delete(Object object)
Removes an instance of a persistent object from a database.
Void Delete(String entityname, object)
Removes an instance of a persistent object from a database.
Void Disablefilter(String filtername)
Disable the name filter of the current session.
Connection Disconnect()
DisconnectedSessionAnd the current JDBC connection.
Filter Enablefilter(String filtername)
Open the name filter of the current session.
Void Evict(Object object)
Clears the current object instance from the session cache.
Void Flush()
Force commit refresh (flush)Session.
Object Get(Class clazz, serializable ID)
Returns the instance of the Persistent Object Based on the given identifier and object class. If no persistence object instance meets the condition, null is returned.
Object Get(Class clazz, serializable ID, lockmode)
Returns the instance of the Persistent Object Based on the given identifier and object class. If no persistence object instance meets the condition, null is returned.
Object Get(String entityname, serializable ID)
Returns the persistence instance that matches the given object name and identifier. If no persistence instance is matched, null is returned.
Object Get(String entityname, serializable ID, lockmode)
Returns the persistence instance that matches the given object class and identifier. If no persistence instance exists, null is returned.
Cachemode Getcachemode()
Obtain the current cache mode.
Lockmode Getcurrentlockmode(Object object)
Checks the current lock level of a given object.
Filter Getenabledfilter(String filtername)
Obtain a filter based on the name ).
Entitymode Getentitymode()
Obtains the effective entity mode of this session.
String Getentityname(Object object)
Returns the Object Name of a persistent object.
Flushmode Getflushmode()
Obtain the current flush mode.
Serializable Getidentifier(Object object)
Obtain the given object instance inSessionIf the instance is in the free status (transient) orSessionAssociation throws an exception.
Query Getnamedquery(String queryname)
ObtainsQuery)Instance.
Session Getsession(Entitymode)
Starts a new valid session based on the given entity mode.
Sessionfactory Getsessionfactory()
ObtainSessionfactoryInstance.
Sessionstatistics Getstatistics()
Obtains the statistics of this session.
Transaction Gettransaction()
ObtainTransaction)Instance. Instance associated with this session.
Boolean Isconnected()
Check the currentSessionWhether it is in the connection status.
Boolean Isdirty()
CurrentSessionIs there any changes (data status) that need to be synchronized with the database? If we refresh the Commit (flush) session, will there be SQL Execution?
Boolean Isopen()
Check the currentSessionWhether or not it is still enabled.
Object Load(Class theclass, serializable ID)
If a qualified instance exists, a persistent instance is returned Based on the given entity class and identifier.
Object Load(Class theclass, serializable ID, lockmode)
If a qualified instance exists, a persistent instance is returned Based on the given entity class, identifier, and specified lock level.
Void Load(Object object, serializable ID)
Copy the persistent state (value) corresponding to the given identifier to the given trasient instance.
Object Load(String entityname, serializable ID)
If a qualified instance exists, a persistent instance is returned Based on the given entity class and identifier.
Object Load(String entityname, serializable ID, lockmode)
If a qualified instance exists, a persistent instance is returned Based on the given entity class, identifier, and specified lock level.
Void Lock(Object object, lockmode)
Obtain the specified lock level from a given object.
Void Lock(String entityname, object, lockmode)
Obtain the specified lock level from a given object.
Object Merge(Object object)
Copy the status of a given object to a persistent object with the same identifier.
Object Merge(String entityname, object)
Copy the status of a given object to a persistent object with the same identifier.
Void Persist(Object object)
Persists an instance in the Free State (transient.
Void Persist(String entityname, object)
Persists an instance in the Free State (transient.
Void Reconnect()
Not recommended. Manual reconnection is only applicable when applications provide connections. In this case, you may need to useReconnect (Java. SQL. Connection).
Void Reconnect(Connection)
Reconnect to the specified JDBC connection.
Void Refresh(Object object)
Reads the status of a given instance from the database.
Void Refresh(Object object, lockmode)
Based on the specifiedLock mode)To re-read the status of a given instance from the database.
Void Replicate(Object object, replicationmode)
Uses the current Identifier value to persist the given free state (transient) entity.
Void Replicate(String entityname, object, replicationmode)
Uses the current Identifier value to persist the given free state (transient) entity.
Serializable Save(Object object)
First, generate an identifier for the given free state (transient) object (according to the configuration) and assign a value, and then make it persistent.
Serializable Save(String entityname, object)
First, generate an identifier for the given free state (transient) object (according to the configuration) and assign a value, and then make it persistent.
Void Saveorupdate(Object object)
Based on the value of the Identity attribute of the given instance (Note: unsaved-value can be specified. The default value is null .) To determine the executionSave ()OrUpdate ()Operation.
Void Saveorupdate(String entityname, object)
Based on the value of the Identity attribute of the given instance (Note: unsaved-value can be specified. The default value is null .) To determine the executionSave ()OrUpdate ()Operation.
Void Setcachemode(Cachemode)
Set the refresh submission mode.
Void Setflushmode(Flushmode)
Set the refresh submission mode.
Void Setreadonly(Object entity, Boolean readonly)
Set an unchanged Persistent Object to read-only mode, or mark a read-only object as a mode that can be modified.
Void Update(Object object)
Update the persistent instance based on the ID of the specified detached (idle state) object instance.
Void Update(String entityname, object)
Update the persistent instance based on the ID of the specified detached (idle state) object instance.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.