0

I have a few entity beans with sets of other entities as attributes. I want to sort them and guarantee that at every insertion they will remain sorted. I tried this way:

@Entity
@Table(name = "Document")
public class Document implements Serializable, Comparable<Document> {

    ...

    @ManyToOne
    private Project relativeProject;

    ...

    @Override
    public int compareTo(Document d) {
        long data1 = getDate().getTimeInMillis();
        long data2 = d.getDate().getTimeInMillis();
        if(data1 > data2)
            return 1;
        else
            if(data2 < data1)
                return -1;
            else
                return 0;
    }

    @Override
    public boolean equals(Object d) {
        return (getIdDocument() == ((Document)d).getIdDocument());
    }

    @Override
    public int hashCode() {
        return Long.valueOf(getIdDocument()).hashCode();
    }

}




@Entity
@Table(name="Project")
public class Project implements Serializable, Comparable<Project> {

    ...

    @OneToMany(mappedBy="relativeProject", fetch = FetchType.EAGER)
    @Sort(type = SortType.NATURAL)
    private SortedSet<Document> formedByDocuments;

    public Progetto() {
        this.formedByDocuments = new TreeSet<Document>();
    }

    ...

}

But it does not work. The problem is that, even if in the database there are all needed entries, when a session bean returns a Project there will miss some Document. Moreover, entries are not sorted at all in the database.
If I do not sort at all (using HashSet) and republish the project, everything works fine and I get all the elements in a set (but not sorted, of course).
Can someone help me to find out what's wrong with my sorting?

4
  • 1
    A small remark, but you might want to use English names in your code. This makes it much easier to share your code with others (for instance here). When I read things like 'progettoRelativo' it's like you obfuscated the code. You might as well put "jkgdklgflkg" there. I guess many others feel the same way and this might lower your chances of getting good answers. Jan 8, 2012 at 15:52
  • Sorry, I know. I thought it was still clear (Documento -> Document, Progetto -> Project) but I did not think there were also more complex words.
    – Simon
    Jan 8, 2012 at 16:00
  • Please post your equals() and hashCode() method for Document as well
    – Andre
    Jan 8, 2012 at 16:13
  • @Andre Done. I did hashCode() right now, I did not implemented it before. Still not working anyway.
    – Simon
    Jan 8, 2012 at 16:29

1 Answer 1

1

I'm assuming getIdDocument() returns an object and not a primitive.

In that case, you need to use equals and not ==

public boolean equals(Object d) {
    return (getIdDocument().equals((Document)d).getIdDocument());
}

Edit:

Looks like the problem is in the second if(data2 < data1) statement of the compareTo() method. This should be if(data1 < data2)

4
  • It is a long type, so == is fine. Anyway, I solved writing: if(data1 > data2) return 1; else return -1; in compareTo(). But I don't understand why.
    – Simon
    Jan 8, 2012 at 17:13
  • 1
    What if you simplify your compareTo()? return getDate().compareTo(d.getDate());
    – Andre
    Jan 8, 2012 at 17:21
  • Still working. Yet I don't understanding why my "manual" comparison between two dates didn't work.
    – Simon
    Jan 8, 2012 at 17:28
  • @Simon, the second if was swapped around. See edited answer.
    – Andre
    Jan 8, 2012 at 17:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.