1. configuration/sessionfactory/session
The configuration instance represents an application. Program The complete set of Java-to-SQL database ing in. configuration is used to build a (immutable) sessionfactory.
Sessionfactory is thread-safe and costly to create.
The session is non-thread-safe and lightweight. A session corresponds to a JDBC connection,
Session connection () gets the connection object of the session and its corresponding database.
The session function is to operate on objects that are mapped to database tables.
The session operation objects are stateful and divided into three types:
Free State (transient): Not persistent, not associated with any session, and there is no corresponding record in the database table.
Persistent: it is associated with a session and corresponds to a record in the database table.
Detached.
Instances in the Free State can be persisted by calling the SAVE (), persist (), or saveorupdate () method. A persistent instance can be changed to a free state by calling Delete. The instances obtained through the get () or load () method are in a persistent state. Instances in the free status can be persisted by calling Update (), 0 saveorupdate (), lock (), or replicate. Instances in the free or free State can become a new persistent instance by calling the Merge () method.
2. Save ()/persist ()/update ()/saveorupdate ()/Merge ()/delete () method of the session
The SAVE () method saves the specified object and inserts a data entry into the table;
The persist () method saves the specified object and inserts a piece of data into the table. I have not found any special difference between it and the Save method.
The replicate () method uses the values (including the ID) of each attribute of the given object to persist the given free state (transient) entity. It is very violent, you also need to specify the storage mode (four storage policies are available ).
The Update () method updates the specified object and updates a piece of data in the table;
The saveorupdate () method receives an object and determines whether to save or update the object based on its ID. In this way, the methods for saving and updating objects are unified;
The merge () method copies the state of a given object to a persistent object with the same identifier.
The Delete () method deletes a specified object and deletes a data entry in the table;
Note: To use the saveorupdate () method, you can set the unsaved-value = "null" of the <ID> tag to determine what operations to perform when the ing file is defined: when the ID attribute is equal to the value of unsaved-value (null here), it is considered that it has not been saved. The save operation should be performed; otherwise, the update operation should be performed. After this setting, you can use the saveorupdate () method to save and update the methods.
<ID name = "ID" column = "ID" type = "Java. Lang. Integer" unsaved-value = "null">
<Generator class = "native"/>
</ID>
Unsaved-value can be set with four values:
Any: always stored
None: always updated
Null: storage when ID is null (preset)
Valid: stored when ID is null or a specified value
3. Get ()/load () method of the session
The get () method always queries object objects. If no object exists, null is returned;
The load () method is also used to obtain an object. If the object does not exist, an exception occurs when the NULL pointer is thrown.
4. Clear ()/evict () method of the session
The clear () method clears all objects (including various States) in the session-level cache to release the memory.
The evict () method clears the specified object (including various States) in the session-level cache.
Of course, after the session is closed, these caches will no longer exist and will wait for JVM to recycle them.
5. Session flush () method
Flush () forces persistence of the Entity objects in the session cache. Clear () or evict () is also called to save and release valuable memory resources.
6. The COMMIT ()/rollback () method of the session
The COMMIT () method is used to commit transactions on a session. Otherwise, the unit of work will not affect the database. If an exception (that is, a Commit () failure occurs) occurs during execution, the previous operation is canceled and the previous operation can be undone by performing rollback.
7. session close ()/isopen ()/isconnected ()/reconnect () method
The close () method closes the connection to the database corresponding to the session, and the lifecycle of the associated object ends.
The isopen () method checks whether the session is still open. If the session has been disconnected, you can use reconnect (connection) to re-associate the session with a JDBC connection.
The isconnected () method checks whether the current session is connected.
8. Criteria, detchedcriteria, and Query Interfaces
Criteria and query instances are bound to the session, and the lifecycle ends with the end of the session.
The detchedcriteria instance is equivalent to an SQL template for reuse. The getexecutablecriteria (Session) method receives and binds a session object and returns a criteria object.
9. initialize () method of the hibernate class
The initialize () method forces hibernate to immediately load the objects and sets associated with the specified object. There are several other useful but not commonly used methods in the hibernate class.
10. Lazy attributes in the ing File
In hibernate3, the lazy attribute of the class element is true by default. If this attribute is not required, it must be set to lazy = "false". Otherwise, an exception is thrown to the object returned by the operation load. In addition, you can specify the lazy attribute for the object attribute in hibernate3.
11. JDBC transactions and JTA transactions
Hibernate does not have the transaction management function. It relies on JDBC or JTA's transaction management function. In the hibernate configuration file, if the factory category attribute of transaction is not explicitly specified, hibernate. transaction. the configuration of factory_class is a JDBC transaction by default:
<Property name = "hibernate. transaction. factory_class"> org. hibernate. transaction. jdbctransactionfactory </property>.
After the session is obtained through sessionfactory, The JDBC connection instance associated with the session is set to false.
Note: If the database does not support transactions, for example, the MyISAM engine table of MySQL does not support transactions, and the transaction declaration does not work. To enable mysql5 tables to support transactions, you can specify the engine type of the table as InnoDB. If you are studying or researching, it is best to use PostgreSQL 8.3 or DB2 or Oracle.
JDBC transactions are always associated with a database connection (or session.
JTA transactions can span multiple data connections (or multiple sessions). These connections can also be connections to different databases. JTA transactions are generally managed by containers. You only need to define the boundaries of JTA transactions at the beginning and end of multiple operation units.
Note: If a JTA transaction is used, you cannot use a JDBC transaction to manage the operations of each session. Otherwise, an error occurs. For the universality of the program, JTA transactions are generally used to build the application, which uses any environment. Of course, you can also use the transaction proxy to add transaction control to each JDBC operation method. This also makes it very convenient for the program to be transplanted to the JTA container transaction later. In fact, we can now use Spring transaction management, which is perfectly integrated with hibernate.
PS: The persist () method does not return a value. The save () method returns an object identifier.
The persist () method can only save objects in transient and persistent states. The save () method can save objects in any State.
In common: After a method is called, the object state changes to persistent state.
The get () method returns the instance of the object. For the load () method, if the object with this identifier exists in the persistent context, the instance of the object is returned. Otherwise, the proxy of the object is returned, the proxy object only contains the object identifier.
Be careful when using get or load
Load can improve the cache efficiency, but because it is a proxy, subsequent operations will be more prone to problems.
Get directly accesses the database, so the cache function is useless, but the results will be immediately less troublesome, but frequent calls will inevitably increase the pressure on the server.