Bonjour/Bonsoir
Je génère mes table avec JPA 2.0 et Hibernate,
dans mon schéma j'utilise une clé composite décrit par l'annotation @IdClass,
seulement la génération des colonnes ne suit pas l'ordre de déclaration du code, j'aimerai savoir si il existe une façon de dire à JPA, que tel ou tel colonne vient avant ou après celle ci, et que la composition de la clé prend en premier tel ou tel champs?
voici mon code
Code java : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 package organization.basis.domain.model.security.groups; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; import org.apache.commons.lang.builder.ToStringBuilder; import java.io.Serializable; /** * * @author Khaled.Noordin */ public class GroupAuthoritiesId implements Serializable { private static final long serialVersionUID = 1516747697065512431L; private Groups groups; private String authority; public Groups getGroups() { return groups; } public String getAuthority() { return authority; } public void setGroups(Groups groups) { this.groups = groups; } public void setAuthority(String authority) { this.authority = authority; } @Override public String toString() { return ToStringBuilder.reflectionToString(this); } @Override public int hashCode() { return HashCodeBuilder.reflectionHashCode(this); } @Override public boolean equals(Object object) { return ( !( object instanceof GroupAuthoritiesId ) || object == null ) ? false : EqualsBuilder.reflectionEquals( this, object); } }
Code java : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104 package organization.basis.domain.model.security.groups; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; import org.apache.commons.lang.builder.ToStringBuilder; import org.hibernate.annotations.Index; import javax.persistence.*; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; import javax.xml.bind.annotation.XmlRootElement; import java.io.Serializable; /** * * @author Khaled.Noordin */ @Entity @Table(name = "group_authorities") @Access(value = AccessType.PROPERTY) @IdClass(value = GroupAuthoritiesId.class) @XmlRootElement @NamedQueries({ @NamedQuery(name = "GroupAuthorities.findAll", query = "SELECT g FROM GroupAuthorities g"), @NamedQuery(name = "GroupAuthorities.findByGroups", query = "SELECT g FROM GroupAuthorities g WHERE g.groups = :groups"), @NamedQuery(name = "GroupAuthorities.findByAuthority", query = "SELECT g FROM GroupAuthorities g WHERE g.authority = :authority") }) public class GroupAuthorities implements Serializable { private static final long serialVersionUID = 3665548523131336391L; private Groups groups; private String authority; public GroupAuthorities() { this.groups = null; this.authority = null; } public GroupAuthorities( Groups groups, String authority) { this(); this.groups = groups; this.authority = authority; } public GroupAuthorities( GroupAuthoritiesId groupAuthoritiesId) { this(groupAuthoritiesId.getGroups(), groupAuthoritiesId.getAuthority()); } @Id @JoinColumn(name = "group_id", referencedColumnName = "id", insertable = false, updatable = false) @ManyToOne(optional = false) public Groups getGroups() { return groups; } @Id @Basic(optional = false) @NotNull @Size(min = 1, max = 50) @Column(name = "authority") @Index(name = "idx_group_auth_autority") public String getAuthority() { return authority; } public void setGroups(Groups groups) { this.groups = groups; } public void setAuthority(String authority) { this.authority = authority; } @Override public String toString() { return ToStringBuilder.reflectionToString(this); } @Override public int hashCode() { return HashCodeBuilder.reflectionHashCode(this); } @Override public boolean equals(Object object) { return ( !( object instanceof GroupAuthorities ) || object == null ) ? false : EqualsBuilder.reflectionEquals( this, object); } }
voici le sql généré
Code sql : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9 CREATE TABLE `group_authorities` ( `authority` varchar(255) NOT NULL, `group_id` bigint(20) NOT NULL, PRIMARY KEY (`authority`,`group_id`), KEY `idx_group_auth_autority` (`authority`), KEY `FK771BA161B605BC9F` (`group_id`), CONSTRAINT `FK771BA161B605BC9F` FOREIGN KEY (`group_id`) REFERENCES `groups` (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
le sql attendu
Code sql : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8 create table group_authorities( group_id bigint(20) unsigned not null, authority varchar(50) not null, constraint fk_group_authorities_group foreign key(group_id) references groups(id), primary key(group_id,authority) )ENGINE=InnoDB DEFAULT CHARSET=utf8;
Cordialement, Khaled
Partager