gpt4 book ai didi

java - Hibernate ManyToMany JoinTable 默认 OrderBy

转载 作者:行者123 更新时间:2023-11-29 10:37:05 24 4
gpt4 key购买 nike

我使用以下注释配置了ManyToMany关系:

@ManyToMany
@JoinTable(name="back_date_entry_project",
joinColumns={@JoinColumn(name="back_date_entry_id")},
inverseJoinColumns={@JoinColumn(name="project_id", columnDefinition="INT(10) UNSIGNED")},
foreignKey=@ForeignKey(name="fk_back_date_entry_project_back_date_entry_back_date_entry_id"),
inverseForeignKey=@ForeignKey(name="fk_back_date_entry_project_project_project_id"),
uniqueConstraints=@UniqueConstraint(columnNames={"back_date_entry_id","project_id"})
)
private Set<Project> projects;

此配置在创建联接表时添加:

KEY `fk_back_date_entry_project_project_project_id` (`project_id`),  

连接表创建如下::

mysql> show create table  back_date_entry_project ;
+-------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------------------------+------------------------------------------------------------------------------------------------------------------------------------------------+
| back_date_entry_project | CREATE TABLE `back_date_entry_project` (
`back_date_entry_id` int(10) unsigned NOT NULL,
`project_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`back_date_entry_id`,`project_id`),
KEY `fk_back_date_entry_project_project_project_id` (`project_id`),
CONSTRAINT `fk_back_date_entry_project_back_date_entry_back_date_entry_id` FOREIGN KEY (`back_date_entry_id`) REFERENCES `back_date_entry` (`back_date_entry_id`),
CONSTRAINT `fk_back_date_entry_project_project_project_id` FOREIGN KEY (`project_id`) REFERENCES `project` (`project_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

如果我使用选择查询,结果为::

mysql> select * from  back_date_entry_project ;
+--------------------+------------+
| back_date_entry_id | project_id |
+--------------------+------------+
| 1 | 65 |
| 3 | 65 |
| 2 | 85 |
| 2 | 95 |
| 1 | 99 |
+--------------------+------------+
5 rows in set (0.00 sec)

此结果按project_id排序?
如何在 sql 控制台中使用 back_date_entry_id 对此进行排序?

最佳答案

If you don't specify order by you can't be sure about order.

编辑:

@ManyToMany
@JoinTable(name="back_date_entry_project",
joinColumns={@JoinColumn(name="back_date_entry_id")},
inverseJoinColumns={@JoinColumn(name="project_id", columnDefinition="INT(10) UNSIGNED")},
foreignKey=@ForeignKey(name="fk_back_date_entry_project_back_date_entry_back_date_entry_id"),
inverseForeignKey=@ForeignKey(name="fk_back_date_entry_project_project_project_id"),
uniqueConstraints=@UniqueConstraint(columnNames={"back_date_entry_id","project_id"})
)
@OrderBy(value="nameOfTheProjectColumn asc/desc")
@OrderColumn(name="back_date_entry_id")
private Set<Project> projects;

@OrderBy 适用于项目属性。

您可以尝试使用 @OrderColumn(name="back_date_entry_id")

关于java - Hibernate ManyToMany JoinTable 默认 OrderBy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46234078/

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