Francois Gergaud
Francois Gergaud

Reputation: 404

Spring Transaction propagation: can't get the @OneToMany related entities when using the same transaction for creation and consultation operation

I have the following problem: I am working on a spring-boot application which offers REST services and use a relational (SQL) database using spring-data-jpa.

I have two REST services: - a entity-creation service, which create the child-entity, the parent-entity and associate them in a same transaction. When this service ends, the data are committed into the database. - an entity consultation service, which get back the parent-entity with its children

These two services are annotated with the @Transactional annotation. It production case, it works well: I can create an parent-entity with its children in one transaction (which is commited/ended), and get it in another transaction latter.

The problem is when I want to create integration-tests. My idea was to annotate each test with the @Transactional annotation, and do a rollback after each test. This way I keep my database clean between each test, and I don't have a generate the schema again or clean all the records in the database.

The integration test consists in creating a parent and its children and then reading it, everything in one transaction (as the test is annotated with @Transaction). When reading the entity previously created in the same transaction, I can get the parent entity, but the children are not fetched (null value). I am not sure to understand very well the transaction mechanism: I was thinking that using the @Transactional on the test method, the services (annotated with "@Transactional") invoked by this test should detect and use the same transaction opened by the test method (the propagation is configured to "REQUIRED"). Hence as the transaction uses the same EntityManager, this one should be able to return the relation between the parent entity and its children created previously in the same transaction, even if the data has not been committed to the database. The strange thing is that it retrieve the parent entity (which has not been yet committed into the database), but not its children. Is my understanding of the transaction concept correct? If not, could someone explains me what am I missing?

Also, if someone did something similar, could he explain me how he did it please?

My code is quite complex. I first want to know if I understand well how are transaction managed and if someone already did something similar. If really it is required, I can send more information about my implementation (how the transaction-manager and the entity-manager are initialized, the JPA entities, the services etc...)

Upvotes: 1

Views: 886

Answers (1)

Francois Gergaud
Francois Gergaud

Reputation: 404

Binding the Entity-manager in my test and calling its flush method from my test,between the creation and the reading, the reading operation works well: I get the parent entity with its children. But the data are written into the database during the creation to read it latter during the read operation. And I don't want the transaction to be committed as I need my test to work on an empty database. My misunderstanding is not so much about the Transaction mechanism, but more about the entity-manager: it does not keep as a cache the entities created and theirs relations...

This post help me. Issue with @Transactional annotations in Spring JPA

As a final word, I am thinking about calling an SQL script before each test to empty my database.

Upvotes: 0

Related Questions