hibernate中单向@onetomanyMap中的子实体计数

mlmc2os5  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(306)

我正在从事一个项目,其中两个实体具有单向@onetomanyMap。当我尝试用此查询加载特定父id的所有子实体时- select p.childEntities from Parent p where p.id =:parentId 很好用。还要注意,这里我使用HibernateAPI进行分页,所以我只得到10,25。。。记录。在许多情况下,我只需要数个实体。现在,我正在尝试加载此查询中所有子实体的计数-

select count(p.childEntities) from Parent p where p.id =:parentId

它失败了,oracle错误代码为-ora-00936:缺少表达式
我的情况(此项目的权限较低)-我无法将实体Map更改为双向Map。而且不使用本机sql。而且我认为使用- " select p.childEntities from Parent p where p.id =:parentId " 然后仅仅获取count的size(),性能代价就很高。
项目父类中的Map一瞥-

@Entity
@Table(name = "PARENT")
public class Parent implements Serializable{

    private static final long             serialVersionUID     = 2232715856164345328L;

    private Long   id;

    private String first;

    private String second;

    private String third;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "parent_id")
    private List<Child> childEntities;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getFirst() {
        return first;
    }

    public void setFirst(String first) {
        this.first = first;
    }

    public String getSecond() {
        return second;
    }

    public void setSecond(String second) {
        this.second = second;
    }

    public String getThird() {
        return third;
    }

    public void setThird(String third) {
        this.third = third;
    }

    public List<Child> getChildEntities() {
        return childEntities;
    }

    public void setChildEntities(List<Child> childEntities) {
        this.childEntities = childEntities;
    }

    public static long getSerialversionuid() {
        return serialVersionUID;
    }

}

儿童班-

@Entity
@Table(name = "Child")
public class Child {

    private Long   id;

    private Integer number;

    private String prop1;

    private String prop2;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Integer getNumber() {
        return number;
    }

    public void setNumber(Integer number) {
        this.number = number;
    }

    public String getProp1() {
        return prop1;
    }

    public void setProp1(String prop1) {
        this.prop1 = prop1;
    }

    public String getProp2() {
        return prop2;
    }

    public void setProp2(String prop2) {
        this.prop2 = prop2;
    }
}

我还有什么选择?

ttygqcqt

ttygqcqt1#

您需要了解联接(顺便说一句,这也是您在sql中使用的):

select count(c.id) from Parent p join p.childEntities c where p.id = :parentId

相关问题