(Java 8を使用)SpringデータJpaRepositoryインタフェースを再利用する方法
私は、永続化レイヤで使用するSpring Data JPAリポジトリインタフェースを持っています。
package com.bla.bla.persistence;
import com.bla.bla..EntityA;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Transactional
public interface TableAJPARepository extends JpaRepository<EntityA, Long> {
@Query("select distinct i.username from EntityA i")
List<Integer> findDistinctUsernames();
@Modifying
@Query("update EntityA i set i.firstName = ?3 where i.lastName = ?1 and i.age < ?2")
int updateEntityA(Integer age, String firstName, String lastName);
@Query("select i from EntityA i where i.firstName = ?1 and (i.age =?2 or i.lastName =?3)")
List<EntityA> findByFirstNameAndAgeOrLastName(String firstName, Integer age, String lastName);
@Query("select DISTINCT(i) from EntityA i, EntityA1 r where r.bookId=i.id " +
"and i.firstName=?1 and r.title=?2 and i.lastName=?3 and i.age!=2 and i.age<=?4 " +
"order by i.firstName Desc, i.age DESC ")
List<EntityA> findBySomeFields(String firstName, String title, String lastName, Integer age, Pageable pageable);
...
}
クエリのいくつかは、ヘルパー表のA1に参加:それはここではインターフェースのコードは、テーブルA.
を担当しています。
これで、テーブルAと同じ名前のテーブルBが作成されました。表Bは、表A1と同じジョインに対して表B1を使用します。
インターフェイスには多くのメソッドがあり、コードを複製するのは嫌です。
Idどこの方法でも、正確にTableAJPARepository
のようなインターフェイスを作成できますが、代わりにテーブルBとB1を扱うのですか?
なぜこれらのメソッドがたくさんありますか?あなたがJPAをバイパスしたいように思えます(そして、Springは、あなたがそれを指定する必要がなく、あなたのためにクエリを生成します)。 –
[SpEL式](http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query.spel-expressions)を使用してください。 – manish
@ M.Deinum、これらは単なる例にすぎません。他のクエリには結合と順序付けがあります。もう1つの問題は、結合表 'A1'を表 'B1'に変更することです。 – summerbulb