Search code examples
inheritancejpaouter-join

JPA how to configure outer join with InheritanceType.JOINED


I have a domain model with InheritanceType.JOINED.

@Table(name = "S_MC_CC_RAPPORTI")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "COD_TIPORAPPORTO")
public class RapportoImpl implements Rapporto, Cloneable {

    @Id
    @Column(name = "COD_RAPPORTO")
    protected Long codiceRapporto;

and the subclass:

@Entity
@Table(name = "CARTE")
@DiscriminatorValue("4 ")
public class CartaImpl extends RapportoImpl implements Carta, Cloneable {

    private static final long serialVersionUID = 1723366781345274590L;

This works great until the DB is consistent.

Table Rapporto

Id   COD_TIPORAPPORTO
1       4

Table Carte

Id   Description
1     Carta

So when I load a Rapporto by id 1 all works great. But when the db goes inconsistent all works bad.
Imagine that I have no more the record on the child table Carte. When I try to load it I get a null object because JPA makes an inner join between super and child table. Is it possible to make configure it in a way to make an outer join on the child table


Solution

  • To sum up - if your JPA provider is EclipseLink than there is a way to say that it should use OUTER JOINs when fetching child objects in join inheritance strategy.