Hibernate中Query对象的运用
Hibernate中Query对象的使用
1 个或多个属性查询:
Query query=session.createQuery(”select customername,customerid from Customer”)
List l=query.list();
For(int i=0;i<l.size();i++)
{
Obejct[] object=(Object[])l.get(i);
Object[0] object[1]
}
}
分组: “select count(*),productname from Product group by productname order by productname”
取值与属性一样
配置的查询,在*.hbm.xml中
<query name=”sql”>
<![CDATA[
from Product where productid=:productid
]]>
</query>
Query query=session.getNamedQuery(sql);
联接1
”from Customer as customer join fetch customer.buySet”:将多的放到buySet属性中,得出的结是Customer有一个,Buy有多个
联接2
“from Customer as customer join customer.buySet”:得出的对象,customer与buy是1对1
子查询:
”from Customer as customer where (select count(*) from customer.buySet)>1″
1 个或多个属性查询:
Query query=session.createQuery(”select customername,customerid from Customer”)
List l=query.list();
For(int i=0;i<l.size();i++)
{
Obejct[] object=(Object[])l.get(i);
Object[0] object[1]
}
}
分组: “select count(*),productname from Product group by productname order by productname”
取值与属性一样
配置的查询,在*.hbm.xml中
<query name=”sql”>
<![CDATA[
from Product where productid=:productid
]]>
</query>
Query query=session.getNamedQuery(sql);
联接1
”from Customer as customer join fetch customer.buySet”:将多的放到buySet属性中,得出的结是Customer有一个,Buy有多个
联接2
“from Customer as customer join customer.buySet”:得出的对象,customer与buy是1对1
子查询:
”from Customer as customer where (select count(*) from customer.buySet)>1″