Home » Java » Hibernate notes

Hibernate notes

Writing rules for entity classes
Attribute privatization
Getter/setter method for private attributes
Entity classes are required to have attributes as unique values (usually using ID values)
The

entity class property recommends that you do not use the base data type, and the wrapper class

corresponding to the base data type should be used
 -- because the base data type cannot represent no data --
Example: means student scores
Int score;
Score = 0; means that the student scored 0 points, but not exactly if the student took the exam
Integer score = null; can accurately indicate whether the students have participated in the examination

Hibernate primary key generation strategy
Entity classes are required to have attributes as unique values, corresponding to the primary keys in the table, and primary keys can be generated differently
Hibernate primary key generation strategy
 

native


uuid


id must be a string type


The status of the Hibernate entity class

instant


The

object has no ID value and is not associated with the session


// example: the following objects
User user = new, User ();
User.setUsername ("Rose");
User.setPassword ("123456");
User.setAddress (Guangdong, Guangzhou);

persistent state


The

object has a ID value associated with session,


User tuser = session.get (User.class, 2);

managed state


The

object has a ID value that is not associated with the session


User u = new, User ();
U.setUid (1);

Hibernate bind session

gets the session

with the local thread
Configuration in the hibernate core configuration file
< - binding thread local -- >!
thread

Provides an instance of session that returns the binding to the local thread
public, static, Session, getCurrentSession () {
Return, sessionFactory.getCurrentSession ();
}

When you get the session instance bound to the local thread, you don't have to close the session manually. If you close it, you'll make a mistake
org.hibernate.SessionException:, Session, was, already, closed

Latest