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.  [ 11 posts ] 
Author Message
 Post subject: @orderBy doesn´t work
PostPosted: Tue Oct 02, 2007 11:06 am 
Beginner
Beginner

Joined: Tue Aug 21, 2007 4:58 am
Posts: 30
I´m trying to order by id with the desc clause, I´ve tried in different ways, according to what I have foun on the net, but none of them work. This is what I have
Code:
@OrderBy( "id DESC")


I have also tried :

Code:
@org.hibernate.annotations.OrderBy( clause= "id DESC")


Any ideas?

thanx[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 02, 2007 6:00 pm 
Regular
Regular

Joined: Sun Sep 30, 2007 7:51 pm
Posts: 93
What are you trying to order?

Regards,
Pavol


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 03, 2007 2:25 am 
Newbie

Joined: Mon Oct 01, 2007 8:59 am
Posts: 18
If you want to retrive list in the desc order of id than you have many ways for that:
1. You can add order in criteria queries.
session.createCriteria(className.class).addOrder( Property.forName("id").desc() ).list();
or
session.createCriteria(className.class).addOrder(Order.desc("id")).list();

2. session.createQuery("from ClassName user order by user.id desc").list();


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 03, 2007 3:39 am 
Beginner
Beginner

Joined: Tue Aug 21, 2007 4:58 am
Posts: 30
I am trying to order a OneToMany relation. I have sarched in serveral places and it says that if I use the tag @OrderBy it works, but it doesn´t.
This is what I have:

Code:
@OneToMany(mappedBy="viviendaLocalActual",cascade=CascadeType.ALL,fetch=FetchType.LAZY)
   @OrderBy("id")
   @Cascade(org.hibernate.annotations.CascadeType.LOCK)
   private List<ViviendaLocalHistorico> viviendaLocalHistoricos;


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 03, 2007 3:41 am 
Beginner
Beginner

Joined: Tue Aug 21, 2007 4:58 am
Posts: 30
I am trying to order a OneToMany relation. I have sarched in serveral places and it says that if I use the tag @OrderBy it works, but it doesn´t.
This is what I have:

Code:
@OneToMany(mappedBy="viviendaLocalActual",cascade=CascadeType.ALL,fetch=FetchType.LAZY)
   @OrderBy("id")
   @Cascade(org.hibernate.annotations.CascadeType.LOCK)
   private List<ViviendaLocalHistorico> viviendaLocalHistoricos;


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 03, 2007 4:09 am 
Regular
Regular

Joined: Sun Sep 30, 2007 7:51 pm
Posts: 93
So your class ViviendaLocalHistorico has an attribute called id? If yes, it should be ok and it should work :-|.

If the "id" is your PK for ViviendaLocalHistorico, you can try just to add the @OrderBy with no params at all.

Sorting by other attribute works?

Regards,
Pavol


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 03, 2007 4:59 am 
Beginner
Beginner

Joined: Tue Aug 21, 2007 4:58 am
Posts: 30
ViviendaLocalHistorico it´s just a list that keeps a changes record. So the thing is that I want to order from the last to the first change, that is why I need to order by desc. The thing is that no matter what I do I get the list ordered by ASC.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 03, 2007 6:08 am 
Regular
Regular

Joined: Sun Sep 30, 2007 7:51 pm
Posts: 93
Can you post the class, or some part you think are important?

Have you tried order by another field with DESC ?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 03, 2007 6:20 am 
Beginner
Beginner

Joined: Tue Aug 21, 2007 4:58 am
Posts: 30
This is the class, and no I haven´t tried to order by other field....I will try as soon as I can.

Code:
package com.telvent.padron.bi.territorio.model;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.ForeignKey;

import com.telvent.padron.bi.comun.DatosControlTroncalActual;
import com.telvent.padron.bi.comun.IEsHistorico;
import com.telvent.padron.bi.comun.ITieneHistorico;
import com.telvent.padron.bi.territorio.comun.ViviendaLocalBase;
import com.telvent.padron.comun.ILocalizable;
import com.thoughtworks.xstream.annotations.XStreamAlias;

@Entity
@Table( name="TRR_HUECO")
@XStreamAlias("ViviendaLocal")
public class ViviendaLocal extends ViviendaLocalBase implements Serializable,ITieneHistorico, ILocalizable{
   /*ATRIBUTOS*/
   /**
    *
    */
   private static final long serialVersionUID = 1L;

/**
* Código interno de identificación única del registro. 
* Número de Identificación del hueco.  El NIDEN se crea pero nunca se modifica.
* ([P]rimary [K]ey). Secuencial.
*/
   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   @Column(name = "NIDEN_HUECO")
   private Integer id;   
   
   protected DatosControlTroncalActual datosControl = new DatosControlTroncalActual();
   
/**
* NIDEN del texto normlizado de vivienda.
* [F]oreing [K]ey de enlace con la tabla de Valor Válido de texto normalizado de vivienda
* (TRR_TEXTO_NORM_HUECO).
*/
   @ManyToOne
   @JoinColumn(name="NIDEN_TXT_NOR_VIV")
   @ForeignKey(name="FK_HUECO_TNV")
   @Cascade(org.hibernate.annotations.CascadeType.LOCK)
   private TextoNormalizadoViviendaLocal textoNormalizadoViviendaLocal;
   
   @OneToMany(mappedBy="viviendaLocalActual",cascade=CascadeType.ALL,fetch=FetchType.LAZY)
   @OrderBy("id DESC")
   @Cascade(org.hibernate.annotations.CascadeType.LOCK)
   private List<ViviendaLocalHistorico> viviendaLocalHistoricos;
   
   /**
    * NIDEN del Numerero al que pertenece el hueco (vivienda o local). 
    * [F]oreing [K]ey de enlace con la tabla troncal de Numerero (TRR_NUMERERO)
    */
   @ManyToOne
   @JoinColumn(name="NIDEN_APP", nullable=false)
   @ForeignKey(name="FK_HUECO_NUMERERO")
   @Cascade(org.hibernate.annotations.CascadeType.LOCK)
   private Numerero numerero;
   
   @Column(name = "CODIGO_HUECO", nullable=true)
   private Integer codHueco;
   
   /*CONSTRUCTORES*/

   
   public Numerero getNumerero() {
      return numerero;
   }
   public void setNumerero(Numerero numerero) {
      this.numerero = numerero;
   }
   public ViviendaLocal(){
      
   }
   /*METODOS PUBLICOS*/
   public Integer getId() {
      return id;
   }
   public void setId(Integer id) {
      this.id = id;
   }
   public TextoNormalizadoViviendaLocal getTextoNormalizadoViviendaLocal() {
      return textoNormalizadoViviendaLocal;
   }
   public void setTextoNormalizadoViviendaLocal(
         TextoNormalizadoViviendaLocal textoNormalizadoViviendaLocal) {
      this.textoNormalizadoViviendaLocal = textoNormalizadoViviendaLocal;
   }
   
   public List<ViviendaLocalHistorico> getViviendaLocalHistoricos() {
      return viviendaLocalHistoricos;
   }
   public void setViviendaLocalHistoricos(
         List<ViviendaLocalHistorico> viviendaLocalHistoricos) {
      this.viviendaLocalHistoricos = viviendaLocalHistoricos;
   }
   public void cargarDeActual(ITieneHistorico alife) {
      // TODO Auto-generated method stub
      
   }
   /*public Set<Poblacion> getPoblaciones() {
      return poblaciones;
   }
   public void setPoblaciones(Set<Poblacion> poblaciones) {
      this.poblaciones = poblaciones;
   }*/
   public void cargarDesdeHistorico(IEsHistorico historico) {
      
      ViviendaLocalHistorico vl = (ViviendaLocalHistorico)historico;
      super.setCodigoReferenciaDgc(vl.getCodigoReferenciaDgc());
      super.setFechaGrabacionCierre(vl.getFechaGrabacionCierre());
      super.setFechaGrabacionAyto(vl.getFechaGrabacionAyto());
      
      this.setPlanta(vl.getPlanta());
      this.setPuerta(vl.getPuerta());
      this.setTextoNormalizadoViviendaLocal(vl.getTextoNormalizadoViviendaLocal());
      this.setTipo(vl.getTipo());
      this.setTipoColectivo(vl.getTipoColectivo());
      this.setTipoLocal(vl.getTipoLocal());
      this.setNumerero(vl.getNumerero());
   }
   
   
   public DatosControlTroncalActual getDatosControl() {
      return datosControl;
   }
   public void setDatosControl(DatosControlTroncalActual datosControl) {
      this.datosControl = datosControl;
   }
   public List<ViviendaLocalHistorico> getHistorico() {
      return this.viviendaLocalHistoricos;
   }
   public void setHistorico(List col) {
      this.viviendaLocalHistoricos = col;
   }
   public void addHistorico(ViviendaLocalHistorico viviendaLocalHistorico)
   {
      this.getHistorico().add(viviendaLocalHistorico);
      viviendaLocalHistorico.setViviendaLocalActual(this);
   }
   
   
   public Integer getNumPersonas(Date fechaDada){
      //TO DO implementar el método de recuento de personas en hueco
      if(fechaDada == null)
         return 0;
      else
         return 10;
   }
   public Integer getCodHueco()
   {
      Integer cod=codHueco;
      if(cod==null)
         cod=id;
      return codHueco;
   }
   public void setCodHueco(Integer codHueco)
   {
      this.codHueco = codHueco;
   }
   /**
    * Constructs a <code>String</code> with all attributes
    * in name = value format.
    *
    * @return a <code>String</code> representation
    * of this object.
    */
   public String toString()
   {
       final String TAB = "    ";
   
       StringBuilder retValue = new StringBuilder();
      
       retValue.append("ViviendaLocal ( ")
           .append(super.toString()).append(TAB)
           .append("id = ").append(this.id).append(TAB)
           .append("textoNormalizadoViviendaLocal = ").append(this.textoNormalizadoViviendaLocal).append(TAB)
           .append("codHueco = ").append(this.codHueco).append(TAB)
           .append("datosControl = ").append(this.datosControl).append(TAB)
           .append(" )");
      
       return retValue.toString();
   }




}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 03, 2007 6:27 am 
Regular
Regular

Joined: Sun Sep 30, 2007 7:51 pm
Posts: 93
I was thinking the ViviendaLocalHistorico class, sorry :-). Can you post this one?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 03, 2007 6:31 am 
Beginner
Beginner

Joined: Tue Aug 21, 2007 4:58 am
Posts: 30
Not a big deal. Here it goes:

Code:
package com.telvent.padron.bi.territorio.model;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import com.telvent.padron.bi.comun.DatosControlTroncalHistorico;
import com.telvent.padron.bi.comun.IEsHistorico;
import com.telvent.padron.bi.comun.ITieneHistorico;
import com.telvent.padron.bi.territorio.comun.ViviendaLocalBase;

@Entity
@Table( name="HTRR_HUECO" )

public class ViviendaLocalHistorico extends ViviendaLocalBase implements Serializable, IEsHistorico{
   /*ATRIBUTOS*/
   /**
    *
    */
   private static final long serialVersionUID = 1L;

   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   @Column(name = "ID")
   private Integer id;
   
   protected DatosControlTroncalHistorico datosControl = new DatosControlTroncalHistorico();
   
   @ManyToOne
   @JoinColumn(name="NIDEN_HUECO", nullable=false)
   protected ViviendaLocal viviendaLocalActual;
   
   @ManyToOne
   @JoinColumn(name="NIDEN_TXT_NOR_VIV")
   private TextoNormalizadoViviendaLocal textoNormalizadoViviendaLocal;   
   
   @ManyToOne
   @JoinColumn(name="NIDEN_APP")
   private Numerero numerero;
   
   @Column(name="F_EXPIRACION")
   @Temporal(TemporalType.TIMESTAMP)
   private Date fechaExpiracion;
   
   @Column(name = "codigoHueco", nullable=true)
   private Integer codHueco;
   
   public Date getFechaExpiracion() {
      return fechaExpiracion;
   }
   public void setFechaExpiracion(Date fechaExpiracion) {
      this.fechaExpiracion = fechaExpiracion;
   }   
   
   public Numerero getNumerero() {
      return numerero;
   }
   public void setNumerero(Numerero numerero) {
      this.numerero = numerero;
   }
   public TextoNormalizadoViviendaLocal getTextoNormalizadoViviendaLocal() {
      return textoNormalizadoViviendaLocal;
   }
   public void setTextoNormalizadoViviendaLocal(
         TextoNormalizadoViviendaLocal textoNormalizadoViviendaLocal) {
      this.textoNormalizadoViviendaLocal = textoNormalizadoViviendaLocal;
   }

   /*CONSTRUCTORES*/
   public ViviendaLocalHistorico(){
      
   }
   /*METODOS PUBLICOS*/
   public Integer getId() {
      return id;
   }

   public void setId(Integer id) {
      this.id = id;
   }


   public ViviendaLocal getViviendaLocalActual() {
      return viviendaLocalActual;
   }

   public void setViviendaLocalActual(ViviendaLocal viviendaLocalActual) {
      this.viviendaLocalActual = viviendaLocalActual;
   }

   public IEsHistorico getActual() {
      return (IEsHistorico)viviendaLocalActual;
   }

   public void setActual(IEsHistorico is) {
      this.viviendaLocalActual = (ViviendaLocal)is;
   }

   public void cargarDesdeActual(ITieneHistorico actual) {
      //Heredados del vivo
      ViviendaLocal vl = (ViviendaLocal)actual;
      this.datosControl = new DatosControlTroncalHistorico(vl.getDatosControl());
      this.setCodigoReferenciaDgc(vl.getCodigoReferenciaDgc());
      this.setNumerero(vl.getNumerero());
      this.setFechaGrabacionCierre(vl.getFechaGrabacionCierre());
      this.setFechaGrabacionAyto(vl.getFechaGrabacionAyto());
      
      this.setPlanta(vl.getPlanta());
      this.setPuerta(vl.getPuerta());
      this.setTextoNormalizadoViviendaLocal(vl.getTextoNormalizadoViviendaLocal());
      this.setTipo(vl.getTipo());
      this.setTipoColectivo(vl.getTipoColectivo());
      this.setTipoLocal(vl.getTipoLocal());
      this.setViviendaLocalActual((ViviendaLocal)actual);
   }

   public DatosControlTroncalHistorico getDatosControl() {
      return datosControl;
   }

   public void setDatosControl(DatosControlTroncalHistorico datosControl) {
      this.datosControl = datosControl;
   }
   /* METODOS PRIVADOS */
   
   public Integer getNumPersonas(Date fechaDada){
      
      //TO DO implementar el método de recuento de personas en hueco
      if(fechaDada == null)
         return 0;
      else
         return 10;
   }
   
   public Integer getCodHueco()
   {
      Integer cod=codHueco;
      if(cod==null)
         cod=id;
      return codHueco;
   }
   public void setCodHueco(Integer codHueco)
   {
      this.codHueco = codHueco;
   }
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 11 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.