Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.9k views
in Technique[技术] by (71.8m points)

jpa 2.0 - Spring JPA Specification Join Two Tables Only to filter the Query

I have been Trying to generate JPA2 Specification that can filter data by joining another table. The tables are

ApplicationUser{
  String userId;
}
ExternalUser{
  String userId;
}
Comment{
  String message;
  String userType; //appuser or externaluser
  String userId; // Either the user id in ApplicationUser table or ExternalUser table
  
}

Now I would like to run a query to find all Comments posted by ExternalUser with this query

select comment.* from Comment inner outer join ExternalUser on (ExternalUser.userId = Comment.userId and Comment.userType = 'externaluser')

How to query this using Jpa2 Specification wihout the User table actually having a OneToMany Mapping. The user class is already fixed so cannot change it to add the relationship.

public default Specification<Comment> search(String userType,String userId) {
  Specification<Comment> spec = (root, cq, cb) -> {
    return predicate???
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...