I have an entity
@Entity
public class FirstEntity {
@Id
@Column(name = "id", nullable = false)
private UUID id;
@Column(name = "second_id")
private UUID secondId;
@ManyToOne
@JoinColumn(name = "second_id", nullable = false, insertable = false, updatable = false)
private SecondEntity second;
}
The idea behind the double mapping of second_id
is to prevent inserting/updating a SecondEntity
while saving a FirstEntity
. So when I save a FirstEntity
, I just set the secondId
field. But when I read it, the second
field is all set.
My current problem is with this usecase:
- I save a single FirstEntity
- Then I get all of them for some logic
--> Hibernate use the FirstEntity
it has in the persistent context (set after the save) when getting all entities : that entity has a null second
field.
I can't find a clean way to fix this.
Should I set both secondId
and second
fields ?
Should I remove the new entity from the persistent context for force a clean fetch ?
Also, I use spring transaction.
Thanks :)