openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Craig L Russell <Craig.Russ...@Sun.COM>
Subject Re: compareTo of Comparable not being called
Date Wed, 24 Oct 2007 17:43:46 GMT
Hi Julien,

By my reading, the specification doesn't allow the primary key to be  
explicitly named by the @OrderBy annotation, even though it's the  
default for ordering. This allows the implementation to have the  
database do the proper ordering via the SQL statement executed when  
the association List is constructed.

You might have to implement the ordering algorithm in the @OrderBy  
annotation by naming the fields and the ordering you want, as in

@OrderBy ("internalReference DESC")

Craig

On Oct 24, 2007, at 6:54 AM, Julien Martin wrote:

> Hello,
>
> I have the following PK class:
>
> [code]
> /*
>  * AmountPK.java
>  *
>  * Created on Oct 19, 2007, 6:59:36 PM
>  *
>  * To change this template, choose Tools | Templates
>  * and open the template in the editor.
>  */
> package com.calyonfinancial.cdr.entities;
>
> import java.io.Serializable;
> import javax.persistence.Column;
> import javax.persistence.Embeddable;
>
> /**
>  *
>  * @author jumartin
>  */
> @Embeddable
> public class AmountPK implements Serializable, Comparable {
>
>     @Column(name = "AMOUNT_CATEGORY_CODE", nullable = false)
>     private String amountCategoryCode;
>     @Column(name = "STATEMENT_ID", nullable = false)
>     private int statementId;
>     @Column(name = "INTERNAL_REFERENCE", nullable = false)
>     private int internalReference;
>     @Column(name = "AMOUNT_TYPE_CODE", nullable = false)
>     private char amountTypeCode;
>
>     public AmountPK() {
>         System.out.println("AmountPK constructor");
>     }
>
>     public AmountPK(String amountCategoryCode, int statementId, int
> internalReference, char amountTypeCode) {
>         this.amountCategoryCode = amountCategoryCode;
>         this.statementId = statementId;
>         this.internalReference = internalReference;
>         this.amountTypeCode = amountTypeCode;
>     }
>
>     public String getAmountCategoryCode() {
>         return amountCategoryCode;
>     }
>
>     public void setAmountCategoryCode(String amountCategoryCode) {
>         this.amountCategoryCode = amountCategoryCode;
>     }
>
>     public int getStatementId() {
>         return statementId;
>     }
>
>     public void setStatementId(int statementId) {
>         this.statementId = statementId;
>     }
>
>     public int getInternalReference() {
>         return internalReference;
>     }
>
>     public void setInternalReference(int internalReference) {
>         this.internalReference = internalReference;
>     }
>
>     public char getAmountTypeCode() {
>         return amountTypeCode;
>     }
>
>     public void setAmountTypeCode(char amountTypeCode) {
>         this.amountTypeCode = amountTypeCode;
>     }
>
>     @Override
>     public int hashCode() {
>         int hash = 0;
>         hash += (amountCategoryCode != null ?  
> amountCategoryCode.hashCode()
> : 0);
>         hash += (int) statementId;
>         hash += (int) internalReference;
>         hash += (int) amountTypeCode;
>         return hash;
>     }
>
>     @Override
>     public boolean equals(Object object) {
>         // TODO: Warning - this method won't work in the case the  
> id fields
> are not set
>         if (!(object instanceof AmountPK)) {
>             return false;
>         }
>         AmountPK other = (AmountPK) object;
>         if ((this.amountCategoryCode == null &&  
> other.amountCategoryCode !=
> null) || (this.amountCategoryCode != null &&
> !this.amountCategoryCode.equals(other.amountCategoryCode))) {
>             return false;
>         }
>         if (this.statementId != other.statementId) {
>             return false;
>         }
>         if (this.internalReference != other.internalReference) {
>             return false;
>         }
>         if (this.amountTypeCode != other.amountTypeCode) {
>             return false;
>         }
>         return true;
>     }
>
>     @Override
>     public String toString() {
>         return "com.calyonfinancial.cdr.entities.AmountPK 
> [amountCategoryCode="
> + amountCategoryCode + ", statementId=" + statementId + ",
> internalReference=" + internalReference + ", amountTypeCode=" +
> amountTypeCode + "]";
>     }
>
>    public int compareTo(Object o) {
>         System.out.println("compareTo");
>         AmountPK specifiedPk = (AmountPK) o;
>         int _specified = specifiedPk.internalReference;
>         int _this = this.internalReference;
>
>         if (_this - _specified < 0) {
>             System.out.println("<0");
>             return -1;
>         } else if (_this - _specified > 0) {
>             System.out.println(">0");
>             return 1;
>         } else {
>             System.out.println("=0");
>             return 0;
>         }
>     }
> }
>
> [/code]
>
> This is the relation/mapping:
>
> [code]
> @OneToMany(cascade = CascadeType.ALL, mappedBy = "statement")
>     @OrderBy("amountPK DESC")
>     private List<Amount> amountList;
> [/code]
>
> The above annotation will not call the compareTo method...
>
> Can anyone help please?
>
> Julien.

Craig Russell
Architect, Sun Java Enterprise System http://java.sun.com/products/jdo
408 276-5638 mailto:Craig.Russell@sun.com
P.S. A good JDO? O, Gasp!


Mime
View raw message