0

I'm trying to implement entity auditing in my Java Spring Boot project using spring-data-envers. All the entities are being created as they should, but I've come up against a brick wall when executing the query.

parentRepository.findRevisions(id).stream().map(Parent::getEntity).collect(Collectors.toList());

During this select the repository is supposed to fetch info also from the child entity, instead I get unable to find <child object> with {id}.

According to my experiments categoryId is being searched in the Category_Aud table, instead of the actual table with desired data.

Code snippets:

@Data
@Entity
@Audited
@NoArgsConstructor
public class Parent {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Enumerated(EnumType.STRING)
    private Status status;

    @Enumerated(EnumType.STRING)
    private Type requestType;

    private String fullName; 

    @ManyToOne
    @JoinColumn(name = "child_id")
    private Child child;
}
@Data
@Entity
@Audited
@NoArgsConstructor
public class Child {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;

    private String description;
}

I've extended Parent with RevisionRepository

@Repository
public interface ParentRepository extends RevisionRepository<Parent, Long, Long>, JpaRepository<Parent, Long>

And annotated my SpringBootApplication entry class with: @EnableJpaRepositories(repositoryFactoryBeanClass = EnversRevisionRepositoryFactoryBean.class)

I couldn't find any explanation for this so far, how can make parentRepository get what I need?

1 Answers1

0

The underlying problem here is that the reference from a versioned entity isn't really properly defined. Which variant of the reference should be returned? The one at the start of the version you use as a basis, the one at the end? The one that exists right now?

There are scenarios for which each variant makes sense.

Therefor you have to query the revisions yourself and can't simply navigate to them.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348