Home > Mobile >  JPA/Hibernate @OrderBy annotation with boolean row
JPA/Hibernate @OrderBy annotation with boolean row

Time:02-08

I want to order by a row "pk" in attributes. But it has a boolean data. The attributes that has pk=true should go first. I just can't write @OrderBy("pk=true"). How can I make it to order by this row?

public class Entity {

//here are some rows;

@OneToMany
    @JoinColumn(name = "id")
    @OrderBy("pk", "firstName")
    private Collection<Attributes> attributes;
}

public class Attributes {

    @Id
    private Long id;

    @Column(name = "first_name")
    private String firstName;

    @Column(name = "last_name")
    private String lastName;

    @Column(name = "pk")
    private Boolean pk;
}

CodePudding user response:

It will sort false first, since in the database true and false are represented as 1 and 0, respectively

CodePudding user response:

Ok, from the example you gave you already know how to sort by the field pk. It is just that you want the reverse order.

For that you would write

@OrderBy("pk desc")

See also https://www.logicbig.com/tutorials/java-ee-tutorial/jpa/order-by-annotation.html

  •  Tags:  
  • Related