我正在尝试转换这个原始的SQL查询:
select product.* from following_relationship
join product on following_relationship.following=product.owner_id
where following_relationship.owner=input
Run Code Online (Sandbox Code Playgroud)
进入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();
}
};
}
Run Code Online (Sandbox Code Playgroud)
我得到了这个例外:
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
Run Code Online (Sandbox Code Playgroud)
我想补充一点,我是Spring框架的新手,例如这是我春天的第一个应用程序,所以我为新手问题道歉;)
编辑:添加的实体Product,FollowingRelationShip
Entity
@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "json_id_prop")
public class FollowingRelationship extends BaseEntity {
@ManyToOne(fetch = …Run Code Online (Sandbox Code Playgroud) 我最近在Joaquim Verques的演讲中听说过Realm for android(也可用于iOS).非常有趣且非常强大的持久数据工具.
我决定在视频之后试一试,研究和阅读文档.
我发现它很容易使用和设置,但我最终陷入了我的项目中间,因为我无法成功地进行多对多关系的查询.
我为这个主题创建了一个小例子.
我的模特:
public class Feed extends RealmObject {
@PrimaryKey
private int id;
private String title;
private String description;
private String link;
private Terms terms;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
} …Run Code Online (Sandbox Code Playgroud)