-->
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.  [ 4 posts ] 
Author Message
 Post subject: Order for embedded primary keys
PostPosted: Fri Jan 19, 2007 1:28 pm 
Senior
Senior

Joined: Mon Jul 24, 2006 8:43 am
Posts: 160
Hi,
I want to specify the order for relationships where the entities in the relationships use an embedded primary key.
My Embedded Primary key encapsulates two columns 'C' and 'I'.
I wish to sort by 'C'.
I try using the @OrderBy annoation where the relationship is specified.

I use:

@ManyToMany(mappedBy="drives")
@OrderBy("C ASC")
public List<Person> getDrivers() {
return drivers;
}

I get the exception:

javax.persistence.PersistenceException: org.hibernate.AnnotationException: property from @OrderBy clause not found: Person.c

How do I orderBy a column in embedded key?
Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 21, 2007 8:59 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
how is person mapped?

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 22, 2007 7:05 am 
Senior
Senior

Joined: Mon Jul 24, 2006 8:43 am
Posts: 160
Thanks for getting back,

Here is the Person POJO

@Entity
@Table(name="TPerson")
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public class Person {

private Collection <Car> drives = new ArrayList();
protected ClassInstanceEmbPK pk; // 3009

public void setPk(ClassInstanceEmbPK pk){
this.pk = pk;
}

@EmbeddedId
public ClassInstanceEmbPK getPk() {
return pk;
}


public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("Person is, ");
sb.append(this.getPk());
return sb.toString();
}

@ManyToMany
@JoinTable(name="RPERSONCARDRIVES", joinColumns={@JoinColumn(name="C_FROM", referencedColumnName="C"),
@JoinColumn(name="I_FROM", referencedColumnName="I")},
inverseJoinColumns={@JoinColumn(name="C_TO", referencedColumnName="C", unique=false),
@JoinColumn(name="I_TO", referencedColumnName="I", unique=false)})
public Collection<Car> getDrives() {
return drives;
}

public void setDrives(Collection<Car> drives){
this.drives = drives;
}

}


Here is the ClassInstance embedded key,

@Embeddable
public class ClassInstanceEmbPK implements Comparable, Serializable {

private int c;
private int i;
public static int counter = 0;
public ClassInstanceEmbPK() {
}

public ClassInstanceEmbPK(int c, int i) {
this.c = c;
this.i = i;
}

public int getC() {
return c;
}


public void setC(int c){
this.c = c;
}

public int getI() {
return i;
}


public void setI(int i){
this.i = i;
}

public boolean equals(Object obj){
if (obj == this) return true;
if (!(obj instanceof ClassInstancePK)) return false;
ClassInstanceEmbPK ciPK = (ClassInstanceEmbPK)obj;
if ((ciPK.c == this.c) && (ciPK.i == this.i)){
return true;
} return false;
}

public int hashCode() {
return c + i;
}

public int compareTo(Object o) {
ClassInstanceEmbPK pk = (ClassInstanceEmbPK)o;

if (this.c > pk.c)
return 1;
else if (this.c < pk.c)
return -1;
// Check IndexIds
else if (this.i < pk.i)
return -1;
else if (this.i > pk.i)
return 1;
else
return 0;
}

public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("C=" + c);
sb.append(",I="+ i);
return sb.toString();
}

}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 24, 2007 3:44 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Code:
@OrderBy("pk.c ASC")
should do the trick

_________________
Emmanuel


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

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.