3

I have this entity

@Entity
public class RentAmount {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long rentAmountId;

    private BigDecimal unpaidBalance;

    private BigDecimal cashAdvance;

    private BigDecimal totalRentAmount;

    private LocalDate paymentRentDueDate;

    private LocalDate paymentDueDate;

    @OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.LAZY)
    @OrderBy
    private Set<RoomPayment> roomPaymentList;

    @OneToOne
    private Lodger lodger;
}

My query

select r from RentAmount r Join fetch r.lodger l join fetch l.bailList b join fetch r.roomPaymentList p where r.paymentRentDueDate <= :date and b.paymentPeriod=:paymentPeriod order by r.rentAmountId

RoomPaymentList can be very huge, i search a way to limit the number of record returned.

Is there a way to limit the number of value from roomPaymentList?

robert trudel
  • 5,283
  • 17
  • 72
  • 124

3 Answers3

4

If I understand you right - you want to limit number of RoomPaymentList rows for each RentAmount, but not limit rows number in result set. If so, ther is no such way in JPA. But you can write some native query or stored procedure and limit it in subquery etc (Example of such query for MS SQL).

Community
  • 1
  • 1
Javasick
  • 2,753
  • 1
  • 23
  • 35
0

If you have a plain MySql Query you can do that by appending

Limit 5;

to your query. Oracle offers the ROWNUM-alternative:

ROWNUM <= 5;

(as described here http://www.w3schools.com/sql/sql_top.asp).

Christian
  • 303
  • 2
  • 15
0

This might be help for you to get the 10 rows using the jpa query.

String hql = "select r from RentAmount r Join fetch r.lodger l join fetch l.bailList b join fetch r.roomPaymentList p where r.paymentRentDueDate <= :date and b.paymentPeriod=:paymentPeriod order by r.rentAmountId";
Query query = session.createQuery(hql);
query.setParameter("date", "date");
query.setParameter("paymentPeriod", "paymentPeriod");
List results = query.list();

// If you want to fetch only 10 rows then
query.setMaxResults(10); //it takes only an integer parameter
Sudhakar
  • 3,104
  • 2
  • 27
  • 36