私のコード構造は次のようになります。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を通過しましたが、より簡単な魂を思いつきませんでした。どうすればこの問題を克服できますか?または私は何か間違っているのですか?
[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
@fiskraこれはで動作しません、私はUserReadNewsArticlesRepositoryではなく、** NewsArticleRepository **から選択したいと思います。私はすでに** NotIn **で遊んでいますが、まだそれを解決することができませんでした。 – SIRIM4N
あなたの問題とは別に、あなたは既にエンティティをバインドしているので、 'readAccountId'と' readArticleId'の両方のフィールドを削除できます。 – KLHauser