2017-09-15 4 views
0

J'ai configuré relation ManyToMany à l'aide Annotation suivante:ManyToMany joinTable Par défaut OrderBy

@ManyToMany 
@JoinTable(name="back_date_entry_project", 
    joinColumns={@JoinColumn(name="back_date_entry_id")}, 
    inverseJoinColumns={@JoinColumn(name="project_id", columnDefinition="INT(10) UNSIGNED")}, 
    [email protected](name="fk_back_date_entry_project_back_date_entry_back_date_entry_id"), 
    [email protected](name="fk_back_date_entry_project_project_project_id"), 
    [email protected](columnNames={"back_date_entry_id","project_id"}) 
) 
private Set<Project> projects; 

Cette configuration ajoute clé tout en créant la table d'inscription:

KEY `fk_back_date_entry_project_project_project_id` (`project_id`), 

Joignez-vous à table est créée comme suit ::

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) 

Si j'utilise s élu requête me donne comme résultat ::

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) 

Ce résultat est trié par project_id?
Comment trier cela en utilisant back_date_entry_id dans la console SQL?

Répondre

1

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")}, 
    [email protected](name="fk_back_date_entry_project_back_date_entry_back_date_entry_id"), 
    [email protected](name="fk_back_date_entry_project_project_project_id"), 
    [email protected](columnNames={"back_date_entry_id","project_id"}) 
) 
@OrderBy(value="nameOfTheProjectColumn asc/desc") 
@OrderColumn(name="back_date_entry_id") 
private Set<Project> projects; 

@OrderBy fonctionnera pour les propriétés du projet.

Vous pouvez essayer avec @OrderColumn (name = « back_date_entry_id »)

+0

oui, mais je veux faire dans la configuration .. et qui devrait refléter dans la console SQL trop –

+0

vous pouvez utiliser OrderBy ou annotation OrderColumn. J'ai édité ma réponse –

+0

Ok désolé, mais j'ai oublié de dire, j'ai essayé les deux, ceux-ci refléteront dans la requête HQL je suppose pas à sql console \ –