簡體   English   中英

如何使用休眠連接兩個表

[英]how to join two tables using hibernate

我想使用HQL或Criterial聯接兩個表

表是

  • 購物車表(購物車ID,BookID,CustomerEmailID,數量)
  • 圖書表(bookId,bookName,bookPrice,bookQuantity)

我的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

這將給的計數cartId和總和bookPrice

我將此結果存儲到List<Cart>

以下是cart POJO變量

private int bookid,cartId;
    private String customeremailid,bookName,bookDescription,image;
    private int quantity;
    private long price;

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());
             }

我在Hibernate中沒有怎么做

請建議謝謝

使用下面的休眠標准示例代碼加入:

List cats = sess.createCriteria(Cat.class)
    .createAlias("kittens", "kt")
    .createAlias("mate", "mt")
    .add( Restrictions.eqProperty("kt.name", "mt.name") )
    .list();

參考: https : //docs.jboss.org/hibernate/orm/3.5/reference/en/html/querycriteria.html#querycriteria-associations

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM