jsp Hibernate入门教程第1/3页
程序员文章站
2024-02-20 12:54:58
例如: 复制代码 代码如下:hibernatetest.java import onlyfun.caterpillar.*; import net.sf.hibernate...
例如:
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());
}
}
}
这个程序会显示数据表中的所有数据,并将数据表中的第一笔数据更新,一个执行的结果如下:
log4j:warn no appenders could be 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
复制代码 代码如下:
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());
}
}
}
这个程序会显示数据表中的所有数据,并将数据表中的第一笔数据更新,一个执行的结果如下:
log4j:warn no appenders could be 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
1