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