Erreur en Colllection .sort()

Résolu
ld91 - 3 mai 2013 à 10:22
 ld91 - 3 mai 2013 à 11:59
Bonjour,
je suis en cours de developper une application avec hibernate ,spring,jsf j'ai une erreur au niveau du code que je ne comprend pas
voila la methode :

public List getListRenouvellementsGaranties() {
try {
if (selectedGarantie != null) {
if (selectedGarantie.getNumSeqGar() != 0)
listRenouvellementsGaranties = new ArrayList(
renouvellementGarantieBusiness
.findListeRenouvellementsAttachésGarantie(selectedGarantie));
}
Collections.sort(listRenouvellementsGaranties);
} catch (Exception e) {
logger.error(e.getMessage());
}
return listRenouvellementsGaranties;
}



l'erreur est
Bound mismatch: The generic method sort(List<T>) of type Collections is not applicable for the arguments (List). The inferred type Action is not a valid substitute for the bounded parameter <T extends Comparable<? super T>>

et voila la classe Action
package com.bna.saga.models;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
* Action generated by hbm2java
*/
@Entity
@Table(name "action", catalog "saga")
public class Action implements java.io.Serializable {

private long numSeqAct;
private DemandeExigee demandeexigee;
private CabinetExpert cabinetexpert;
private Operation operation;
private Personne personne;
private Garantie garantie;
private Long valEstmAct;
private Date datDatAct;
private Date datFinAct;
private Date datDemAct;
private Date datEffGar;
private String refEnrCtr;
private Date datExpGar;
private String refInsGar;
private Date datEnrCtr;
private String codEtatAct;

public Action() {
}

public Action(long numSeqAct) {
this.numSeqAct = numSeqAct;
}

public Action(long numSeqAct, DemandeExigee demandeexigee,
CabinetExpert cabinetexpert, Operation operation,
Personne personne, Garantie garantie, Long valEstmAct,
Date datDatAct, Date datFinAct, Date datDemAct, Date datEffGar,
String refEnrCtr, Date datExpGar, String refInsGar, Date datEnrCtr,
String codEtatAct) {
this.numSeqAct = numSeqAct;
this.demandeexigee = demandeexigee;
this.cabinetexpert = cabinetexpert;
this.operation = operation;
this.personne = personne;
this.garantie = garantie;
this.valEstmAct = valEstmAct;
this.datDatAct = datDatAct;
this.datFinAct = datFinAct;
this.datDemAct = datDemAct;
this.datEffGar = datEffGar;
this.refEnrCtr = refEnrCtr;
this.datExpGar = datExpGar;
this.refInsGar = refInsGar;
this.datEnrCtr = datEnrCtr;
this.codEtatAct = codEtatAct;
}

@Id
@Column(name "numSeqAct", unique true, nullable = false)
public long getNumSeqAct() {
return this.numSeqAct;
}

public void setNumSeqAct(long numSeqAct) {
this.numSeqAct = numSeqAct;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "numSeqDem")
public DemandeExigee getDemandeexigee() {
return this.demandeexigee;
}

public void setDemandeexigee(DemandeExigee demandeexigee) {
this.demandeexigee = demandeexigee;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "codCaexCaexp")
public CabinetExpert getCabinetexpert() {
return this.cabinetexpert;
}

public void setCabinetexpert(CabinetExpert cabinetexpert) {
this.cabinetexpert = cabinetexpert;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "codOperOper")
public Operation getOperation() {
return this.operation;
}

public void setOperation(Operation operation) {
this.operation = operation;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "numSeqPers")
public Personne getPersonne() {
return this.personne;
}

public void setPersonne(Personne personne) {
this.personne = personne;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "numSeqGar")
public Garantie getGarantie() {
return this.garantie;
}

public void setGarantie(Garantie garantie) {
this.garantie = garantie;
}

@Column(name = "valEstmAct")
public Long getValEstmAct() {
return this.valEstmAct;
}

public void setValEstmAct(Long valEstmAct) {
this.valEstmAct = valEstmAct;
}

@Temporal(TemporalType.DATE)
@Column(name "datDatAct", length 10)
public Date getDatDatAct() {
return this.datDatAct;
}

public void setDatDatAct(Date datDatAct) {
this.datDatAct = datDatAct;
}

@Temporal(TemporalType.DATE)
@Column(name "datFinAct", length 10)
public Date getDatFinAct() {
return this.datFinAct;
}

public void setDatFinAct(Date datFinAct) {
this.datFinAct = datFinAct;
}

@Temporal(TemporalType.DATE)
@Column(name "datDemAct", length 10)
public Date getDatDemAct() {
return this.datDemAct;
}

public void setDatDemAct(Date datDemAct) {
this.datDemAct = datDemAct;
}

@Temporal(TemporalType.DATE)
@Column(name "datEffGar", length 10)
public Date getDatEffGar() {
return this.datEffGar;
}

public void setDatEffGar(Date datEffGar) {
this.datEffGar = datEffGar;
}

@Column(name "refEnrCtr", length 50)
public String getRefEnrCtr() {
return this.refEnrCtr;
}

public void setRefEnrCtr(String refEnrCtr) {
this.refEnrCtr = refEnrCtr;
}

@Temporal(TemporalType.DATE)
@Column(name "datExpGar", length 10)
public Date getDatExpGar() {
return this.datExpGar;
}

public void setDatExpGar(Date datExpGar) {
this.datExpGar = datExpGar;
}

@Column(name "refInsGar", length 50)
public String getRefInsGar() {
return this.refInsGar;
}

public void setRefInsGar(String refInsGar) {
this.refInsGar = refInsGar;
}

@Temporal(TemporalType.DATE)
@Column(name "datEnrCtr", length 10)
public Date getDatEnrCtr() {
return this.datEnrCtr;
}

public void setDatEnrCtr(Date datEnrCtr) {
this.datEnrCtr = datEnrCtr;
}

@Column(name "CodEtatAct", length 50)
public String getCodEtatAct() {
return this.codEtatAct;
}

public void setCodEtatAct(String codEtatAct) {
this.codEtatAct = codEtatAct;
}

}
merci de m'aider a comprendre ce probléme

6 réponses

cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 371
3 mai 2013 à 10:43
Salut,

Pour que ta liste soit triée, il faut que tu expliques comment comparer les objets qu'elle contient. Pour cela, il faut implémenter l'interface Comparable

public Action() implements Comparable 
3
cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 371
3 mai 2013 à 11:49
Oui, tu ne peux invoquer compareTo que sur un objet,
return this.numSeqAct - o.getNumSeqAct(); 


Je n'aime pas trop cette facon d'écrire le if, tu devrais utiliser des {}, de plus, ce if ne sert à rien, un long n'est jamais null, ce n'est pas un objet

donc, ta fonction serait :
public int compareTo(Action o) { 
   return (int)(this.numSeqAct - o.getNumSeqAct());
}
3
cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 371
3 mai 2013 à 10:46
Et ensuite, tu devrais implémenter l'unique méthode de cette interface :

public int compareTo(Action a){
   // Et c'est là que tu expliques comment trier tes actions
}


Si le résultat de cette méthode est 0 alors les deux actions sont identiques. Plus le résultat est grand, plus les actions sont différentes

L'idéal est de créer cette méthode en se servant des méthodes compareTo d'autres fonctions par exemple :
public int compareTo(Action a){
   return refEnrCtr.compareTo(a.getRefEnrCtr());
}
0
ok je vais le faire mercii
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
j'ai fait comme ça

public int compareTo(Action o) {
if (this.numSeqAct != null && o.getNumSeqAct() != null)
return this.numSeqAct.compareTo(o.getNumSeqAct());
return -1;
}
j'ai cette erreur
Cannot invoke compareTo(long) on the primitive type long
0
merciii infiniment
0
Rejoignez-nous