-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
boutss
wants to merge
2
commits into
hibernate:main
Choose a base branch
from
boutss:HHH-19599
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
102 changes: 102 additions & 0 deletions
102
...t/java/org/hibernate/orm/test/annotations/onetomany/OneToManyWithUnmodifiableSetTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright Red Hat Inc. and Hibernate Authors | ||
*/ | ||
package org.hibernate.orm.test.annotations.onetomany; | ||
|
||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
import org.hibernate.testing.orm.junit.DomainModel; | ||
import org.hibernate.testing.orm.junit.JiraKey; | ||
import org.hibernate.testing.orm.junit.SessionFactory; | ||
import org.hibernate.testing.orm.junit.SessionFactoryScope; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import jakarta.persistence.Access; | ||
import jakarta.persistence.AccessType; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.OneToMany; | ||
import jakarta.persistence.PreUpdate; | ||
import jakarta.persistence.Transient; | ||
import org.assertj.core.api.InstanceOfAssertFactories; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Allows testing a collection accessed via a getter that returns a {@link Collections#unmodifiableSet(Set)} | ||
* | ||
* @author Vincent Bouthinon | ||
*/ | ||
@DomainModel( | ||
annotatedClasses = { | ||
OneToManyWithUnmodifiableSetTest.Client.class, | ||
OneToManyWithUnmodifiableSetTest.Command.class | ||
} | ||
) | ||
@SessionFactory | ||
@JiraKey(value = "HHH-19589") | ||
public class OneToManyWithUnmodifiableSetTest { | ||
|
||
@Test | ||
void test(SessionFactoryScope scope) { | ||
scope.inTransaction( session -> { | ||
Client client = new Client(); | ||
Command command = new Command(); | ||
client.addToCommands( command ); | ||
session.persist( client ); | ||
session.persist( command ); | ||
session.flush(); | ||
session.clear(); | ||
Client clientFind = session.find( Client.class, client.id ); | ||
assertThat( clientFind ) | ||
.isNotNull() | ||
.extracting( Client::getCommands ) | ||
.asInstanceOf( InstanceOfAssertFactories.COLLECTION ) | ||
.isNotEmpty(); | ||
} ); | ||
} | ||
|
||
|
||
@Entity(name = "Client") | ||
public static class Client { | ||
@Transient | ||
protected Set<Command> commands = new HashSet<>(); | ||
@Id | ||
@GeneratedValue | ||
private Long id; | ||
|
||
@OneToMany | ||
@Access(AccessType.PROPERTY) | ||
public Set<Command> getCommands() { | ||
return Collections.unmodifiableSet( commands ); | ||
} | ||
|
||
public void setCommands(Set<Command> commands) { | ||
this.commands = commands; | ||
} | ||
|
||
public void addToCommands(Command command) { | ||
this.commands.add( command ); | ||
} | ||
|
||
/** | ||
* It triggers the {@link org.hibernate.event.internal.DefaultFlushEntityEventListener#copyState} method, because otherwise, if there’s no callbackPreUpdate, | ||
* the system assumes the entity isn’t dirty and doesn’t perform copyState to check for dirtiness. | ||
*/ | ||
@PreUpdate | ||
public void preUpdate() { | ||
} | ||
} | ||
|
||
@Entity(name = "Command") | ||
public static class Command { | ||
|
||
@Id | ||
@GeneratedValue | ||
private Long id; | ||
} | ||
} |
102 changes: 102 additions & 0 deletions
102
...ibernate/orm/test/mapping/converted/converter/ConverterOverrideTypeRegisttrationTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright Red Hat Inc. and Hibernate Authors | ||
*/ | ||
package org.hibernate.orm.test.mapping.converted.converter; | ||
|
||
import java.util.BitSet; | ||
|
||
import org.hibernate.annotations.TypeRegistration; | ||
import org.hibernate.orm.test.mapping.basic.bitset.BitSetHelper; | ||
import org.hibernate.orm.test.mapping.basic.bitset.BitSetUserType; | ||
|
||
import org.hibernate.testing.orm.junit.DomainModel; | ||
import org.hibernate.testing.orm.junit.JiraKey; | ||
import org.hibernate.testing.orm.junit.SessionFactory; | ||
import org.hibernate.testing.orm.junit.SessionFactoryScope; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import jakarta.persistence.AttributeConverter; | ||
import jakarta.persistence.Convert; | ||
import jakarta.persistence.Converter; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.Id; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* <pre> | ||
* The @Converter should take precedence over @TypeRegistration. | ||
* This test shows that this is not the case. | ||
* | ||
* To ensure that the @Converter is taken into account without @TypeRegistration, you just need to remove the @TypeRegistration. | ||
* </pre> | ||
* | ||
* @author Vincent Bouthinon | ||
*/ | ||
@DomainModel( | ||
annotatedClasses = { | ||
ConverterOverrideTypeRegisttrationTest.SimpleEntity.class | ||
} | ||
) | ||
@SessionFactory | ||
@JiraKey(value = "HHH-19589") | ||
public class ConverterOverrideTypeRegisttrationTest { | ||
|
||
@Test | ||
void test(SessionFactoryScope scope) { | ||
scope.inTransaction( session -> { | ||
final SimpleEntity object = new SimpleEntity( 77L ); | ||
BitSet bitSet = new BitSet(); | ||
bitSet.set( 0, true ); | ||
object.setBitSet( bitSet ); | ||
session.persist( object ); | ||
session.flush(); | ||
session.clear(); | ||
SimpleEntity simpleEntity = session.find( SimpleEntity.class, object.id ); | ||
assertThat( simpleEntity.getBitSet().get( 7 ) ).isTrue(); | ||
} ); | ||
} | ||
|
||
|
||
@Entity(name = "SimpleEntity") | ||
@TypeRegistration(basicClass = BitSet.class, userType = BitSetUserType.class) // Remove this annotation to test the use of @Converter | ||
public static class SimpleEntity { | ||
|
||
@Id | ||
private Long id; | ||
@Convert(converter = BitSetConverter.class) | ||
private BitSet bitSet; | ||
|
||
public SimpleEntity() { | ||
} | ||
|
||
public SimpleEntity(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public BitSet getBitSet() { | ||
return bitSet; | ||
} | ||
|
||
public void setBitSet(final BitSet bitSet) { | ||
this.bitSet = bitSet; | ||
} | ||
} | ||
|
||
@Converter | ||
public static class BitSetConverter implements AttributeConverter<BitSet, String> { | ||
|
||
@Override | ||
public String convertToDatabaseColumn(final BitSet attribute) { | ||
return BitSetHelper.bitSetToString( attribute ); | ||
} | ||
|
||
@Override | ||
public BitSet convertToEntityAttribute(final String dbData) { | ||
BitSet bitSet = new BitSet(); | ||
bitSet.set( 7, true ); | ||
return bitSet; | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:
@Access(AccessType.PROPERTY)
here to begin with, orCollections.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.