Skip to content

Circular dependencies when using @EntityListeners #59

Open
@s-seidel

Description

@s-seidel

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions