Hibernate ManyToMany JoinTable Default OrderBy

1.2k views Asked by At

I have configured ManyToMany relation using following Annotation :

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

This Configuration adds key while creating Join table :

KEY `fk_back_date_entry_project_project_project_id` (`project_id`),  

Join Table is created as follows ::

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)  

If I use select query give me result as ::

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)

This result is sorted by project_id ?
How do I sort this by using back_date_entry_id in sql console ?

1

There are 1 answers

4
Lemmy On

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

EDIT:

@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 will work for Project properties.

You can try with @OrderColumn(name="back_date_entry_id")