计算实体子项的 CriteriaQuery 生成格式错误的数字常量: 。错误

CriteriaQuery to count children of an entity produces malformed numeric constant: . error

我在数据库中有以下 table。在我的 Java 代码中全部使用 Hibernate 注释表达。

|LibraryItem Table             |
|LibraryItemId|LibraryItemTitle|

|ItemListing Table                              |
|ListingId|ChildLibrayItemId|ParentLibraryItemId|

所以基本上都有图书馆的资料。每个图书馆项目可能是另一个图书馆项目的子项或父项,并且这种关系存储在 Itemlisting table 中。

我正在尝试使用 CriteriaBuilder 方法获取特定图书馆项目的所有子项的计数。这是我的代码:

public int getNumChildren(LibraryItem libItem) {
        CriteriaBuilder builder = sessionFactory.getCriteriaBuilder();
        CriteriaQuery<Long> query = builder.createQuery(Long.class);
        Root<LibraryItem> root = query.from(LibraryItem.class);
        query.select(builder.count(root.get("itemChildren")));
        query.where(builder.equal(root.get("libraryItemId"), libItem.getLibraryItemId()));
        return Math.toIntExact(sessionFactory.getCurrentSession().createQuery(query).uniqueResult());
    }

这会产生以下错误:

java.sql.SQLSyntaxErrorException: malformed numeric constant: . in statement [select count(.) as col_0_0_ from library_item libraryite0_, ITEM_LISTING itemchildr1_, library_item libraryite2_ where libraryite0_.LIBRARY_ITEM_ID=itemchildr1_.PARENT_LIB_ITEM_ID and itemchildr1_.CHILD_LIB_ITEM_ID=libraryite2_.LIBRARY_ITEM_ID and libraryite0_.LIBRARY_ITEM_ID=4601]

有人可以向我解释一下我做错了什么吗?

编辑:

这是实体 class。我省略了一些我认为无关紧要的代码:

@Entity
@Table(name = "library_item", uniqueConstraints = {
        @UniqueConstraint(columnNames={"LIBRARY_ITEM_TITLE", "LIBRARY_ID"})

})
public class LibraryItem extends DatabaseObject {

    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "hilo_sequence_generator")
    @GenericGenerator(
            name = "hilo_sequence_generator",
            strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator",
            parameters = {
                    @org.hibernate.annotations.Parameter(name = "sequence_name", value = "hilo_seqeunce"),
                    @org.hibernate.annotations.Parameter(name = "initial_value", value = "1"),
                    @org.hibernate.annotations.Parameter(name = "increment_size", value = "100"),
                    @org.hibernate.annotations.Parameter(name = "optimizer", value = "hilo")
            })
    @Id
    @Column(name = "LIBRARY_ITEM_ID", unique = true, nullable = false)
    private Long libraryItemId;

    @Column(name = "LIBRARY_ITEM_TITLE", nullable = false)
    private String libraryItemTitle;

    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinTable(name = "ITEM_LISTING",
            joinColumns = {@JoinColumn(name = "PARENT_LIB_ITEM_ID", nullable=false)},
            inverseJoinColumns = {@JoinColumn(name="CHILD_LIB_ITEM_ID", nullable = false)})
    private Set<LibraryItem> itemChildren = new HashSet<>();

    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "itemChildren")
    private Set<LibraryItem> itemParents = new HashSet<>();
}

您正在对多值字段使用 count。这在 JPQL 中是无效的,因此在 Criteria 中也是如此。

这样做的方法是使用 size 函数 (CriteriaBuilder.size),它明确用于集合字段。