-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Wip/6.0 HHH-13877 Make@SortNatural default for SortedSet and SortedMap
#3233
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
Merged
sebersole
merged 1 commit into
hibernate:wip/6.0
from
NathanQingyangXu:SortNatual-by-default-for-Sorted
Mar 19, 2020
Merged
Changes from all commits
Commits
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
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
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 |
|---|---|---|
|
|
@@ -47,12 +47,15 @@ public void createData(SessionFactoryScope scope) { | |
| entityContainingMaps.addBasicByBasic( "someKey", "someValue" ); | ||
| entityContainingMaps.addBasicByBasic( "anotherKey", "anotherValue" ); | ||
|
|
||
| entityContainingMaps.addSortedBasicByBasic( "key1", "value1" ); | ||
| entityContainingMaps.addSortedBasicByBasic( "key2", "value2" ); | ||
| entityContainingMaps.addSortedBasicByBasic( "key1", "value1" ); | ||
|
||
|
|
||
| entityContainingMaps.addSortedBasicByBasicWithComparator( "kEy1", "value1" ); | ||
| entityContainingMaps.addSortedBasicByBasicWithComparator( "KeY2", "value2" ); | ||
|
|
||
| entityContainingMaps.addSortedBasicByBasicWithSortNaturalByDefault( "key2", "value2" ); | ||
| entityContainingMaps.addSortedBasicByBasicWithSortNaturalByDefault( "key1", "value1" ); | ||
|
|
||
| entityContainingMaps.addBasicByEnum( EnumValue.ONE, "one" ); | ||
| entityContainingMaps.addBasicByEnum( EnumValue.TWO, "two" ); | ||
|
|
||
|
|
@@ -164,6 +167,30 @@ public void testSortedMapWithComparatorAccess(SessionFactoryScope scope) { | |
| ); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSortedMapWithSortNaturalByDefaultAccess(SessionFactoryScope scope) { | ||
| scope.inTransaction( | ||
| session -> { | ||
| final EntityOfMaps entity = session.get( EntityOfMaps.class, 1 ); | ||
| assertThat( entity.getSortedBasicByBasicWithSortNaturalByDefault(), InitializationCheckMatcher.isNotInitialized() ); | ||
|
|
||
| // trigger the init | ||
| Hibernate.initialize( entity.getSortedBasicByBasicWithSortNaturalByDefault() ); | ||
| assertThat( entity.getSortedBasicByBasicWithSortNaturalByDefault(), InitializationCheckMatcher.isInitialized() ); | ||
| assertThat( entity.getSortedBasicByBasicWithSortNaturalByDefault().size(), is( 2 ) ); | ||
| assertThat( entity.getBasicByEnum(), InitializationCheckMatcher.isNotInitialized() ); | ||
|
|
||
| final Iterator<Map.Entry<String, String>> iterator = entity.getSortedBasicByBasicWithSortNaturalByDefault().entrySet().iterator(); | ||
| final Map.Entry<String, String> first = iterator.next(); | ||
| final Map.Entry<String, String> second = iterator.next(); | ||
| assertThat( first.getKey(), is( "key1" ) ); | ||
| assertThat( first.getValue(), is( "value1" ) ); | ||
| assertThat( second.getKey(), is( "key2" ) ); | ||
| assertThat( second.getValue(), is( "value2" ) ); | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| public void testOrderedMap(SessionFactoryScope scope) { | ||
| // atm we can only check the fragment translation | ||
|
|
||
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
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
185 changes: 185 additions & 0 deletions
185
.../java/org/hibernate/orm/test/metamodel/mapping/collections/SortNaturalByDefaultTests.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,185 @@ | ||
| package org.hibernate.orm.test.metamodel.mapping.collections; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.Objects; | ||
| import java.util.SortedSet; | ||
| import java.util.TreeSet; | ||
| import javax.persistence.CascadeType; | ||
| import javax.persistence.Entity; | ||
| import javax.persistence.GeneratedValue; | ||
| import javax.persistence.Id; | ||
| import javax.persistence.OneToMany; | ||
|
|
||
| import org.hamcrest.collection.IsIterableContainingInOrder; | ||
|
|
||
| import org.hibernate.testing.TestForIssue; | ||
| import org.hibernate.testing.orm.junit.DomainModel; | ||
| import org.hibernate.testing.orm.junit.ServiceRegistry; | ||
| import org.hibernate.testing.orm.junit.SessionFactory; | ||
| import org.hibernate.testing.orm.junit.SessionFactoryScope; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.hamcrest.MatcherAssert.assertThat; | ||
|
|
||
| /** | ||
| * Tests Hibernate specific feature that {@link org.hibernate.annotations.SortNatural @SortNatural} will take effect implicitly | ||
| * when no <i>sort</i> or <i>order</i> related annotations exist, including: | ||
| * <ul> | ||
| * <li>{@link org.hibernate.annotations.SortNatural @SortNatural}</li> | ||
| * <li>{@link org.hibernate.annotations.SortComparator @SortComparator}</li> | ||
| * <li>{@link org.hibernate.annotations.Sort @Sort}</li> | ||
| * <li>{@link org.hibernate.annotations.OrderBy @OrderBy(from hibernate)}</li> | ||
| * <li>{@link javax.persistence.OrderBy @OrderBy(from JPA)}</li> | ||
| * </ul> | ||
| * | ||
| * @author Nathan Xu | ||
| */ | ||
| @ServiceRegistry | ||
| @DomainModel( | ||
| annotatedClasses = { | ||
| SortNaturalByDefaultTests.Person.class, | ||
| SortNaturalByDefaultTests.Phone.class | ||
| } | ||
| ) | ||
| @SessionFactory | ||
| @TestForIssue( jiraKey = "HHH-13877" ) | ||
| public class SortNaturalByDefaultTests { | ||
|
|
||
| @Test | ||
| void test(SessionFactoryScope scope) { | ||
| scope.inTransaction( | ||
| session -> { | ||
| final Person person = session.createQuery( "select p from Person p", Person.class ).getSingleResult(); | ||
| final SortedSet<Phone> phones = person.getPhones(); | ||
| assertThat( phones, IsIterableContainingInOrder.contains( | ||
| new Phone( "123-456-789" ), | ||
| new Phone( "234-567-891" ), | ||
| new Phone( "345-678-912" ), | ||
| new Phone( "456-789-123" ), | ||
| new Phone( "567-891-234" ), | ||
| new Phone( "678-912-345" ), | ||
| new Phone( "789-123-456" ), | ||
| new Phone( "891-234-567" ), | ||
| new Phone( "912-345-678" ) ) | ||
| ); | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| @BeforeEach | ||
| void createTestData(SessionFactoryScope scope) { | ||
| scope.inTransaction( | ||
| session -> { | ||
| final Person person = new Person(); | ||
| final SortedSet<Phone> phones = new TreeSet<>( | ||
| Arrays.asList( | ||
| new Phone( "678-912-345" ), | ||
| new Phone( "234-567-891" ), | ||
| new Phone( "567-891-234" ), | ||
| new Phone( "456-789-123" ), | ||
| new Phone( "123-456-789" ), | ||
| new Phone( "912-345-678" ), | ||
| new Phone( "789-123-456" ), | ||
| new Phone( "345-678-912" ), | ||
| new Phone( "891-234-567" ) | ||
| ) | ||
| ); | ||
| person.setPhones( phones ); | ||
| session.persist( person ); | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| @AfterEach | ||
| void cleanUpTestData(SessionFactoryScope scope) { | ||
| scope.inTransaction( | ||
| session -> { | ||
| session.createQuery( "delete Person" ).executeUpdate(); | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| @Entity(name = "Person") | ||
| public static class Person { | ||
|
|
||
| @Id | ||
| @GeneratedValue | ||
| private Long id; | ||
|
|
||
| @OneToMany(cascade = CascadeType.ALL) | ||
| private SortedSet<Phone> phones = new TreeSet<>(); // no '@SortNatural', '@SortComparator' or '@OrderBy' annotation present | ||
|
|
||
| public Long getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public void setId(Long id) { | ||
| this.id = id; | ||
| } | ||
|
|
||
| public SortedSet<Phone> getPhones() { | ||
| return phones; | ||
| } | ||
|
|
||
| public void setPhones(SortedSet<Phone> phones) { | ||
| this.phones = phones; | ||
| } | ||
| } | ||
|
|
||
| @Entity(name = "Phone") | ||
| public static class Phone implements Comparable<Phone> { | ||
|
|
||
| @Id | ||
| @GeneratedValue | ||
| private Long id; | ||
|
|
||
| private String number; | ||
|
|
||
| public Phone() { | ||
| } | ||
|
|
||
| public Phone(String number) { | ||
| this.number = number; | ||
| } | ||
|
|
||
| public Long getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public void setId(Long id) { | ||
| this.id = id; | ||
| } | ||
|
|
||
| public String getNumber() { | ||
| return number; | ||
| } | ||
|
|
||
| public void setNumber(String number) { | ||
| this.number = number; | ||
| } | ||
|
|
||
| @Override | ||
| public int compareTo(Phone o) { | ||
| return number.compareTo( o.getNumber() ); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if ( this == o ) { | ||
| return true; | ||
| } | ||
| if ( o == null || getClass() != o.getClass() ) { | ||
| return false; | ||
| } | ||
| Phone phone = (Phone) o; | ||
| return Objects.equals( number, phone.number ); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash( number ); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
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.
I manually compared to explicit
@SortNaturalcase and found everything is the same(onlyhadExplicitSortvalues are different but it is used internally in this method and its main usage is the above code snippet), so removing the above code snippet seems enough to achieve the goal of this PR, unless I were wrong.