JPQL:直接查询对象列表

时间:2018-07-02 11:50:23

标签: java jpa jpql java-10

我们有一个 Universe 和一个 Funkopop 。我们建立了@OneToMany关系。

@Entity
public class Universe implements HasId{

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    Integer id;

    String name;

    @OneToMany
    List<FunkoPop> funkoPops = new ArrayList<>(); 

}



@Entity
public class FunkoPop implements HasId {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id = null;
    private String name;
    private boolean waterproof = false;
}

我们想列出一个Universe中的所有funkopop。

var pops = em.createQuery("SELECT u.funkoPops FROM Universe u WHERE u.name = :universeName", 
                 FunkoPop.class)
                .setParameter("universeName", "Kung-Fury")
                .getResultList();

不幸的是,我们有:

Exception in thread "main" java.lang.IllegalArgumentException: 
Type specified for TypedQuery [io.robusta.funko.entities.FunkoPop] is incompatible with 
query return type [interface java.util.Collection]

0 个答案:

没有答案