简体   繁体   中英

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. 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.

In fact, I don't even see evidence in the log that Hibernate is even selecting from the select_items table. 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.

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.

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.

Can someone please explain what's going on?

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

@OrderBy("orderBy ASC")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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