Cosium / spring-data-jpa-entity-graph

Spring Data JPA extension allowing full dynamic usage of EntityGraph on repositories
MIT License
466 stars 51 forks source link

Circular dependencies when using @EntityListeners #59

Open s-seidel opened 2 years ago

s-seidel commented 2 years ago

What steps will reproduce the problem? Steps to reproduce the behavior: I have an Entity with a Listener:

@Entity
@EntityListeners(MyEntityUpdateListener.class)
public class MyEntity {
...

and the Listener needs to modify the database, so it needs a Repository or the entityManager:

public class MyEntityUpdateListener {
    private final EntityManager entityManager;

    public MyEntityUpdateListener(EntityManager _em) {
...

When starting this, I get

***************************
APPLICATION FAILED TO START
***************************

Description:

The dependencies of some of the beans in the application context form a cycle:

...
      ↓
   myRepository defined in ....MyRepository defined in @EnableJpaRepositories declared on MyApplication
      ↓
   (inner bean)#55e91e61
┌─────┐
|  entityManagerFactory defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]
↑     ↓
|  ....MyEntityUpdateListener
↑     ↓
|  org.springframework.orm.jpa.SharedEntityManagerCreator#0
└─────┘

What is the expected output?

This works fine with standard Spring Boot.

What happens instead?

Should also work with spring-data-jpa-entity-graph

Environment:

Things I have tried (none have worked):