Ошибка EntityNotFoundException в Java, Hibernate
Получаю EntityNotFoundException, когда пытаюсь получить ревизии класса Action для поля Plan для некоторых значений Action. Хотя planId присутствует и в таблице Action, и во всех ревизиях Action.
Метод:
private Optional<Revision<Integer, Action>> getLatestActionRevisionBeforeDate(final Long id, final LocalDate date) {
final var beforeInstant = date.plusDays(1).atStartOfDay(ZoneId.systemDefault()).toInstant();
return this.actionRepository.findRevisions(id).stream()
.filter(rev -> rev.getMetadata().getRequiredRevisionInstant().isBefore(beforeInstant))
.max(Comparator.naturalOrder());
}
Классы:
@Getter
@Setter
@ToString
@DynamicUpdate
@Entity(name = "new_action")
@Accessors(chain = true)
@NamedEntityGraph(name = "fetch.whole.action",
attributeNodes = {
@NamedAttributeNode(value = "plan"),
@NamedAttributeNode(value = "actionAdditional"),
@NamedAttributeNode(value = "status", subgraph = "action.status.translations"),
@NamedAttributeNode(value = "evidences", subgraph = "action.evidence.documents")},
subgraphs = {
@NamedSubgraph(name = "action.status.translations", attributeNodes = @NamedAttributeNode(value = "translations")),
@NamedSubgraph(name = "action.evidence.documents", attributeNodes = @NamedAttributeNode(value = "documents"))
})
@Audited
@AuditOverride(forClass = UpdatableEntity.class)
public class Action extends UpdatableEntity {
private static final long serialVersionUID = 4489013915376063513L;
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(updatable = false, nullable = false)
private Long id;
@Column(nullable = false)
private String title;
@Lob
@Column(nullable = false)
private String description;
@Exclude
@ManyToOne(fetch = LAZY)
@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
private ActionStatus status;
@Column(nullable = false)
private boolean isOnRisk;
@Column(nullable = false)
private boolean isValidated;
@Lob
private String comment;
@Exclude
@ManyToOne(fetch = LAZY)
@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
private Plan plan;
private LocalDateTime scheduledDateTime;
@Column(nullable = false)
private LocalDate deadlineDate;
private LocalDateTime statusUpdatedDateTime;
@Exclude
@OneToMany(mappedBy = "action", cascade = ALL, orphanRemoval = true)
@NotAudited
private Set<Evidence> evidences = new HashSet<>();
@Exclude
@PrimaryKeyJoinColumn
@OneToOne(mappedBy = "action", cascade = ALL, orphanRemoval = true)
@NotAudited
private ActionAdditional actionAdditional;
public Action setEvidences(@NonNull final Set<Evidence> evidences) {
this.evidences = new HashSet<>(evidences);
evidences.forEach(evidence -> evidence.setAction(this));
return this;
}
public Action setActionAdditional(final ActionAdditional actionAdditional) {
if (actionAdditional != null) {
actionAdditional.setAction(this);
}
this.actionAdditional = actionAdditional;
return this;
}
public Action add(@NonNull final Evidence evidence) {
this.evidences.add(evidence);
evidence.setAction(this);
return this;
}
public Action remove(@NonNull final Evidence evidence) {
this.evidences.remove(evidence);
evidence.setAction(null);
return this;
}
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) {
return false;
}
final Action action = (Action) o;
return this.getId() != null && Objects.equals(this.getId(), action.getId());
}
@Override
public int hashCode() {
return this.getClass().hashCode();
}
}
@Getter
@Setter
@Entity(name = "new_plan")
@Accessors(chain = true)
@Audited
@AuditOverride(forClass = UpdatableEntity.class)
public class Plan extends UpdatableEntity {
private static final long serialVersionUID = -424662028473956250L;
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(updatable = false, nullable = false)
private Long id;
@Column(nullable = false)
private boolean isLaunched;
private String diagnosis;
@Column(name = "external_location_id")
private String locationId;
@Column(name = "external_audit_id")
private String auditId;
@Column(name = "external_company_id")
private String companyId;
@Column(name = "external_address_id")
private String addressId;
@ManyToOne(fetch = EAGER)
@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
@JoinColumns({
@JoinColumn(name = "external_location_id", referencedColumnName = "LOCATION_ID", insertable = false, updatable = false),
@JoinColumn(name = "external_company_id", referencedColumnName = "COMPANY_ID", insertable = false, updatable = false),
@JoinColumn(name = "external_address_id", referencedColumnName = "ADDRESS_ID", insertable = false, updatable = false)
})
private ExtLocation externalLocation;
@OneToOne
@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
@JoinColumn(name = "external_company_id", insertable = false, updatable = false)
private ExtCompany extCompany;
@Column(nullable = false)
private LocalDate startDate;
private LocalDate kickOffDate;
private LocalDate plannedFinishingDate;
@Convert(converter = SponsorsToJsonConverter.class)
private List<Sponsor> sponsors;
@Exclude
@PrimaryKeyJoinColumn
@OneToOne(mappedBy = "plan", cascade = ALL, orphanRemoval = true)
@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
private Close close;
@Exclude
@ManyToOne(fetch = EAGER)
@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
private Category category;
@ManyToOne(fetch = EAGER)
@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
private PlanStatus status;
@Exclude
@OneToMany(mappedBy = "plan", cascade = ALL, orphanRemoval = true, fetch = EAGER)
@NotAudited
private Set<Action> actions = new HashSet<>();
@Exclude
@OneToMany(mappedBy = "plan", cascade = {ALL}, orphanRemoval = true, fetch = LAZY)
@NotAudited
private Set<ManuallyCreatedNonConformity> nonConformity = new HashSet<>();
@Exclude
@OneToMany(mappedBy = "plan", cascade = ALL, orphanRemoval = true, fetch = LAZY)
@NotAudited
private List<Milestone> milestones = new LinkedList<>();
@Exclude
@OneToMany(mappedBy = "plan", cascade = ALL, orphanRemoval = true, fetch = EAGER)
@NotAudited
private Set<Extension> extensions = new HashSet<>();
@ManyToOne(fetch = EAGER)
@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
private PlanOrigin origin;
public Plan add(@NonNull final Action action) {
this.actions.add(action);
action.setPlan(this);
return this;
}
public Plan add(@NonNull final ManuallyCreatedNonConformity nonConformity) {
this.nonConformity.add(nonConformity);
nonConformity.setPlan(this);
return this;
}
public Plan add(@NonNull final Extension extension) {
this.extensions.add(extension);
extension.setPlan(this);
return this;
}
public Plan add(@NonNull final Milestone milestone) {
this.milestones.add(milestone);
milestone.setPlan(this);
return this;
}
public Plan setMilestones(@NonNull final List<Milestone> milestones) {
this.milestones = new LinkedList<>(milestones);
this.milestones.forEach(milestone -> milestone.setPlan(this));
return this;
}
public Plan actualizeMilestones() {
this.milestones.forEach(milestone -> milestone.setPlan(this));
return this;
}
public Plan addAllExtensions(@NonNull final Set<Extension> extensions) {
this.extensions.addAll(extensions);
this.extensions.forEach(extension -> extension.setPlan(this));
return this;
}
public Plan addAllActions(@NonNull final Collection<Action> actionsToAdd) {
this.actions.addAll(actionsToAdd);
actionsToAdd.forEach(action -> action.setPlan(this));
return this;
}
public Plan addAllNonConformity(@NonNull final Collection<ManuallyCreatedNonConformity> nonConformity) {
this.nonConformity.addAll(nonConformity);
nonConformity.forEach(item -> item.setPlan(this));
return this;
}
public Plan addAllMilestones(@NonNull final Collection<Milestone> milestonesToAdd) {
this.milestones.addAll(milestonesToAdd);
milestonesToAdd.forEach(milestone -> milestone.setPlan(this));
return this;
}
public Plan remove(@NonNull final Action action) {
this.actions.remove(action);
action.setPlan(null);
return this;
}
public Plan setClose(final Close close) {
this.close = close;
Optional.ofNullable(close)
.map(close1 -> close1.setPlan(this));
return this;
}
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) {
return false;
}
final Plan plan = (Plan) o;
return this.id != null && Objects.equals(this.id, plan.id);
}
@Override
public int hashCode() {
return this.getClass().hashCode();
}
}