HHH-8827 corrected @SortNatural and @SortComparator, test case

This commit is contained in:
Brett Meyer 2014-01-09 11:59:13 -05:00
parent ff12d5cdf1
commit 2f636e4a52
3 changed files with 105 additions and 7 deletions

View File

@ -23,6 +23,12 @@
*/
package org.hibernate.annotations;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.util.Comparator;
/**
@ -38,6 +44,8 @@ import java.util.Comparator;
*
* @author Steve Ebersole
*/
@Target({METHOD, FIELD})
@Retention(RUNTIME)
public @interface SortComparator {
/**
* Specifies the comparator class to use.

View File

@ -23,6 +23,13 @@
*/
package org.hibernate.annotations;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
/**
* Specifies in-memory Set/Map sorting using natural sorting.
*
@ -36,5 +43,7 @@ package org.hibernate.annotations;
*
* @author Steve Ebersole
*/
@Target({METHOD, FIELD})
@Retention(RUNTIME)
public @interface SortNatural {
}

View File

@ -23,27 +23,46 @@
*/
package org.hibernate.test.sorted;
import java.util.Iterator;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import java.util.Iterator;
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.ManyToOne;
import javax.persistence.OneToMany;
import org.hibernate.FetchMode;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.annotations.SortNatural;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
/**
* @author Gavin King
* @author Brett Meyer
*/
public class SortTest extends BaseCoreFunctionalTestCase {
public String[] getMappings() {
@Override
protected String[] getMappings() {
return new String[] { "sorted/Search.hbm.xml" };
}
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] { Owner.class, Cat.class };
}
@Test
@SuppressWarnings( {"unchecked"})
@ -92,6 +111,68 @@ public class SortTest extends BaseCoreFunctionalTestCase {
tx.commit();
sess.close();
}
@Test
@TestForIssue(jiraKey = "HHH-8827")
public void testSortNatural() {
Session s = openSession();
s.beginTransaction();
Owner owner = new Owner();
Cat cat1 = new Cat();
Cat cat2 = new Cat();
cat1.owner = owner;
cat1.name = "B";
cat2.owner = owner;
cat2.name = "A";
owner.cats.add( cat1 );
owner.cats.add( cat2 );
s.persist( owner );
s.getTransaction().commit();
s.clear();
s.beginTransaction();
owner = (Owner) s.get( Owner.class, owner.id );
assertNotNull(owner.cats);
assertEquals(owner.cats.size(), 2);
assertEquals(owner.cats.first().name, "A");
assertEquals(owner.cats.last().name, "B");
s.getTransaction().commit();
s.close();
}
@Entity
private static class Owner {
@Id
@GeneratedValue
private long id;
@OneToMany(mappedBy = "owner", cascade = CascadeType.ALL)
@SortNatural
private SortedSet<Cat> cats = new TreeSet<Cat>();
}
@Entity
private static class Cat implements Comparable<Cat> {
@Id
@GeneratedValue
private long id;
@ManyToOne
private Owner owner;
private String name;
@Override
public int compareTo(Cat cat) {
return this.name.compareTo( cat.name );
}
}
}