我有一个设置,许多所有者可以拥有猫,每个主人可以拥有几只猫。鉴于此,我想编写一个规范来帮助我找到具有给定所有者名称的所有猫。
这是一个简单的类设置。
@Entity
public class Cat extends AbstractEntity {
@Column
private String name;
}
*没有getters / setters的简洁性。 Id字段位于超类中。
@Entity
public class Owner extends AbstractEntity {
@Column
private String name;
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "OWNER_2_CATS",
joinColumns = @JoinColumn(name = "OWNER_ID"),
inverseJoinColumns = @JoinColumn(name = "CAT_ID"))
@OrderColumn(name = "order_column")
private List<Cat> cats = Lists.newArrayList();
}
*没有getters / setters的简洁性。 Id字段位于超类中。
这是一个存储库,其中包含一个可以正常工作的查询和一个不起作用的规范。
public interface CatRepository extends AtomicsRepository<Cat, Long> {
// This query works.
@Query("SELECT c FROM Owner o INNER JOIN o.cats c WHERE o.name = ?")
List<Cat> findAllByOwner(String ownerName);
// But how do I accomplish this in a specification?
public static class Specs {
static Specification<Cat> hasOwnerName(final String ownerName) {
return (root, query, cb) -> {
// These next lines don't work! What do I put here?
Root<Owner> owner = query.from(Owner.class);
owner.join("cats");
return cb.equal(owner.get("name"), ownerName);
};
}
}
}
请帮我写规范。
我遇到的麻烦是,这种关系似乎需要与表达关系的方式相悖:一个拥有者有一个cats
列表,但一只猫没有一个{ {1}}列表。
答案 0 :(得分:12)
此规范的棘手之处在于您查询的 Cat 与所有者没有直接关系。
一般的想法是:
我首选的方法是使用子查询来介绍所有者:
// Subquery using Cat membership in the Owner.cats relation
public static class Specs {
static Specification<Cat> hasOwnerName(final String ownerName) {
return (root, query, cb) -> {
query.distinct(true);
Root<Cat> cat = root;
Subquery<Owner> ownerSubQuery = query.subquery(Owner.class);
Root<Owner> owner = ownerSubQuery.from(Owner.class);
Expression<Collection<Cat>> ownerCats = owner.get("cats");
ownerSubQuery.select(owner);
ownerSubQuery.where(cb.equal(owner.get("name"), ownerName), cb.isMember(cat, ownerCats));
return cb.exists(ownerSubQuery);
};
}
}
哪个Hibernate 4.3.x生成SQL查询,如:
select cat0_.id as id1_1_
from cat cat0_
where
exists (
select owner1_.id from owner owner1_ where
owner1_.name=?
and (cat0_.id in (
select cats2_.cat_id from owner_2_cats cats2_ where owner1_.id=cats2_.owner_id
))
)
另一种方法是使用笛卡尔积来引入所有者:
// Cat membership in the Owner.cats relation using cartesian product
public static class Specs {
static Specification<Cat> hasOwnerName(final String ownerName) {
return (root, query, cb) -> {
query.distinct(true);
Root<Cat> cat = root;
Root<Owner> owner = query.from(Owner.class);
Expression<Collection<Cat>> ownerCats = owner.get("cats");
return cb.and(cb.equal(owner.get("name"), ownerName), cb.isMember(cat, ownerCats));
};
}
}
哪个Hibernate 4.3.x生成SQL查询,如:
select cat0_.id as id1_1_
from cat cat0_
cross join owner owner1_
where
owner1_.name=?
and (cat0_.id in (
select cats2_.cat_id from owner_2_cats cats2_ where owner1_.id=cats2_.owner_id
))
答案 1 :(得分:0)
您可以使用子查询使IN谓词获取cat的id值。
public static class Specs {
static Specification<Cat> hasOwnerName(final String ownerName) {
return (root, query, cb) -> {
//EntityType<Cat> Cat_ = root.getModel();
final Subquery<Long> queryOwner = query.subquery(Long.class);// Check type of the Cat's ID attribute
final Root<Owner> aliasOwner = queryOwner.from(Owner.class);
//EntityType<Owner> Owner_ = aliasOwner.getModel();
//final Join<Owner, Cat> aliasCatsOwner = aliasOwner.join(Owner_.cats);
final Join<Owner, Cat> aliasCatsOwner = aliasOwner.join("cats");
//queryOwner.select(aliasCatsOwner.<Long> get(Cat_.id)));
queryOwner.select(aliasCatsOwner.<Long> get("id")));// Check type and name of the Cat's ID attribute
queryOwner.where(cb.equal(Owner.<String> get("name"), ownerName));
//return cb.in(root.get(Cat_.id).value(queryOwner);
return cb.in(root.get("id").value(queryOwner);//check the name of ID attribute!
};
}
}
答案 2 :(得分:0)
简短的回答就是这样:
Page<Cat> findByOwner_Name(String ownerName, Pageable p);
但困难在于如何查询那些不属于任何人的猫,等于
select * from cat where cat.id NOT IN (select cat.id from owner_has_cat);
有人有想法吗?