0

Is it possibile to define somehow a relationship based on a status column?

Table A: id, valueA
Table B: id, refIdA, valueB, status

So in class A I would like to fetch only B where the status is active. Fetching with em.find(id, A.class). Is there way to define that within the entity?

@Entity
public class A {
  ...
  @OneToMany
  @JoinColumn(name = "refIdA")
  private List<B> b;
  ...
}
Matthias Baumgart
  • 905
  • 1
  • 9
  • 22

2 Answers2

0

Try @Where. There are plenty of examples on the site on how to use it, i.e. How to use @Where in JPA Hibernate

Community
  • 1
  • 1
Peter L
  • 302
  • 1
  • 6
  • Thank you, that lead me into the right direction. `@Where` is not availabele in EclipseLink, but it is possible by `@AdditionalCriteria` – Matthias Baumgart Aug 19 '16 at 03:57
0

For Eclipse Link one solution is to use @AdditionalCriteria. However this annotations is not applicable on a member. But for my usecase that is ok. So annotating B filters all B's for Status.ACTIVE.

@Entity
@AdditonalCriteria("this.status = com.example.Status.ACTIVE")
public class B {
  ...
}

Thanks to Peter L.

Community
  • 1
  • 1
Matthias Baumgart
  • 905
  • 1
  • 9
  • 22