JPA problem with an @Entity and @DiscriminatorValue annotation

25 views Asked by At

I have a problem in JPA with an @Entity object and the @DiscriminatorValue annotation. When I search for the object with JpaRepository, the child object is null.

this is the class:

@Entity
@DiscriminatorValue("KLP_IF_INVOICE_FINANCING")
public final class InvoiceFinancing<I extends InvoiceFinancingInfo> extends Agreement<I,Underlying<?>> {

    private I invoiceFinancingInfo;

    @Override
    @OneToOne(targetEntity = InvoiceFinancingInfo.class, mappedBy = "agreement", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    public I getInfo() {
        return this.invoiceFinancingInfo;
    }

    public void setInfo(I invoiceFinancing) {
        this.invoiceFinancingInfo = invoiceFinancing;
    }
}

When i get the invoiceFinancingInfo, with getInfo() the object is null, but in database table the record exist. Follow the othres classes:

@Entity
@Table(name = "KLP_IF_INVOICE_FINANCING")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@EntityListeners(AuditingEntityListener.class)
public class InvoiceFinancingInfo extends AbstractAgreementInfo {

    private String domainId;
    private String name;
    private String externalId;
    //GETTERS AND SETTERS

}

@MappedSuperclass
public abstract class AbstractAgreementInfo extends IdentifiableEntity implements AgreementInfo {

    private ZonedDateTime startDate;
    private ZonedDateTime endDate;
    private Agreement<?,?> agreement;
    
    public ZonedDateTime getStartDate() {
        return startDate;
    }
    
    public void setStartDate(ZonedDateTime startDate) {
        this.startDate = startDate;
    }
    
    public ZonedDateTime getEndDate() {
        return endDate;
    }
    
    public void setEndDate(ZonedDateTime endDate) {
        this.endDate = endDate;
    }
    
    @Override
    @OneToOne(targetEntity = Agreement.class, cascade = CascadeType.ALL, fetch = FetchType.LAZY, optional = false)
    @JoinColumn(nullable = false, insertable = true, updatable = false)
    public Agreement<?, ?> getAgreement() {
        return agreement;
    }
    
    public void setAgreement(Agreement<?, ?> agreement) {
        this.agreement = agreement;
    }
    
}

@MappedSuperclass
public abstract class IdentifiableEntity implements ManagedMappedSuperclass {
    private String id;
    //GETTERS AND SETTERS
}

public interface AgreementInfo {
    ZonedDateTime getStartDate();
    ZonedDateTime getEndDate();
    Agreement<?,?> getAgreement();
    void setAgreement(Agreement<?,?> agreement);
}

@Entity
@Table(name = "KLP_AGREEMENT")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class Agreement<I extends AgreementInfo, U extends Underlying<?>> extends IdentifiableEntity {

    private Collection<Participant> participants;
    private Collection<? extends U> underlyings;
    /* Commit comment */
    public static final <U extends Underlying<?>, I extends AgreementInfo, A extends Agreement<I,U>> A of(Supplier<A> supplier, I info, Collection<U> underlyings, Collection<Participant> participants) {
        A agreement = supplier.get();
        if(info != null) {
            agreement.setInfo(info);
            info.setAgreement(agreement);
        }
        if(participants != null) {          
            agreement.setParticipants(participants);
            participants.forEach(p -> p.setAgreement(agreement));
        }
        if(underlyings != null) {           
            agreement.setUnderlyings(underlyings);
            underlyings.forEach(u -> u.setAgreement(agreement));
        }
        info.setAgreement(agreement);
        return agreement;
    }

    @OneToMany(targetEntity = Participant.class, mappedBy = "agreement", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    public Collection<Participant> getParticipants() {
        return participants;
    }

    public void setParticipants(Collection<Participant> participants) {
        this.participants = participants;
    }

    @OneToMany(targetEntity = Underlying.class, mappedBy = "agreement", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    public Collection<? extends U> getUnderlyings() {
        return underlyings;
    }

    public void setUnderlyings(Collection<? extends U> underlyings) {
        this.underlyings = underlyings;
    }
    
    @Transient
    public abstract I getInfo();
    public abstract void setInfo(I info);
    
}

@Entity
@Table(name = "KLP_UNDERLYING")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class Underlying<T> extends IdentifiableEntity {

    private Agreement<?,?> agreement;

    public static final <T , U extends Underlying<T>> U of(Supplier<U> supplier, T underlyingObject) {
        U underlying = supplier.get();
        if(underlyingObject != null) {
            underlying.setObject(underlyingObject);
        }
        return underlying;
    }
    
    @ManyToOne(targetEntity = Agreement.class, fetch = FetchType.LAZY, optional = false)
    @JoinColumn(nullable = false, insertable = true, updatable = false)
    public Agreement<?,?> getAgreement() {
        return agreement;
    }

    public void setAgreement(Agreement<?,?> agreement) {
        this.agreement = agreement;
    }
    
    @Transient
    public abstract T getObject();
    public abstract void setObject(T info);
    
}

I hope that someone can help me. The problem started after updating to springboot 2.6.6, before that it worked!

0

There are 0 answers