2017-07-09 5 views
1

でテーブルに参加:春JPA仕様私はこれは、テーブルのための私のコードである機能に参加</p> <p>春のJPAの仕様を使いたいような

  1. 表InStudent
  2. 表InParent

    public static Specification<InStudent> filterByKeywordAndStatus(final String keyword) { 
    return (Root<InStudent> root, CriteriaQuery<?> query, CriteriaBuilder cb) -> { 
        List<Predicate> predicates = new ArrayList<>(); 
    
        if (StringUtils.hasText(keyword)) { 
    
         predicates.add(
           cb.or(
             cb.like(root.get(InStudent_.name), "%" + keyword + "%"), 
             cb.like(root.get(InStudent_.address), "%" + keyword + "%"), 
             cb.like(root.get(InStudent_.phone), "%" + keyword + "%") 
           ) 
         ); 
        } 
    
        return cb.and(predicates.toArray(new Predicate[predicates.size()])); 
    }; 
    

    }

仕様内のinStudentテーブルとinParentテーブルを結合するにはどうすればよいですか?

+1

[春データJPAで2つのテーブルのエンティティを結合]の可能複製(https://stackoverflow.com/questions/19977130/を結合2テーブルエンティティ、スプリングデータjpa) –

+0

あなたのコードには関係は含まれていません。とにかく結合を使用するには 'Join parent = root.join(InStudent_.parent);'のようになります。 – Ranjeet

+0

こんにちは、答えがあなたが/ upvoteを受け入れることを忘れないで助けた場合。 – Cepr0

答えて

2

この(私が正しくあなたを理解している場合)のような何か試してみてください:

public static Specification<Student> filterByKeywordAndStatus(String keyword, String parentStatus) { 

    return (student, query, cb) -> { 

     Join<Student, Parent> joinParent = student.join("parent"); 

     return cb.and(
       cb.or(
        cb.like(student.get("name"), "%" + keyword + "%"), 
        cb.like(student.get("address"), "%" + keyword + "%"), 
        cb.like(student.get("phone"), "%" + keyword + "%") 
       ), 
       cb.like(joinParent.get("status"), "%" + parentStatus + "%")); 
    }; 
} 
関連する問題