eXPRESS
eXPRESS

Reputation: 475

Java JPA loaded OneToMany list not updated after change

Maybe it is a really stupid question but I just can't find solution. I have:

@OneToMany(mappedBy = "project", fetch = FetchType.LAZY)
private List<Comment> comments;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "project_id")
private Project project;

When I launch application, I retrieve all comments by getter (it is loaded properly). When I however delete/add another comment, database is updated but application is not until rerun. When creating comment it is persisted - it is in DB - so I do not understand how is it not updated. Tried various cascade types but I do not think it is necessary here anyway. I also tried EAGER and it does not work as well.

When I manually get comments from DB after adding/deleting it works but that is I think very dirty solution right?

Thanks for any suggestions.

Upvotes: 1

Views: 1095

Answers (1)

Antoniossss
Antoniossss

Reputation: 32535

You have to fetch that entity you pasted a piece of code of once again. Already fetched list will not magicly update itself.

Its like using SQL - you have rerun the query to see the updates. ORM just maps SQL results row into objects.

Upvotes: 1

Related Questions