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