---
# System prepended metadata

title: Untitled
tags: [join, manytomany, jpa]

---

###### tags: `manytomany` `join` `jpa`

```
	/**
	 * @return 有「主播」身份的号
	 */
	public static Specification<ZhangHao> camgirls() {
		return (Root<ZhangHao> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) -> {
			Root<ZhangHao> zhangHaoRoot = root;
			Subquery<JueSe> subquery = criteriaQuery.subquery(
				JueSe.class
			);
			Root<JueSe> jueSeRoot = subquery.from(JueSe.class);
			Expression<Collection<ZhangHao>> expression = jueSeRoot.get("zhangHaoMen");
			subquery.
				select(jueSeRoot).
				distinct(true).
				where(
					criteriaBuilder.equal(
						jueSeRoot.get("roleName"),
						"ROLE_CAMGIRL"
					),
					criteriaBuilder.isMember(
						zhangHaoRoot,
						expression
					)
				);
			return criteriaBuilder.exists(subquery);
		};
	}
```

https://stackoverflow.com/questions/31841471/spring-data-jpa-specification-for-a-manytomany-unidirectional-relationship