1 year ago

#338178

test-img

Rolando Rodriguez

Select CASE WHEN in JPA Specification

I am having a problem with the next scenario:

I have these Java entities:

@Entity
public class A {
    @Id
    private int id;
    private String country;
    @Column(name = "end_date")
    private String endDate;
    @OneToMany
    private List<B> b;
}
@Entity
public class B {
    @Id
    private int id;
    @Column(name = "created_at")
    private String createdAt;
    @ManyToOne
    private A a;
}

And I need to implement a JPA Specification to achieve the next native query:

SELECT a.id,
       a.country,
       CASE
         WHEN b.created_at IS NULL THEN a.end_date
         ELSE b.created_at
       END AS end_date
FROM   a a
       LEFT JOIN b b
              ON a.id = b.a_id
WHERE  a.country = 'PE'; 

Is it posible to achieve this using only JPA Specifications?

java

hibernate

spring-data-jpa

query-builder

specifications

0 Answers

Your Answer

Accepted video resources