Home Spring Data throwing an EntityNotFoundException only with EntityGraph
Reply: 0

Spring Data throwing an EntityNotFoundException only with EntityGraph

user3738
1#
user3738 Published in May 21, 2018, 11:02 am

I'm currently working on a bug that I can't explain, so I'm looking for some help here.

I'm using Spring Data to get my objects, and they are all managed by a column called "FlagCMS" which, if set to 'S' means that the object should not be linked to any other new one. It's a kind of soft-deleting that worked fine until now.

The classes are defined like this (minus a lot of unrelated code) :

Action.java

@Entity
@Table(name = "action")
@Where(clause = FlagCMS.WHERE_NOSOFTDELETED)
@NamedEntityGraphs({
    @NamedEntityGraph(name = "Action.maj", attributeNodes = {
            @NamedAttributeNode("ouvragesAction")
    }
})
public class Action {

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "action", cascade = CascadeType.ALL)
    private Set<OuvrageAction> ouvragesAction = new HashSet<>();

    @PreUpdate
    protected void cascadeDelete() {
        if (isDeleted()) {
            getAllCascades().stream().filter(Objects::nonNull).forEach(SoftDeletable::setDeleted);
        }
    }

    public Collection<OuvrageAction> getOuvragesAction() {
        return ouvragesAction;
    }

    public void setOuvragesAction(final Set<OuvrageAction> ouvragesAction) {
        this.ouvragesAction = ouvragesAction;
    }

    public List<Ouvrage> getOuvrages() {
        return ouvragesAction.stream().map(OuvrageAction::get).collect(Collectors.toList());
    }
}

OuvrageAction.java

@Entity
@Table(name = "ouvrage_action")
@Where(clause = FlagCMS.WHERE_NOSOFTDELETED)
@AttributeOverrides({
    @AttributeOverride(name = "id", column = @Column(name = "id_action_ouvrage"))
})
public class OuvrageAction extends AbstractReferentielAction<Ouvrage> {

    @ManyToOne(fetch = FetchType.EAGER, optional = false)
    @JoinColumn(name = "id_ouvrage", nullable = false)
    private Ouvrage ouvrage;

    public OuvrageAction() {
        super();
    }

    public OuvrageAction(Ouvrage ouvrage, Action action) {
        super(action);
        this.ouvrage = ouvrage;
    }

    public Ouvrage getOuvrage() {
        return ouvrage;
    }

    public void setOuvrage(Ouvrage ouvrage) {
        this.ouvrage = ouvrage;
    }

}

Ouvrage.java

@Entity
@Immutable
@Table(name = "ouvrage")
@Where(clause = FlagCMS.WHERE_NOSOFTDELETED)
public class Ouvrage {

    @Column(name = "code")
    protected String code;

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }
}

WHERE_NOSOFTDELETED is defined like this in FlagCMS.java :

public static final String WHERE_NOSOFTDELETED = "(flag_cms = 'C' or flag_cms = 'M')";

And I access the Action object using two different JpaRepositories defining similar methods, one keeping the lazy loading, and one using the EntityGraph :

@EntityGraph(value = "Action.maj", type = EntityGraphType.LOAD)
List<Action> findDistinctByCodeIn(List<String> codes);

and

List<Action> findDistinctByCodeIn(List<String> codes);

Now let's say I have an Action object linked to a Ouvrage object. The second have FlagCMS set to S. Using the lazy repository, everything is fine, I get my Actions and can access the code property of Ouvrage through it. Accessing the Action by using the repository with EntityGraph, I only get an EntityNotFoundException telling me that the Ouvrage object does not exist.

This does not seem consistent to me.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.338169 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO