【问题标题】:Hibernate ManyToMany JoinTable Default OrderByHibernate ManyToMany JoinTable 默认 OrderBy
【发布时间】:2017-09-15 07:30:03
【问题描述】:

我已经使用以下注释配置了多对多关系:

@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:

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)  

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

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

【问题讨论】:

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


    【解决方案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(name="back_date_entry_id")

    【讨论】:

    • 是的,但我想在配置中这样做......这也应该反映在 sql 控制台中
    • 您可以使用 OrderBy 或 OrderColumn 注释。我编辑了我的答案
    • 好吧对不起,但我忘了说,我都试过了,这些都会反映在 HQL 查询中我猜不是在 sql 控制台\
    • 让我试试`@OrderColumn(name="back_date_entry_id")`
    猜你喜欢
    • 2012-06-03
    • 1970-01-01
    • 2014-01-09
    • 2013-10-23
    • 1970-01-01
    • 1970-01-01
    • 2021-01-30
    • 2021-09-08
    • 2015-10-15
    相关资源
    最近更新 更多