-->
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.  [ 2 posts ] 
Author Message
 Post subject: Order by with two columns
PostPosted: Mon Sep 05, 2011 9:40 am 
Newbie

Joined: Mon Sep 05, 2011 9:18 am
Posts: 2
Hi, I have two objects

Objects: Objeto, Comunicado and ComunicadoTramite

Code:

@Entity
@Table(name = "objetos")
@Inheritance(strategy = InheritanceType.JOINED)
public class Objeto extends Fact {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id_objeto", unique = true, nullable = false)
    private Integer id;

    @NotNull
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "dt_criacao")
    private Date dataCriacao;

}

@Entity
@Table(name = "comunicados")
@PrimaryKeyJoinColumn(name = "cd_comunicado")
public class Comunicado extends Objeto {

    @OneToMany(mappedBy = "comunicado")
    private List<ComunicadoTramite> tramites;

}

@Entity
@Table(name = "comunicados_tramites")
@PrimaryKeyJoinColumn(name = "cd_tramite")
public class ComunicadoTramite extends Objeto {

    @NotNull
    @ManyToOne
    @JoinColumn(name = "cd_comunicado")
    private Comunicado comunicado;



The problem is, i want a list of Comunicado that is ordered by dataCriacao, the two objects (Comunicado and ComunicadoTramite) extends Objeto that contains dataCriacao.

if Comunicado.tramites.size() > 0 then get dataCriacao from the last ComunicadoTramite inserted

if Comunicado.tramites.size() = 0 then get dataCriacao from Comunicado.

In native SQL I did this

Code:
SELECT c.cd_comunicado, tb_tram.dt_criacao FROM comunicados AS c
    INNER JOIN (
            SELECT ct.cd_comunicado, MAX(obj.dt_criacao) AS dt_criacao FROM comunicados_tramites AS ct, objetos obj
                WHERE ct.cd_tramite = obj.id_objeto GROUP BY ct.cd_comunicado
            ) AS tb_tram
    ON c.cd_comunicado = tb_tram.cd_comunicado
)
UNION
(SELECT c.cd_comunicado, obj.dt_criacao FROM comunicados c, objetos obj 
    WHERE c.cd_comunicado = obj.id_objeto
        AND c.cd_comunicado NOT IN (SELECT tram.cd_comunicado FROM comunicados_tramites tram))
ORDER BY dt_criacao DESC



I dont know how to do it in JPA... can anyone help me?


Top
 Profile  
 
 Post subject: Re: Order by with two columns
PostPosted: Mon Sep 05, 2011 2:19 pm 
Newbie

Joined: Mon Sep 05, 2011 9:18 am
Posts: 2
I did this

Code:
SELECT obj.dataCriacao FROM Objeto obj
    WHERE obj.id IN (select comunicado.id from Comunicado comunicado
                        where comunicado NOT IN (SELECT tramite.comunicado from ComunicadoTramite tramite))
                OR obj.id IN(select tramite.id from ComunicadoTramite tramite
                where tramite.dataCriacao = (select MAX(tram.dataCriacao) from ComunicadoTramite tram WHERE tram.id = tramite.id)
                group by tramite.comunicado)
ORDER BY obj.dataCriacao DESC


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