Principles and Applications of Hibernate (II)

Source: Internet
Author: User

1. The initialization code (only once) Configuration cfg = newConfiguration (); cfg. configure ("config. cfg. xml"); you can also set properties through cfg. setProperty. SessionFactory sessionFactory = cfg. buildSessionFactory () 2. template code Session session = null; Transaction tx = null; try {session = sessionFactory. openSession (); tx = session. beginTransaction ();//... Your code save, delete, update, get... Tx. commit ();} catch (partition tione) {if (tx! = Null) tx. rollback (); throw e;} finally {if (session! = Null) session. close ();} 3 main methods of Session 1. save to save data 2. delete, delete objects 3. update and update objects. If there are no records in the database, an exception will occur. 4. get. query by ID and access the database immediately. 5. Load: query by ID. (The returned result is a proxy and the database is not accessed immediately ). 6. saveOrUpdate (save or update 4 primary key generation mechanism is determined based on the value of ID and version. Increment: The identifier is automatically generated by hibernate in an incremental manner. Each Increment is 1. indentity: identifier generated by a low-level database, provided that the database supports automatic data growth. Sequence: hibernate automatically generates identifiers based on underlying database pairs. tianjian is a database supporting the sequence Native: Choose indentity, Sequence, and hilo based on the ability of the underlying database to automatically generate. Uuid. Hex: hibernate uses the 128-bit uuid algorithm to generate identifiers. This policy is not popular because string-type primary keys occupy more database space than integer primary keys. Assigned: applicable to natural primary keys. The java program is responsible for generating identifiers. You cannot declare the setId () method as private. Try to avoid using the natural primary key Foreign: use the identifier of another associated object, which is often used together with <one-to-one>. Object status: transient: no data in the database corresponds to it. Beyond the scope, the JVM Garbage Collector recycles the object, which is generally a new object and is not associated with the session. Persistent: the database may have data that corresponds to the session. Currently, the session associated with the session is not closed, and the transaction is not committed. The persistent Object status changes, the database is affected when the transaction is committed (which can be detected by hibernate ). Detached. Manipulate the save () method of the persistence object-save () Session to convert a temporary object to the save () method of the persistence object Session to complete the following operations: add the News object to the Session cache, to enable persistence, select the identifier generator specified by the ing file and assign a unique OID to the persistence object. when the proxy primary key is used, the setId () method sets the OID to invalidate for the News object. execute an insert statement to assemble the current property value of the Customer object into the insert statement. Hibernate uses the persistence object OID to maintain its correspondence with database-related records. when a News object is in a persistent state, the program is not allowed to modify its ID at will to manipulate the update () method of the Persistent Object-update () Session to convert a free object to a persistent object, and execute an update statement. manipulate persistent objects-saveOrupdate () sa VeOrUpdate: This method includes both the save and update Methods. If the parameter is a temporary object, the save method is used. If the parameter is a free object, the update method is used. If the parameter is a persistent object, the system returns the result directly.

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.