JPA2リポジトリ@QueryステートメントのLIMIT句の指定
-
21-12-2019 - |
質問
次のMySQL文を持っています。
"SELECT * FROM $this->tableName WHERE news_publication_id = '" + newsPublicationId + "' AND list_order > '" + listOrder +"' ORDER BY list_order LIMIT 1"
.
最初に休止状態DAOレイヤーを実行してデータ処理パスで起動しました。
以下の基準に基づく方法がありました:
@Override
public NewsHeading findNextWithListOrder(NewsPublication newsPublication, int listOrder) {
Criteria criteria = getSession().createCriteria(getPersistentClass());
criteria.add(Restrictions.eq("newsPublication", newsPublication));
criteria.add(Restrictions.gt("listOrder", listOrder)).addOrder(Order.asc("listOrder")).setMaxResults(1);
return (NewsHeading) criteria.uniqueResult();
}
.
そしてそれはちょうど元気に動作します。
それから私はJPA2リポジトリレイヤーをやってみました。
次のJPQLベースの方法を持っています。
@Query("SELECT nh FROM NewsHeading nh WHERE nh.newsPublication = :newsPublication AND nh.listOrder > :listOrder ORDER BY nh.listOrder ASC LIMIT 1")
public NewsHeading findByNextListOrder(@Param("newsPublication") NewsPublication newsPublication, @Param("listOrder") int listOrder);
.
しかしそれはうまくいきません。LIMIT句は無効であるためです。
だから私は何をすべきか疑問に思います。
このJPA2リポジトリ層では、上記の基準に基づく方法を使用できますか?
クライアントに制限値を渡し、その値(1)を私のレイヤーに渡すように信頼することを避けたい。
また、そのステートメントは1つのオブジェクトだけを返しますので、ページ区切りは必要ありません。
あらゆる先端はほとんど歓迎されます。
親切な敬意、
Stephane Eybert
解決
おそらくPageRequest
でこれを使用できます。 http.://docs.spring.io/spring-data/jpa/docs/1.6.2.release/Reference/html/repositories.html#repositories.core-concepts
HibernateはsetMaxResults()
オブジェクトのQuery
を実行したいが、JPAはそれと直接インタフェースしません。
その他のオプションは@NamedNativeQuery
所属していません StackOverflow