JPA/Hibernate Join On Constant Value

后端 未结 3 1415
逝去的感伤
逝去的感伤 2021-01-02 05:09

I am trying to join to different entities from the same table using a constant value in the join statement. In SQL, I would do something like this...

SELECT         


        
相关标签:
3条回答
  • 2021-01-02 05:51

    If you don't mind using Hibernate-specific annotations you could try with the @WhereJoinTable annotation, e.g.:

    @OneToOne(mappedBy = "owner", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name = "ID", referencedColumnName = "ID")
    @WhereJoinTable(clause = "TYPE = 'A'")
    private TypeA inspectionSnapshot;
    

    Note that the clause attribute must contain SQL, not JPQL, so you need to use the database column name instead of the JPA entity field name.

    0 讨论(0)
  • 2021-01-02 05:53

    Try to specify a constant as the value of the formula

    @JoinColumnsOrFormulas({
    @JoinColumnOrFormula(formula=@JoinFormula(value="'A'", referencedColumnName="type")),
    @JoinColumnOrFormula(column = @JoinColumn("id", referencedColumnName="id"))
    })
    private TypeA inspectionSnapshot;
    
    0 讨论(0)
  • 2021-01-02 06:00

    You're looking at non-standard joins. Here's the documentation for treating such a case:

    http://docs.oracle.com/cd/E13189_01/kodo/docs40/full/html/ref_guide_mapping_notes_nonstdjoins.html

    Hope it helps!

    0 讨论(0)
提交回复
热议问题