Introduction to jsp Hibernate Functions

Source: Internet
Author: User

1. Configuration/SessionFactory/Session
The Configuration instance represents the complete set of Java-to-SQL database ing in an application. 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 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.

Related Article

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.