-1

I have 2 entities A & B which I would like to join on 2 conditions:

SELECT * FROM A JOIN B ON A.A_ID = B.A_ID AND SYSDATE BETWEEN B.START_DATE AND B.END_DATE


Class A {
    
    @Id
    @Column(name = "A_ID")
    String aId;
    
    @Column(name = "A_NAME")
    String aName;
    
    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name = "aId", nullable = false, insertable = false, updatable = false)  // Add SYSDATE condition here
    Set<B> B bObjects;
    
}

Class B {
    
    @Column(name = "A_ID")
    String aId;
    
    @Column(name = "B_ID")
    String bId;
    
    @Column(name = "B_NAME")
    String bName;
    
    @Column(name = "START_DATE")
    LocalDate startDate;
    
    @Column(name = "END_DATE")
    LocalDate endDate;
}

Not sure how do I add the SYSDATE BETWEEN B.START_DATE AND B.END_DATE condition in the @JoinColumn annotation?

crazycoder
  • 65
  • 1
  • 8

1 Answers1

0

You can not do that through simple annotations, as far as I'm aware.

Nonetheless, in your JPA queries you can use an ON clause, if you use JPA 2.1. You got an example in: How to make a CriteriaBuilder join with a custom "on" condition?

HaroldH
  • 533
  • 1
  • 4
  • 10