JPQL中具有类型列表属性的投影 [英] Projection in JPQL with property of type list

查看:73
本文介绍了JPQL中具有类型列表属性的投影的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在JPQL上选择类型列表的属性? 例如:

How I can made a select of a property of type list on JPQL? example:

@Entity
public class Person {

    @id
    private Long id;
    private String name;
    private String lastname;
    private String birthdate;
    @OneToMany
    private List<Phone> getPhones();

    ...
}

@Entity
public class Phone {

    @id
    private Long id;
    private String number;

    ...
}

在存储库中,我需要一个投影,所以:

And on repository I want a projection, so:

public interface IPersonProjection {

    Long getId();
    String getName();
    List<Phone> phones();
}




@Repository
public interface IAtendimentoRepository extends JpaRepository<Atendimento, Long> {

    @Query("SELECT P.id, P.name, P.phones FROM Person P ")
    List<IPersonProjection> findAllProjected();
}

但是当我尝试这种操作(SELECT P.id,P.name,P.phone FROM Person P)出现SQL语法错误时.

But when I try this (SELECT P.id, P.name, P.phones FROM Person P) occur an error on syntax of SQL.

推荐答案

Spring Data Docs ,您应该在Projection Interface中具有属性的访问器.所以我认为您应该将IPersonProjectionphones方法的名称更改为getPhones.

As specified in Spring Data Docs, you should have accessors of your properties in your Projection Interface. So i think you should change the name of phones method of IPersonProjection to getPhones.

这篇关于JPQL中具有类型列表属性的投影的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆