Home » Java » The operation of the Hibernate entity class

The operation of the Hibernate entity class

Add to

calls session inside save () method



User user = new, User ();
User.setUsername ("Rose");
User.setPassword ("123456");
User.setAddress (Guangdong, Guangzhou);
Session.save (user);

query

calls the get (Class, int) method

, based on the ID query
User tuser = session.get (User.class, 1);
System.out.println (tuser.toString ());

Implementation of the bottom layer of
-- hibernate
Hibernate:
Select
User0_.uid as uid1_0_0_,
User0_.username as username2_0_0_,
User0_.password as password3_0_0_,
User0_.address as address4_0_0_
From
T_user user0_
Where
User0_.uid=?
-- run query results
User, [uid=1, username=Jeff, password=123456, address=, Guangdong, Maoming.]

Modify operation

queries first and then modifies


User tuser = session.get (User.class, 2);
Tuser.setUsername ("Li Bai");
Session.update (tuser);

 - bottom implementation
Hibernate:
Select
User0_.uid as uid1_0_0_,
User0_.username as username2_0_0_,
User0_.password as password3_0_0_,
User0_.address as address4_0_0_
From
T_user user0_
Where
User0_.uid=?
Hibernate:
Update
T_user
Set
Username=?,
Password=?,
Address=?
Where
Uid=?
?
delete
//1. way one
User tuser = session.get (User.class, 1);
Session.delete (tuser);
//2. mode two (Shao Yong)
User u = new, User ();
U.setUid (1);
Session.delete (U);

The use of the saveOrUpdate () method
This method selects, adds, or modifies according to the state of the entity class

//1. instant, add
User UU = new, User ();
Uu.setUsername ("Jack");
Uu.setAddress ("Guangzhou");
Uu.setPassword ("123");
Session.saveOrUpdate (Uu);
/ / managed state changes
/ / modified according to the entity class ID, if not related to the ID table, are not modified nor added
User UU = new, User ();
Uu.setUid (1);
Uu.setUsername ("Jack");
Uu.setAddress ("Guangzhou");
Uu.setPassword ("123");
Session.saveOrUpdate (Uu);
/ / persistent state changes
User UU = session.get (User.class, 1);
Uu.setUsername ("Jack");
Session.saveOrUpdate (Uu);

Latest