gpt4 book ai didi

java - Spring Boot 2 @OrderBy 被忽略

转载 作者:行者123 更新时间:2023-12-01 18:48:17 25 4
gpt4 key购买 nike

我有两个实体:

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;
}

在选择与 Field 实例关联的 SelectItems 时,Hibernate 似乎忽略了 Field 类中的 @OrderBy 注释。我知道这一点是因为 1) 排序不是根据 orderBy 值进行的,2) 当我故意拼错 @OrderBy 注释中的值时,一切仍然有效。

事实上,我什至没有在日志中看到 Hibernate 甚至从 select_items 表中进行选择的证据。一定是因为当我查看从存储库返回的值时,SelectItems 就在那里,但查询没有像所有其余查询那样打印在日志中。

这是我的日志:

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=?

日志中没有后续的 select 语句可以拉出嵌套的 SelectItem 实例 - 但当我取回对象时它们就在那里。

所以不知何故它们被选择了,但似乎 hibernate 并没有这样做,并且无论选择它们是什么都会忽略 @OrderBy 注释。

有人可以解释一下发生了什么事吗?

最佳答案

缺少特定订单类型ASC或DESC,直接通过字段名称调用。

@OrderBy("orderBy ASC")

关于java - Spring Boot 2 @OrderBy 被忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59777899/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com