1,select子查询属性组成成list
1,把select子查属性组装成新的对象
//把select选择的属性封装成list
public void testHqlFindPropertyAsList(){
Configuration cfg = new Configuration().configure();
SessionFactory sessionFactory = cfg.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction ts = session.beginTransaction();
//注意查询属性的方式:使用 p.id p.name p.address
List persons = session.createQuery("select new List(p.id,p.name,p.address) from Person p where p.name like :myname ")
.setString("myname", "%lijuan%").list();
for(Iterator it = persons.iterator(); it.hasNext();){
List eachList = (List)it.next();
for(Iterator it2 = eachList.iterator(); it2.hasNext();){
System.out.println();
System.out.println("***************************");
System.out.println(it2.next());
System.out.println("**************************");
System.out.println();
}
}
}
1,把select子查属性组装成新的对象
//把select选择的属性封装成对象
public void testHqlFindPropertyAsOjbect(){
Configuration cfg = new Configuration().configure();
SessionFactory sessionFactory = cfg.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction ts = session.beginTransaction();
List smalPersonList = session.createQuery("select new SmalPerson(p.name,p.age) from Person p")
.setString("myname", "%lijuan%").list();
for(Iterator it = smalPersonList.iterator();it.hasNext();)
{
SmalPerson sp = (SmalPerson)it.next();
System.out.println(sp.getAge());
System.out.println(sp.getName());
}
}