2017-04-19 7 views
1

私のコード構造は次のようになります。SpringデータJPAクエリで子オブジェクトによるフィルタリングでエラーが発生しました

記事:ユーザーによって読み取ら

@Entity 
public class NewsArticle{ 

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

    [Other class properties such as title, publisher, publishedDate, etc.]   

    @OneToMany(mappedBy = "article") 
    private Set<UserReadNewsArticle> userReadNewsArticles = new HashSet<>(); 

    [Getters and Setters] 
} 

記事:

@Entity 
public class UserReadNewsArticle { 

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

    private Long readAccountId;  
    private Long readArticleId;   

    @JsonIgnore 
    @ManyToOne 
    private Account account; 

    @JsonIgnore 
    @ManyToOne 
    private NewsArticle article; 

    [Getters and Setters] 


} 

アカウント:

@Entity 
public class Account { 

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

    [Other class properties] 

    @OneToMany(mappedBy = "account") 
    private Set<UserReadNewsArticle> userReadNewsArticles = new HashSet<>(); 

    [Getters and Setters] 
} 

私はすべてのニュースを読むを取得するために私NewsArticleRepositoryでクエリメソッドを持つようにしたいですユーザーのための記事。

public interface NewsArticleRepository extends PagingAndSortingRepository<NewsArticle, Long>{ 

    Collection<NewsArticle> findByUserReadNewsArticlesReadAccountId(Long readAccountId); 

} 

この方法は効果的です。しかし、どのようにしてSpring Data JPA Query/Methodを作成して、 "Unread News Articles in user"を得ることができますか?私が試したことは次のとおりです。

Collection<NewsArticle> findByUserReadNewsArticlesReadAccountIdNot(Long readAccountId); 

これは、他のユーザーが読んだ記事のリストを返します。しかし、私のの要件は、すべての未読のニュース記事を取得することです。私はSpring Data JPA Documentationを通過しましたが、より簡単な魂を思いつきませんでした。どうすればこの問題を克服できますか?または私は何か間違っているのですか?

+0

[jpa docs](http://docs.spring.io/spring-data/jpa/docs/1.7.0.M1/reference/htmlsingle/#jpa.query-methods.query-creation) 'not in'を使うと' Collection findNotInUserReadNewsArticlesByReadAccountId(Long readAccountId); 'のように表示されます。 – fiskra

+0

@fiskraこれはで動作しません、私はUserReadNewsArticlesRepositoryではなく、** NewsArticleRepository **から選択したいと思います。私はすでに** NotIn **で遊んでいますが、まだそれを解決することができませんでした。 – SIRIM4N

+0

あなたの問題とは別に、あなたは既にエンティティをバインドしているので、 'readAccountId'と' readArticleId'の両方のフィールドを削除できます。 – KLHauser

答えて

1

また、サブクエリでJPQLクエリを使用して、あなたの結果を実現できますので、最初の現在のユーザーからの読み出しarticelsを取得し、全体の記事一覧からそれらをエクスクルード

public interface NewsArticleRepository extends PagingAndSortingRepository<NewsArticle, Long> { 

    @Query("SELECT n FROM NewsArticle n WHERE n NOT IN " 
      + "(SELECT ur.article FROM UserReadNewsArticle ur JOIN ur.account a WHERE a.id = :readAccountId)") 
    Collection<NewsArticle> findByUserReadNewsArticlesReadAccountIdNotIn(@Param("readAccountId") Long readAccountId); 

} 

http://localhost:8080/newsArticles/search/findByUserReadNewsArticlesReadAccountIdNotIn?readAccountId=1

サブクエリーが明確に必要なので、私は春のデータがあなたを同じようにすることはできないと思います。私が間違っていると、誰かが私を正すことができます。

+0

私はそれを試しました。 – SIRIM4N

+0

私はいくつかの情報を追加しましたが、動作させることができませんでした。たぶんこれが役立ちます。そうでない場合、何が起こっていますか? – KLHauser

+0

実際に動作します。素晴らしい。前回私が試したときに間違いを犯したに違いない。どうもありがとうございます。 – SIRIM4N

関連する問題