|
23 | 23 | */
|
24 | 24 | package org.hibernate.test.sorted;
|
25 | 25 |
|
| 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 | + |
26 | 31 | import java.util.Iterator;
|
| 32 | +import java.util.SortedSet; |
| 33 | +import java.util.TreeSet; |
27 | 34 |
|
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; |
29 | 41 |
|
30 | 42 | import org.hibernate.FetchMode;
|
31 | 43 | import org.hibernate.Hibernate;
|
32 | 44 | import org.hibernate.Session;
|
33 | 45 | import org.hibernate.Transaction;
|
| 46 | +import org.hibernate.annotations.SortNatural; |
| 47 | +import org.hibernate.testing.TestForIssue; |
34 | 48 | 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; |
39 | 50 |
|
40 | 51 | /**
|
41 | 52 | * @author Gavin King
|
| 53 | + * @author Brett Meyer |
42 | 54 | */
|
43 | 55 | public class SortTest extends BaseCoreFunctionalTestCase {
|
44 |
| - public String[] getMappings() { |
| 56 | + |
| 57 | + @Override |
| 58 | + protected String[] getMappings() { |
45 | 59 | return new String[] { "sorted/Search.hbm.xml" };
|
46 | 60 | }
|
| 61 | + |
| 62 | + @Override |
| 63 | + protected Class<?>[] getAnnotatedClasses() { |
| 64 | + return new Class<?>[] { Owner.class, Cat.class }; |
| 65 | + } |
47 | 66 |
|
48 | 67 | @Test
|
49 | 68 | @SuppressWarnings( {"unchecked"})
|
@@ -92,6 +111,68 @@ public void testOrderBy() {
|
92 | 111 | tx.commit();
|
93 | 112 | sess.close();
|
94 | 113 | }
|
| 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 | + } |
95 | 176 |
|
96 | 177 | }
|
97 | 178 |
|
0 commit comments