- Notifications
You must be signed in to change notification settings - Fork3.7k
HHH-19599 : Test demonstrating that a collection accessed via a getter and using Collections#unmodifiableSet is not persisted.#10497
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Uh oh!
There was an error while loading.Please reload this page.
Conversation
…egistration is present
…r and using Collections#unmodifiableSet is not persisted.
@OneToMany | ||
@Access(AccessType.PROPERTY) | ||
public Set<Command> getCommands() { | ||
return Collections.unmodifiableSet( commands ); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
This really isn't a good thing to do. This means that every time Hibernate accesses the collection, it sees a new object. You have two choices:
- Use field access instead of property access (I have no clue why you're using
@Access(AccessType.PROPERTY)
here to begin with, or - Call
Collections.unmodifiableSet()
in the setter or where you initialize the field.
Otherwise I would say that this is something that's just not supported.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I simplified things for the test, but in reality, we have a class hierarchy where the behavior differs for a given attribute depending on the subclass.
We use property access to configure things differently.
Ideally, we would need to refactor the class model to avoid using property access.
Thank you!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
And as for option 2, it’s not feasible because we still want the collection to be modifiable internally within the class, but not externally.
But this approach has its limits — and this case proves it.
Uh oh!
There was an error while loading.Please reload this page.
HHH-19599 : Test demonstrating that a collection accessed via a getter and using Collections#unmodifiableSet is not persisted.
By submitting this pull request, I confirm that my contribution is made under the terms of theApache 2.0 license
and can be relicensed under the terms of theLGPL v2.1 license in the future at the maintainers' discretion.
For more information on licensing, please checkhere.
https://hibernate.atlassian.net/browse/HHH-19589
https://hibernate.atlassian.net/browse/HHH-19599