[英]how to join two tables using hibernate
I want to join two tables using HQL or Criterial 我想使用HQL或Criterial联接两个表
Tables are 表是
My sql query is like this 我的SQL查询是这样的
select count(c.cartId) as cartId,b.bookName as bookName,c.customeremailid as customeremailid,sum(b.bookPrice)as c.price,c.quantity from Book b,Cart c where c.customeremailid='"+customeremailid+"' and b.bookId=c.bookid
which will give the count of cartId
and sum of bookPrice
这将给的计数
cartId
和总和bookPrice
i am storing this result into List<Cart>
我将此结果存储到
List<Cart>
following is cart POJO
variables 以下是
cart POJO
变量
private int bookid,cartId;
private String customeremailid,bookName,bookDescription,image;
private int quantity;
private long price;
HQL Query : HQL查询:
String sql="select count(c.cartId) as cartId,b.bookName as bookName,c.customeremailid as customeremailid,sum(b.bookPrice)as price,c.quantity from Book b,Cart c where c.customeremailid='"+customeremailid+"' and b.bookId=c.bookid";
Query q=s.createSQLQuery(sql);
Cartlist=q.setResultTransformer(Transformers.aliasToBean(Cart.class)).list();
for(Cart c :Cartlist)
{
System.out.println("in cart : "+c.getCartId());
System.out.println("in cart : "+c.getPrice());
}
Iam not getting how to do it in Hibernate 我在Hibernate中没有怎么做
kindly suggest Thanks 请建议谢谢
Joins using hibernate Criteria example code below: 使用下面的休眠标准示例代码加入:
List cats = sess.createCriteria(Cat.class)
.createAlias("kittens", "kt")
.createAlias("mate", "mt")
.add( Restrictions.eqProperty("kt.name", "mt.name") )
.list();
Reference: https://docs.jboss.org/hibernate/orm/3.5/reference/en/html/querycriteria.html#querycriteria-associations 参考: https : //docs.jboss.org/hibernate/orm/3.5/reference/en/html/querycriteria.html#querycriteria-associations
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.