方法概要: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
使用举例:
System.out.println("获取Session...");
Session session =HibernateSessionFactory.currentSession();
System.out.println("开始Transaction...");
Transaction tx =session.beginTransaction();
Test my_hibernate = null;
System.out.println("Iterator查询...");
Iterator iterator = session.iterate("from Testorder by xm");
while(iterator.hasNext())
{
my_hibernate = (Test)iterator.next();
System.out.println(my_hibernate.getXm() + " "+my_hibernate.getXb());
}
System.out.println("List查询...");
List list = session.find("from Test order byxm");
for(int i = 0;i <list.size();i++)
{
my_hibernate = (Test)list.get(i);
System.out.println(my_hibernate.getXm() + " "+my_hibernate.getXb());
}
System.out.println("Query查询...");
Query query = session.createQuery("from Test orderby xm");
list = query.list();
for(int i = 0;i <list.size();i++)
{
my_hibernate = (Test)list.get(i);
System.out.println(my_hibernate.getXm() + " "+my_hibernate.getXb());
}
iterator = query.iterate();
while(iterator.hasNext())
{
my_hibernate = (Test)iterator.next();
System.out.println(my_hibernate.getXm() + " "+my_hibernate.getXb());
}
System.out.println("Criteria查询...");
Criteria criteria =session.createCriteria(Test.class);
criteria.add(Expression.eq("xb","f"));
list = criteria.list();
for(int i = 0;i <list.size();i++)
{
my_hibernate = (Test)list.get(i);
System.out.println(my_hibernate.getXm() + " "+my_hibernate.getXb());
}
System.out.println("更新数据...");
my_hibernate =(Test)session.load(Test.class,"121");
my_hibernate.setXb("f");
System.out.println("提交Transaction...");
tx.commit();
System.out.println("关闭Session...");
HibernateSessionFactory.closeSession();