Hibernate ManyToMany JoinTable默认OrderBy

时间:2017-09-15 07:30:04

标签: java mysql hibernate many-to-many hibernate-annotations

我使用以下注释配置了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`),  

Join Table创建如下::

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)  

如果我使用select查询,请将结果显示为::

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对其进行排序?

1 个答案:

答案 0 :(得分:1)

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(名称=&#34; back_date_entry_id&#34;)