Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring数据JPA规范-@OneToMany依赖项_Spring_Jpa_Criteria Api_Specifications_Spring Data Jpa - Fatal编程技术网

Spring数据JPA规范-@OneToMany依赖项

Spring数据JPA规范-@OneToMany依赖项,spring,jpa,criteria-api,specifications,spring-data-jpa,Spring,Jpa,Criteria Api,Specifications,Spring Data Jpa,我在使用SpringDataJPA规范从EntityPerson获取列表时遇到问题(因为分页)。我需要按个人获取所有笔记,但这两个实体之间的依赖关系在个人方面。我不知道如何创建谓词,因为Note不包含任何与Person相关的属性 我可以简单地使用personsgetter获取列表,但我不能使用这种方式,因为我需要对返回的数据进行分页 @Entity public class Person implements Serializable { @Id private Long pe

我在使用SpringDataJPA规范从EntityPerson获取列表时遇到问题(因为分页)。我需要按个人获取所有笔记,但这两个实体之间的依赖关系在个人方面。我不知道如何创建谓词,因为Note不包含任何与Person相关的属性

我可以简单地使用personsgetter获取列表,但我不能使用这种方式,因为我需要对返回的数据进行分页

@Entity
public class Person implements Serializable {

    @Id
    private Long personId;

    @OneToMany
    @JoinColumn(name = "personId")
    private List<Note> notes;

}

@Entity
public class Note implements Serializable {

    @Id
    private Long noteId;
}
@实体
公共类Person实现可序列化{
@身份证
私人长人形;
@独身癖
@JoinColumn(name=“personId”)
私人名单说明;
}
@实体
公共类Note实现了可序列化{
@身份证
私有长noteId;
}
通常,我会写这样的东西,但我在Note中没有person属性,在这个阶段数据库不能被重新映射

public static Specification<Note> notesByPerson(final Long personId) {
        return new Specification<Note>() {
            @Override
            public Predicate toPredicate(final Root<Note> root, final CriteriaQuery<?> query,
                    final CriteriaBuilder builder) {

                final Path<Person> per = root.<Person> get("person");

                return builder.equal(per.<Long> get("personId"), personId);

            }
        };
    }
公共静态规范notesByPerson(最终长personId){
返回新规范(){
@凌驾
公共谓词toPredicate(最终根、最终标准查询、,
最终标准(生成器){
最终路径per=root.get(“person”);
返回builder.equal(per.get(“personId”),personId);
}
};
}
谢谢,,
Zdend

我不知道如何使用谓词,因为我通常不使用它们,但在JPQL(或HQL,类似)中,您可以这样做:

SELECT Note n FROM Person.notes WHERE XXXX
这与在SQL中执行此操作基本相同

SELECT n.noteId FROM person as p JOIN persons_notes pn ON pn.person=p.personId JOIN notes as n ON n.noteId=pn.noteId
我大胆猜测谓词方法具有与上述类似的功能。

已解决

public static Specification<Note> notesByPerson(final Long personId) {
        return new Specification<Note>() {

            @Override
            public Predicate toPredicate(final Root<Note> noteRoot, final CriteriaQuery<?> query,
                    final CriteriaBuilder cb) {

                final Subquery<Long> personQuery = query.subquery(Long.class);
                final Root<Person> person = personQuery.from(Person.class);
                final Join<Person, Note> notes = person.join("notes");
                personQuery.select(notes.<Long> get("noteId"));
                personQuery.where(cb.equal(person.<Long> get("personId"), personId));

                return cb.in(noteRoot.get("noteId")).value(personQuery);
            }
        };
    }
公共静态规范notesByPerson(最终长personId){
返回新规范(){
@凌驾
公共谓词toPredicate(最终根noteRoot、最终准则查询、,
最终标准(cb){
final Subquery personQuery=query.Subquery(Long.class);
最终根person=personQuery.from(person.class);
最终加入注释=person.Join(“注释”);
personQuery.select(notes.get(“noteId”);
personQuery.where(cb.equal(person.get(“personId”),personId));
返回cb.in(noteRoot.get(“noteId”)).value(personQuery);
}
};
}

这就是问题所在。。我不能像这样使用查询,因为我需要对它进行分页,并且我们的实现只能处理作为查询参数的规范。另外,我对谓词还不熟悉,所以我不知道它们能做什么。对于分页,您可以使用上面的方法或通过CriteriaBuilder(读取谓词)生成的TypedQuery,然后调用setFistResult()调用page*returnCount,调用setMaxResults()调用returnCount。如果你需要一个总数,我相信大多数人只是调用同一个查询,一次获取数据,另一次执行“count()”。你能进一步解释解决方案吗?(详细说明步骤)必须有更好的方法,而不是将其与第条中的方法复杂化。。。。你应该考虑添加<代码> @多个人的人;<代码>在你的笔记类中,这样你就可以做一个简单的连接。我知道这是多年前的事了。但首先要解释他们的答案。创建一个子查询到映射了关联的一侧。这就是Person,子查询的类型是Long,因为它只获取notes行的pk,以便返回id列表,作为in子句在notes根主查询中使用。2) 关于我上面的评论,添加了manytone映射,我同意,但是要小心,因为manytone默认为急切获取,因此即使另一个用例不需要该数据,它也将始终返回。