5--Basic usage of Hibernate--5 3 ways to change the state of persistent objects

Source: Internet
Author: User

1. Persistent Entities

Serializable Save (Object obj): Changes the Obj object to a persisted state, and the object's properties are saved to the database.

void persist (Object obj): Converts the Obj object to a persisted state, and the object's properties are saved to the database.

Serializable Save (object Obj,object PK): Saves the Obj object to the database and, when saved to the database, specifies the primary key value.

void persist (Object Obj,object PK): Converts the Obj object to a persisted state, specifying the primary key value when saving to the database.

2. Load persisted entities based on primary key

News news = Session.load (NEWS.CLASS,PK);

If there is no matching database record, the load () method may throw a hibernateexception exception, and if lazy loading is specified in the persisted annotations, the load () method returns an uninitialized proxy object that does not have the data record loaded. Hibernate does not access the database until a method of the proxy object is called by the program.

News news = Session.get (NEWS.CLASS,PK);

Tips:

1. If the identity attribute (identifier) of news is generated type, hibernate will automatically generate an IDENTITY property value when the Save () method is executed and assign the identity property value to the News object. and the identity property is automatically generated and assigned to the news object when Save () is called. If the identity property of news is assigned type, or when the Composite primary key (composite key) is combined, then the IDENTITY property value should be manually assigned to the news object before calling Save ().

2. The save () method needs to return the identity property value of the persisted object immediately, so the program execution Save () method immediately inserts the data corresponding to the persisted object into the database;

3. Persist () guarantees that it is not immediately converted into an INSERT statement when it is called outside of a transaction. The persist () method is especially important when you need to encapsulate a long session flow.

La La la

5--Basic usage of Hibernate--5 3 ways to change the state of persistent objects

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.