1

I'm Using JPA 2.1. I have 3 entities: Dr01 , Dr02 and Dr03 with the following structure:

public class Dr01 implements Serializable {

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "dr01")
    private List<Dr02> dr02List;

}
public class Dr02 implements Serializable {

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "dr02")
    private List<Dr03> dr03List;

    @JoinColumn(name = "DR2CLM", referencedColumnName = "DR1CLM", insertable = false, updatable = false)
    @ManyToOne(optional = false)
    private Dr01 dr01;

}    

public class Dr03 implements Serializable {

    @JoinColumns({
        @JoinColumn(name = "DR3CLM", referencedColumnName = "DR2CLM", insertable = false, updatable = false),
        @JoinColumn(name = "DR3PTFN", referencedColumnName = "DR2PTFN", insertable = false, updatable = false)})
    @ManyToOne(optional = false)
    private Dr02 dr02;

    private elementOBJ element;
}

public class elementOBJ implements Serializable {

    @Column(name = "XXX")
    private int id;

    @Column(name = "YYY")
    private int status;
}

I want to select from Dr01 and get only the Dr03 objects that have element objects which contains a value of 1 inside the status field.

How do I retrieve dr03List filtered by it's status value? (filtered not after the select).

Thank's In Advance.

3
  • What exactly are you after? If you just want Dr03 entities with a particular element.status value, have you tried "select dr3 from Dr03 dr3 where dr3.element.status = :status"? Commented Sep 8, 2014 at 19:17
  • @Chris: I know I can do that, but I want to only select Dr01 (by it's Id) and get Dr02 and Dr03 , but get them filtered. do you understand what I mean? Commented Sep 9, 2014 at 7:42
  • 1
    Not really. If you mean you want to get a particular Dr01 and have the collection within that entity filtered, then that is not within the scope of JPA, as the managed entities returned are meant to reflect what is in the database. You would instead query for the collection using the filter you want directly rather than traverse the Dr01.dr02List relationship. Commented Sep 9, 2014 at 12:45

1 Answer 1

1

Options that may be of assistance:

  1. Create a DB view based on status of DR03 table and map your entity to that.
  2. Use JPA Inheritance using status as a DiscriminatorColumn.
  3. If using Hibernate use the non-JPA @Where annotation to filter the collection
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.