1

This question is similar/related to this other, but more specifically focused on a non-owning @OneToMany relation side. Given the following mapped getter:

...
private List<Leaf> leaves;
...
@OneToMany(mappedBy = "extkey")
@LazyCollection(LazyCollectionOption.TRUE)
public List<Leaf> getLeaves() {
    return leaves;
}
...

What would be the impact of adding the transient modifier to leaves field in this case?

The relation is lazy and 'reversed', ie the owning side is Leaf object. The code was working before and looks working still now, but I wonder about any undesired subtle side effect, since transient has some peculiarity in JPA context. I needed this as passing the entity instance to an external Gson lib would loop and stackoverflow, I guess bc the bi-directional relation creates a loop and I could avoid it using transient field. Thanks

Shine
  • 3,788
  • 1
  • 36
  • 59

1 Answers1

0

The impact will be when you deserialize your entity since the object state will be different after serialization.

Anyway, you didn't specify why you needed this modifier.

Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
  • I added the reason, sadly I have no control over that lib so that's quite unrelevant to me – Shine Apr 17 '19 at 17:55
  • No, you didn't. I re-read your question and there's no reference to why you needed transient. – Vlad Mihalcea Apr 17 '19 at 18:07
  • "I needed this as passing the entity instance to an external Gson lib would loop and stackoverflow [...]" do you think it's not clear? – Shine Apr 17 '19 at 18:09
  • Yes, it's not clear. What Gson lib? What StackOverflow error? Why would transient solve that? Sounds like you needed something like `@JsonIgnore`. – Vlad Mihalcea Apr 17 '19 at 18:14
  • AFAIK there's no such annotation in gson, you could do it with plain transient, @Expose and excludeFieldsWithoutExposeAnnotation(), or ExclusionStrategy. Having a little time, the first was the easiest to choose – Shine Apr 17 '19 at 18:20
  • I've never used Gson, so I can't help you with that. You need to ask the framework developers for a solution. – Vlad Mihalcea Apr 17 '19 at 18:29