Skip to content

Commit 770a1cb

Browse files
committed
HHH-8827 corrected @SortNatural and @SortComparator, test case
1 parent 26f19d0 commit 770a1cb

File tree

3 files changed

+104
-6
lines changed

3 files changed

+104
-6
lines changed

hibernate-core/src/main/java/org/hibernate/annotations/SortComparator.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323
*/
2424
package org.hibernate.annotations;
2525

26+
import static java.lang.annotation.ElementType.FIELD;
27+
import static java.lang.annotation.ElementType.METHOD;
28+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
29+
30+
import java.lang.annotation.Retention;
31+
import java.lang.annotation.Target;
2632
import java.util.Comparator;
2733

2834
/**
@@ -38,6 +44,8 @@
3844
*
3945
* @author Steve Ebersole
4046
*/
47+
@Target({METHOD, FIELD})
48+
@Retention(RUNTIME)
4149
public @interface SortComparator {
4250
/**
4351
* Specifies the comparator class to use.

hibernate-core/src/main/java/org/hibernate/annotations/SortNatural.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@
2323
*/
2424
package org.hibernate.annotations;
2525

26+
import static java.lang.annotation.ElementType.FIELD;
27+
import static java.lang.annotation.ElementType.METHOD;
28+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
29+
30+
import java.lang.annotation.Retention;
31+
import java.lang.annotation.Target;
32+
2633
/**
2734
* Specifies in-memory Set/Map sorting using natural sorting.
2835
*
@@ -36,5 +43,7 @@
3643
*
3744
* @author Steve Ebersole
3845
*/
46+
@Target({METHOD, FIELD})
47+
@Retention(RUNTIME)
3948
public @interface SortNatural {
4049
}

hibernate-core/src/test/java/org/hibernate/test/sorted/SortTest.java

Lines changed: 87 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,46 @@
2323
*/
2424
package org.hibernate.test.sorted;
2525

26+
import static org.junit.Assert.assertEquals;
27+
import static org.junit.Assert.assertFalse;
28+
import static org.junit.Assert.assertNotNull;
29+
import static org.junit.Assert.assertTrue;
30+
2631
import java.util.Iterator;
32+
import java.util.SortedSet;
33+
import java.util.TreeSet;
2734

28-
import org.junit.Test;
35+
import javax.persistence.CascadeType;
36+
import javax.persistence.Entity;
37+
import javax.persistence.GeneratedValue;
38+
import javax.persistence.Id;
39+
import javax.persistence.ManyToOne;
40+
import javax.persistence.OneToMany;
2941

3042
import org.hibernate.FetchMode;
3143
import org.hibernate.Hibernate;
3244
import org.hibernate.Session;
3345
import org.hibernate.Transaction;
46+
import org.hibernate.annotations.SortNatural;
47+
import org.hibernate.testing.TestForIssue;
3448
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
35-
36-
import static org.junit.Assert.assertEquals;
37-
import static org.junit.Assert.assertFalse;
38-
import static org.junit.Assert.assertTrue;
49+
import org.junit.Test;
3950

4051
/**
4152
* @author Gavin King
53+
* @author Brett Meyer
4254
*/
4355
public class SortTest extends BaseCoreFunctionalTestCase {
44-
public String[] getMappings() {
56+
57+
@Override
58+
protected String[] getMappings() {
4559
return new String[] { "sorted/Search.hbm.xml" };
4660
}
61+
62+
@Override
63+
protected Class<?>[] getAnnotatedClasses() {
64+
return new Class<?>[] { Owner.class, Cat.class };
65+
}
4766

4867
@Test
4968
@SuppressWarnings( {"unchecked"})
@@ -92,6 +111,68 @@ public void testOrderBy() {
92111
tx.commit();
93112
sess.close();
94113
}
114+
115+
@Test
116+
@TestForIssue(jiraKey = "HHH-8827")
117+
public void testSortNatural() {
118+
Session s = openSession();
119+
s.beginTransaction();
120+
121+
Owner owner = new Owner();
122+
Cat cat1 = new Cat();
123+
Cat cat2 = new Cat();
124+
cat1.owner = owner;
125+
cat1.name = "B";
126+
cat2.owner = owner;
127+
cat2.name = "A";
128+
owner.cats.add( cat1 );
129+
owner.cats.add( cat2 );
130+
s.persist( owner );
131+
132+
s.getTransaction().commit();
133+
s.clear();
134+
135+
s.beginTransaction();
136+
137+
owner = (Owner) s.get( Owner.class, owner.id );
138+
assertNotNull(owner.cats);
139+
assertEquals(owner.cats.size(), 2);
140+
assertEquals(owner.cats.first().name, "A");
141+
assertEquals(owner.cats.last().name, "B");
142+
143+
s.getTransaction().commit();
144+
s.close();
145+
}
146+
147+
@Entity
148+
private static class Owner {
149+
150+
@Id
151+
@GeneratedValue
152+
private long id;
153+
154+
@OneToMany(mappedBy = "owner", cascade = CascadeType.ALL)
155+
@SortNatural
156+
private SortedSet<Cat> cats = new TreeSet<Cat>();
157+
}
158+
159+
@Entity
160+
private static class Cat implements Comparable<Cat> {
161+
162+
@Id
163+
@GeneratedValue
164+
private long id;
165+
166+
@ManyToOne
167+
private Owner owner;
168+
169+
private String name;
170+
171+
@Override
172+
public int compareTo(Cat cat) {
173+
return this.name.compareTo( cat.name );
174+
}
175+
}
95176

96177
}
97178

0 commit comments

Comments
 (0)