admin管理员组

文章数量:1122846

With JPQL how can below query can be done ?

SELECT new foo.bar.MyModel(p.parentId, NVL(p.goodChild.childId, p.children.get(0).childId) ) FROM Parent p

So if the good child is not there, then just get the first child. And if there is no children then obviously null.

@Entity
class Parent {

    @Id
    Long parentId;
    
    @ManyToOne
    @JoinColumn(name = "good_child_id")
    Child goodChild;
    
    @OneToMany(mappedBy = "parent")
    List<Child> children;
}

@Entity
class Child {

    @Id
    Long childId;
    
    @ManyToOne
    Parent parent;

}

本文标签: one to manyJPQLhow to get top childStack Overflow