Skip to content

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

boutss
Copy link
Contributor

@boutss boutss commented Jul 9, 2025

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 the Apache 2.0 license
and can be relicensed under the terms of the LGPL v2.1 license in the future at the maintainers' discretion.
For more information on licensing, please check here.


https://hibernate.atlassian.net/browse/HHH-19589
https://hibernate.atlassian.net/browse/HHH-19599

Vincent Bouthinon added 2 commits July 8, 2025 10:52
Comment on lines +72 to +76
@OneToMany
@Access(AccessType.PROPERTY)
public Set<Command> getCommands() {
return Collections.unmodifiableSet( commands );
}
Copy link
Member

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:

  1. Use field access instead of property access (I have no clue why you're using @Access(AccessType.PROPERTY) here to begin with, or
  2. 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.

Copy link
Contributor Author

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!

Copy link
Contributor Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants