Skip to content
Advertisement

Nested entities contains null after save

I have an entity with some nested entities like this

JavaScript

with entity2 and entity3 like this:

JavaScript

Both Entity2 and Entity3 have values stored in the database so when I’m doing an insert on MyEntity, I’m doing this:

JavaScript

it works fine, the data are stored correctly in the DB with the correct foreign keys BUT… After insert I want to build a DTO which contains id, code and desc from the nested entities so something like this:

JavaScript

Here is the problem, I only got the id fields and null on code and description.

When I call getDto() in a search it works and every field has the correct values, so it is something related to the insert transaction? how could I solve this?

Advertisement

Answer

When you create the DTO, the (existing) entities are not attached to the persistence context, so the corresponding data has not been loaded from the DB and cannot be copied to the DTO.

One option would be to load the entities e.g. via ‘myRepository.findById…’ and associate the returned (managed) entities.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement