简体   繁体   English

Spring Boot 2 @OrderBy 被忽略

[英]Spring Boot 2 @OrderBy being ignored

I have two entities:我有两个实体:

public class Field {
    private String inputType;

    @OneToMany(cascade = CascadeType.ALL,
            fetch = FetchType.LAZY,
            mappedBy = "field")
    @OrderBy("order_by")
    private List<SelectItem> selectItems;
}

public class SelectItem {
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "field_id", nullable = false)
    @OnDelete(action = OnDeleteAction.CASCADE)
    private Field field;

    private int orderBy;
}

It seems that Hibernate is ignoring the @OrderBy annotation in the Field class when selecting the SelectItems associated with a Field instance.在选择与 Field 实例关联的 SelectItems 时,Hibernate 似乎忽略了 Field 类中的@OrderBy注释。 I know this because 1) the ordering is not according to the orderBy value and 2) when I intentionally misspell the value in the @OrderBy annotation, everything still works.我知道这一点是因为 1) 排序不是根据orderBy值和 2) 当我故意拼错@OrderBy注释中的值时,一切仍然有效。

In fact, I don't even see evidence in the log that Hibernate is even selecting from the select_items table.事实上,我什至没有在日志中看到 Hibernate 甚至从select_items表中进行选择的select_items Something must be because when I look at the values being returned from the repository, the SelectItems are in there, but the query isn't printing in the log, as all the rest of the queries do.一定是因为当我查看从存储库返回的值时, SelectItems在那里,但查询没有打印在日志中,就像所有其他查询一样。

This is from my log:这是我的日志:

Hibernate: insert into fields (created_by, modified_by, modified_date, category, disabled, hidden, input_type, is_integer, label, max, min, name, is_number, order_by, positive_number, required, suffix, uuid, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into select_items (descr, field_id, order_by, selected, uuid, id) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into select_items (descr, field_id, order_by, selected, uuid, id) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into select_items (descr, field_id, order_by, selected, uuid, id) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into select_items (descr, field_id, order_by, selected, uuid, id) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into select_items (descr, field_id, order_by, selected, uuid, id) values (?, ?, ?, ?, ?, ?)
Hibernate: select field0_.id as id1_18_, field0_.created_by as created_2_18_, field0_.created_date as created_3_18_, field0_.modified_by as modified4_18_, field0_.modified_date as modified5_18_, field0_.category as category6_18_, field0_.disabled as disabled7_18_, field0_.hidden as hidden8_18_, field0_.input_type as input_ty9_18_, field0_.is_integer as is_inte10_18_, field0_.label as label11_18_, field0_.max as max12_18_, field0_.min as min13_18_, field0_.name as name14_18_, field0_.is_number as is_numb15_18_, field0_.order_by as order_b16_18_, field0_.positive_number as positiv17_18_, field0_.required as require18_18_, field0_.suffix as suffix19_18_, field0_.uuid as uuid20_18_ from fields field0_ where field0_.category=? and field0_.name=?

There is no subsequent select statement in the log that would pull the nested SelectItem instances--yet they are there when I get the object back.日志中没有后续的 select 语句会拉出嵌套的SelectItem实例——但是当我取回对象时它们就在那里。

So somehow they are being selected, but it doesn't appear that hibernate is doing it, and whatever is selecting them is ignoring the @OrderBy annotation.因此,不知何故,它们被选中,但似乎 hibernate 并没有这样做,无论选择它们,都忽略了@OrderBy注释。

Can someone please explain what's going on?有人可以解释一下发生了什么吗?

缺少特定order type ASC or DESC并直接按字段名称调用它。

@OrderBy("orderBy ASC")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM