-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Defining the column generation order
PostPosted: Sat Jul 16, 2011 11:09 am 
Newbie

Joined: Sat Jul 16, 2011 10:15 am
Posts: 2
Hello
I'm generating my tables with JPA 2.0 and hibernate 3.6.5,
in my schema I use a composite key describe by the @IdClass annotation,
but the column generation don't follow the order given by the code,
I would like to know if there is a way to tell to the ORM in my case hibernate through JPA,
that this column or that column comes after or before another,
or simply to stricly follow the order given by the code,
and especially for the order of column's keys in a composite key

here is my 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);
}
}


generated sql
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;


expected sql
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;


Best regards, Khaled


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.