How to limit a collection in an Object's property in hibernate?

后端 未结 3 716
离开以前
离开以前 2021-01-25 19:24

I have two entities: item, and bid, each item have many bids for it, so bid is a collection property of item.

in the page that show an item, I just want to show the firs

3条回答
  •  一个人的身影
    2021-01-25 19:49

    For plain SQL, do the following:

    Select B.*
    From item I, bid B,
    Where T.itemId = B.itemId
    Limit 10;
    

    Same can be done in case you are using hibernate, using criteria API or HQL HQL example:

    Query query = session.createQuery("from MyTable");
    query.setMaxResults(10);
    
    Criteria example: 
    session.createCriteria(Abc.class)
                        .addOrder(Order.desc("id"))
                        .setMaxResults(10)
                        .list();
    

提交回复
热议问题