spring-data-jpa-entity-graph icon indicating copy to clipboard operation
spring-data-jpa-entity-graph copied to clipboard

Circular dependencies when using @EntityListeners

Open s-seidel opened this issue 4 years ago • 0 comments

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:

  • Spring Data JPA version (not the Spring Boot one): 2.3.4.RELEASE
  • ORM with version: 5.4.21.Final
  • spring-data-jpa-entity-graph version: 2.3.1, 2.4.2, 2.5.0

Things I have tried (none have worked):

  • change from constructor injection to @Autowired - variable will be null
  • change from EntityManager to requiring a Repository - no change
  • use @PostUpdate annotation in the Entity class - same error

s-seidel avatar Jul 14 '21 12:07 s-seidel