Hello,

Saving then fetching does not provide relations, but new calls does.

It looks like some caching case.

I made a minimalist project to reproduce the issue :

https://github.com/superkeil/jpabug

Java 17, SpringBoot 3.3.3

I encountered this problem on a big project with postgres, but the same thing happen with h2.

What are the differents ways to make the first call work correctly ?

Comment From: philwebb

Thanks for getting in touch, but it feels like this is a question that would be better suited to Stack Overflow. As mentioned in the guidelines for contributing, we prefer to use GitHub issues only for bugs and enhancements. Feel free to update this issue with a link to the re-posted question (so that other people can find it) or add some more details if you feel this is a genuine bug.

Comment From: superkeil

@philwebb thank you for your reply.

I understand this is probably better to post that to stackoverflow.

I did it at first, but they deleted the question because they considered it "off-topic". So I post it here, hoping that i would receive an answer, so please be kind

Comment From: philwebb

@superkeil Perhaps it was marked as off topic because it's really a JPA question, not Spring Boot. Your problem is that you are calling book.setAuthorId(author); and you should be calling book.setAuthor(author);. I think Hibernate is caching the saved Book instance and returning it without fetching the Author. You really should not be using IDs directly to model relationships in JPA.

Comment From: superkeil

@philwebb, thanks for pointing JPA. I will ask in their forum. Thanks for the good practice too

Comment From: superkeil

for people with the same problem, i found a fix, entityManager.clear();