带规格的弹簧数据连接
2022-09-01 14:13:12
我正在尝试转换此原始 sql 查询:
select product.* from following_relationship
join product on following_relationship.following=product.owner_id
where following_relationship.owner=input
进入Spring Data规范,我认为到目前为止我的问题是连接这些表。
以下是我目前在规范中的转换:
protected Specification<Product> test(final User user){
return new Specification<Product>() {
@Override
public Predicate toPredicate(Root<Product> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
Join<FollowingRelationship,Product> pfJoin = query.from(FollowingRelationship.class).join("following");
pfJoin.on(cb.equal(pfJoin.get("following"),"owner"));
return query.where(cb.equal(pfJoin.get("following"),user)).getGroupRestriction();
}
};
}
我得到这个例外:
Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessA
piUsageException: org.hibernate.hql.internal.ast.InvalidWithClauseException: with clause can only reference columns in the driving table
我想补充一点,我是Spring框架的新手,例如,这是我在Spring上的第一个应用程序,所以我对新手问题表示歉意;)
编辑:添加实体产品,关注关系
Entity
@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "json_id_prop")
public class FollowingRelationship extends BaseEntity {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "OWNER", referencedColumnName = "uuid")
private User owner;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "FOLLOWING", referencedColumnName = "uuid")
private User following;
public User getOwner() {
return owner;
}
public void setOwner(User owner) {
this.owner = owner;
}
public User getFollowing() {
return following;
}
public void setFollowing(User following) {
this.following = following;
}
}
@Entity
@Table(name = "product")
@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "json_id_prop")
public class Product extends BaseEntity {
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "OWNER_ID", referencedColumnName = "uuid")
private User owner;
@NotNull
private String name;
@NotNull
private String description;
@NotNull
private String price;
@NotNull
private String brand;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public User getOwner() {
return owner;
}
public void setOwner(User owner) {
this.owner = owner;
}
}
Product 和 FollowRelationShip 实体没有任何显式关系,因此在我的实现上加入 about。我想实现的是从所有用户那里获得所有产品,另一个用户在Spring数据规范中遵循。