JPA ManyToOne with potentially null JoinColumn

1k views Asked by At

I have a relationship I cannot figure out how (or even if) JPA can map it.

Two tables are joined by multiple columns. The columns combined are a unique, but not primary, key.

@ManyToOne
@JoinColumns({
    @JoinColumn(name = "TYPE", referencedColumnName = "TYPE", nullable = false),
    @JoinColumn(name = "SUBTYPE", referencedColumnName = "SUBTYPE", nullable = true)
})
private TypeInformation typeInformation;

JPA only returns TypeInformation if both TYPE and SUBTYPE are specified, but SUBTYPE is frequently null. How can I get JPA to return matching TypeInformation by comparing two null values together as true?

This is usually done in SQL by coalescing the field to a non-null value for the purpose of comparison. Can I specify a value for the SUBTYPE column if the database returns null?

The specific JPA implementation I am using is Apache's OpenJPA 2.0, but I am still interested if other implementations like Hibernate or EclipseLink allow this functionality.

@Entity
@Table(name="TYPE_INFORMATION")
public class TypeInformation implements Serializable {
    @EmbeddedId
    private TypeInformationPK id;
    // other fields and accessors
}


@Embeddable
public class TypeInformationPK {
    @Column(name="TYPE")
    private String type;

    @Column(name="SUBTYPE")
    private String subtype;

    // No other fields
    // accessors
    // override equals and hashCode
}
0

There are 0 answers