JPA Order by with two columns
NickName:Diego Gusava Ask DateTime:2011-09-06T00:35:15

JPA Order by with two columns

I have two objects

Objects: Objeto, Comunicado and ComunicadoTramite

@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.

SQL I did this

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?

Based on Jorge' ideas i did this

SELECT obj 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.comunicado.id = tramite.comunicado.id)
                group by tramite.comunicado)
ORDER BY obj.dataCriacao DESC

Copyright Notice:Content Author:「Diego Gusava」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/7310712/jpa-order-by-with-two-columns

More about “JPA Order by with two columns” related questions

JPA Order by with two columns

I have two objects Objects: Objeto, Comunicado and ComunicadoTramite @Entity @Table(name = "objetos") @Inheritance(strategy = InheritanceType.JOINED) public class Objeto extends Fact { @I...

Show Detail

Spring jpa combine two columns and order by one column and find top

I have table, where I need to fetch row based on two columns and find top records with the help of order by one column. --------------------------------------------------------------------- id

Show Detail

jpa difference in timestamp columns

I have a JPA query to compute differnce between two timestamp columns . select ( endTime - startTime ) from myTable The value is getting returned as secons in MySQL. But in Oracle , its coming ...

Show Detail

JPA SqlResultSetMapping object order

JPA: if classes, entities and columns are defined in @SqlResultSetMapping, what is the order of objects returned? @SqlResultSetMapping( name="CustomerDetailsResult", classes={ ...

Show Detail

JPA Expression concatenate more than two columns

I have the following statement to concatenate two columns which works well Expression&lt;String&gt; stringConcat = cb.concat(cb.concat(root.get(Employee_.userId), " # "), ...

Show Detail

JPA Criteria Query API and order by two columns

I'm stuck with a simple problem; struggling how to invoke order by on a joined entity. Essentially I am trying to achieve the following with JPA Criteria: select distinct d from Department d left...

Show Detail

How to sort columns multiple columns in spring jpa which has parameters being passed?

I have a Jpa repository and I need to sort by two columns i order. public interface PetRepository extends JpaRepository&lt;PetClass, PetKey&gt; { List&lt;PetClass&gt;

Show Detail

JPA and order of columns in the database

I'm using Eclipselink as my persistence provider. Is there any way to specify the order in which the columns appear in the database? The column-order in the database doesn't match the attribute-ord...

Show Detail

Order data by two columns

How can I order a set of data, by two seperate columns? One of the two columns is not always 0, I need it ordered by the largest of either column. Currently getting this set of data from order by

Show Detail

defining Custom Id in JPA

I am new to spring JPA. My Requirement is to have an ID of composite columns. I have a ledger table where I have two columns log_id and order_id. I have to insert the multiple record for the same

Show Detail