Hibernate中使用的两种查询方式:HQL查询和原生SQL查询
package app;
import app.UserInfo;
import java.util.*;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class Test
{
/**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
try{
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
//HQL查询
//List list = session.createQuery("from UserInfo where username = 'ken'").list();
Iterator iterator = session.createSQLQuery("select * from TESTUSERINFO").list().iterator();
//System.out.println(((UserInfo)list.get(0)).getPassword());
for(;iterator.hasNext(); )
{
Object[] obj=(Object[])iterator.next();
for(int i=0;i<obj.length;i++)
{
System.out.println(obj[i].toString());
}
}
/*
UserInfo user = new UserInfo();
for (ListIterator iterator = list.listIterator(); iterator.hasNext(); ) {
user = (UserInfo)iterator.next();
System.out.println("name: " + user.getUsername());
}*/
/*user.setUserid("4");
user.setUsername("wukerit");
user.setPassword("password");
Transaction tx= session.beginTransaction();
session.save(user);
tx.commit();*/
session.close();
sessionFactory.close();
}
catch(PropertyNotFoundException err)
{
err.printStackTrace();
}
catch(Exception err)
{
err.printStackTrace();
}
//System.out.println("新增資料OK!請先用Oracle觀看結果!");
}
}