For example:
Copy Code code as follows:
Hibernatetest.java
Import onlyfun.caterpillar.*;
Import net.sf.hibernate.*;
Import net.sf.hibernate.cfg.*;
Import java.util.*;
public class Hibernatetest {
public static void Main (string[] args) throws Hibernateexception {
Sessionfactory sessionfactory = new Configuration (). Configure (). Buildsessionfactory ();
Session session = Sessionfactory.opensession ();
List users = Session.find ("from User");
User updated = null;
for (Listiterator iterator = Users.listiterator (); Iterator.hasnext ();) {
User user = (user) iterator.next ();
if (updated = null)
Updated = user;
System.out.println (User.getname () +
"\n\tage:" + user.getage () +
"\n\tsex:" + user.getsex ());
}
Updated.setname ("Justin");
Session.flush ();
Users = Session.find ("from User");
Session.close ();
Sessionfactory.close ();
for (Listiterator iterator = Users.listiterator (); Iterator.hasnext ();) {
User user = (user) iterator.next ();
System.out.println (User.getname () +
"\n\tage:" + user.getage () +
"\n\tsex:" + user.getsex ());
}
}
}
This program displays all the data in the datasheet, updates the first data in the datasheet, and executes the following results:
Log4j:warn No Appenders could is found for logger (net.sf.hibernate.cfg.Environment).
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_
Caterpillar
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_
Justin
Age:28
Sex:m
Momor
Age:25
Sex:f
Bush
Age:25
Sex:m
Becky
Age:35
Sex:f
Current 1/3 page
123 Next read the full text