users@glassfish.java.net

How to Sort / OrderBy _at_OneToMany

From: <glassfish_at_javadesktop.org>
Date: Mon, 17 Sep 2007 11:00:06 PDT

I have been searching for a solution to this for several weeks on an off. I did search these forums briefly, but could not find an answer.

I am using toplink essentials to generate code from a mysql database. Objects have attributes and methods. Here is some of the code:

[u][b]Objects.java:[/b][/u]
import java.io.Serializable;
import java.util.*;
import javax.persistence.*;
@Entity
@Table(name = "objects")
@NamedQueries(
      {
    @NamedQuery(name = "Objects.findById", query = "SELECT o FROM Objects o WHERE o.id = :id order by o.id"),
    @NamedQuery(name = "Objects.findBySOAPName", query = "SELECT o FROM Objects o WHERE o.sOAPName = :sOAPName order by o.id"),
  })
public class Objects implements Serializable
{
//snip
  @OneToMany(mappedBy="owner",cascade=CascadeType.ALL)
  Collection<Objectattributes> Attributes;
//snip
  public Collection<Objectattributes> getAttributes()
  {
    return Attributes;
  }
//snip
}


[u][b]Objectattributes.java:[/b][/u]
import java.io.Serializable;
import javax.persistence.*;
@Entity
@Table(name = "objectattributes")
@NamedQueries(
      {
    @NamedQuery(name = "Objectattributes.findById", query = "SELECT o FROM Objectattributes o WHERE o.id = :id order by o.id"),
    @NamedQuery(name = "Objectattributes.findByOwner", query = "SELECT o FROM Objectattributes o WHERE o.owner = :owner order by o.id"),
    @NamedQuery(name = "Objectattributes.findBySOAPName", query = "SELECT o FROM Objectattributes o WHERE o.sOAPName = :sOAPName order by o.id"),
    @NamedQuery(name = "Objectattributes.findBySOAPType", query = "SELECT o FROM Objectattributes o WHERE o.sOAPType = :sOAPType order by o.id"),
//snip
  })
public class Objectattributes implements Serializable
{
  @Id
  @Column(name = "ID", nullable = false)
  private Integer id;

  @ManyToOne
  @JoinColumn(name = "Owner")
  private Objects owner;
//snip other fields
}

Here is the code I use to retrieve the Objectattributes:
        for (Objectattributes DBAttr:DBObj.getAttributes())
          ProcessAttribute(GenClasses.get(DBObj.getToolkitName()),DBAttr);

My problem is that the Attributes are retrieved in a random order so the generated code changes constantly. For instance, lets say there are 3 attributes: id, first_name, last_name on an object. One time I generate, they could be ordered id, last_name, first_name. The next time they could be first_name, id, last_name. This makes the files nearly useless for diffing.

I tried changing the @OneToMany relationship as follows:
  @OneToMany(mappedBy="owner",cascade=CascadeType.ALL)
  @OrderBy("id")
  Collection<Objectattributes> Attributes;

But this does not appear to work. I would actually like to sort based on a text field (method or attribute name) so that the class files contents are alphabetized.

How can I retrieve the methods or attributes in a sorted/orderby order?
[Message sent by forum member 'rmccullough' (rmccullough)]

http://forums.java.net/jive/thread.jspa?messageID=235671