HSQL查询在连接部分使用限制

时间:2017-07-05 09:40:31

标签: spring hibernate spring-boot

我是Spring Boot的新手。我试图在SO和谷歌上找到我的问题的答案,但我无法找到答案。

我试图在我的Spring Boot JpaRepository类中创建一个函数,该类通过id返回一个Customer,其中包含与客户相关的有限数量的升序有序AccountingLog。

我在JpaRepository中的代码行:

@Query("select c from Customer left outer join AccountingLog a on a.customer.id = c.id where c.id= :id")
Customer getWithLimitedNumberOfLastTransactions(@Param("id") Long id, @Param("limit") int limit);

Customer类仅包含相关代码:

@Entity
public class Customer {        
    //...
    @OneToMany(mappedBy = "customer", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @Column(nullable = true)
    @JsonManagedReference
    private Set<AccountingLog> accountingLogs;
    //...
}

只有相关代码的客户类:

@Entity
public class AccountingLog {
    //...
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "customer_id")
    @JsonBackReference
    private Customer customer;
    //...
}

所以我正在寻找一个HSQL查询,它通过id选择一个客户,并按照与客户相关的升序(最后一个)计算会计日志的指定数量(变量名称限制)。

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

如果您使用的是MySql,则可以在@Query中编写本机查询:

@Query(value="select c from Customer left outer join AccountingLog a on a.customer.id = c.id where c.id= :id limit :limit", nativeQuery = true)

或Oracle:

@Query(value="select c from Customer left outer join AccountingLog a on a.customer.id = c.id where c.id= :id and ROWNUM < :limit", nativeQuery = true)