JPA中的包含查询:MEMBER OF
爱好表
@Entry
@Table(name="tb_hobby")
public class Hobby {
/**
* 关键字
*/
public String val;
}
用户表
@Entry
@Table(name="tb_user")
public class User {
/**
* 用户名
*/
public String name;
/**
* 用户爱好
*/
@ManyToMany
@JoinTable(name = "nexus_user_hobby",
joinColumns = {@JoinColumn(name = "user_id", referencedColumnName = "id")},
inverseJoinColumns = {@JoinColumn(name = "hobby_id", referencedColumnName = "id")})
public List<Hobby> hobbys;
}
现有关系如下
如果要查询爱好包含飙车的用户,则可使用以下查询:
Hobby hobby = Hobby.find("byVal","飙车").first();
assert hobby != null;
List<User> users = User.find("?1 MEMBER OF hobbys",hobby).fetch();