EvilJoe
EvilJoe

Reputation: 175

JPA/Hibernate Join On Constant Value

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 *
FROM owner o
JOIN types t on t.owner_id = o.id AND t.type = 'A'
--                                ^^^^^^^^^^^^^^^^ THIS IS WHAT I AM TRYING TO REPLICATE

In Java + JPA/Hibernate, I am trying to do something like this...

@Entity
@Table(name = "OWNER")
public class Owner {

    @Id
    @Column(name="ID")
    private Long id

    @OneToOne(mappedBy = "owner", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumns({
        @JoinColumn(name = "ID", referencedColumnName = "ID"),
        @JoinColumn(constantValue = "A", referencedColumnName="type")})
    //              ^^^^^^^^^^^^^^^^^^^  I AM LOOKING FOR SOMETHING THAT DOES THIS.
    //                                   constantValue IS NOT A VALID ARGUMENT HERE.
    private TypeA inspectionSnapshot;

    @OneToOne(mappedBy = "owner", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumns({
        @JoinColumn(name = "ID", referencedColumnName = "ID"),
        @JoinColumn(constantValue = "B", referencedColumnName="type")})
    private TypeB inspectionSnapshot;

    /* Getters & Setters ... */
}

@Entity
@Table(name = "TYPES")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "TYPE", discriminatorType = DiscriminatorType.STRING)
public abstract class BaseType {

    @Id
    @OneToOne
    @JoinColumn(name = "OWNER_ID", referencedColumnName="ID")
    private Owner id;

    @Id
    @Column(name = "TYPE")
    private char type

    /* Getters & Setters ... */
}

@Entity
@DiscriminatorValue("A")
public class TypeA extends BaseType {
    /* All functionality in BaseType */
}

@Entity
@DiscriminatorValue("B")
public class TypeA extends BaseType {
    /* All functionality in BaseType */
}

Thanks in advance!

Upvotes: 15

Views: 21662

Answers (3)

Dmitry
Dmitry

Reputation: 290

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;

Upvotes: 24

David Levesque
David Levesque

Reputation: 22451

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.

Upvotes: 9

Kikin-Sama
Kikin-Sama

Reputation: 412

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!

Upvotes: 5

Related Questions