JPA/Hibernate Join On Constant Value

后端 未结 3 1416
逝去的感伤
逝去的感伤 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.

提交回复
热议问题