我已经使用以下注释配置了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;


此配置在创建Join表时添加键:

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将适用于Project属性。

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

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

10-12 04:56