If you enable a Session, extract the data from the data table and display it on the user interface, and then close the Session. When the user completes the operation on the interface and presses storage, you need to restart a Session and use the update() method to update the data in the object to the corresponding data table. An example is as follows:
Copy the codeThe code is as follows:
import .*;
import .*;
import .*;
import .*;
public class HibernateTest {
public static void main(String[] args) throws HibernateException {
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = ();
List users = ("from User");
// Close this Session
();
User updated = null;
for (ListIterator iterator = (); (); ) {
User user = (User) ();
if(updated == null)
updated = user;
(() +
"\n\tAge: " + () +
"\n\tSex: " + ());
}
// The user does some operations and then save it
("caterpillar");
// Open a new session
session = ();
// Update data
(updated);
users = ("from User");
();
();
for (ListIterator iterator = (); (); ) {
User user = (User) ();
(() +
"\n\tAge: " + () +
"\n\tSex: " + ());
}
}
}
The following is the following example of the results of this program execution. You can see which SQL actually execute:
log4j:WARN No appenders could be found for logger ().
log4j:WARN Please initialize the log4j system properly.
Hibernate: select user0_.user_id as user_id, user0_.name as name, user0_.sex as sex, user0_.age as age from USER user0_
justin
Age: 28
Sex: M
momor
Age: 25
Sex: F
Bush
Age: 25
Sex: M
Becky
Age: 35
Sex: F
Hibernate: update USER set name=?, sex=?, age=? where user_id=?
Hibernate: select user0_.user_id as user_id, user0_.name as name, user0_.sex as sex, user0_.age as age from USER user0_
caterpillar
Age: 28
Sex: M
momor
Age: 25
Sex: F
Bush
Age: 25
Sex: M
Becky
Age: 35
Sex: F
Previous page123Next pageRead the full text