From 2f636e4a52597e9eeae5ebe768594e493313d8a1 Mon Sep 17 00:00:00 2001 From: Brett Meyer Date: Thu, 9 Jan 2014 11:59:13 -0500 Subject: [PATCH] HHH-8827 corrected @SortNatural and @SortComparator, test case --- .../hibernate/annotations/SortComparator.java | 8 ++ .../hibernate/annotations/SortNatural.java | 9 ++ .../org/hibernate/test/sorted/SortTest.java | 95 +++++++++++++++++-- 3 files changed, 105 insertions(+), 7 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/annotations/SortComparator.java b/hibernate-core/src/main/java/org/hibernate/annotations/SortComparator.java index 6ed0c2710a..83fb074931 100644 --- a/hibernate-core/src/main/java/org/hibernate/annotations/SortComparator.java +++ b/hibernate-core/src/main/java/org/hibernate/annotations/SortComparator.java @@ -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. diff --git a/hibernate-core/src/main/java/org/hibernate/annotations/SortNatural.java b/hibernate-core/src/main/java/org/hibernate/annotations/SortNatural.java index 9fc285dedc..1c3ad5a61c 100644 --- a/hibernate-core/src/main/java/org/hibernate/annotations/SortNatural.java +++ b/hibernate-core/src/main/java/org/hibernate/annotations/SortNatural.java @@ -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 { } diff --git a/hibernate-core/src/test/java/org/hibernate/test/sorted/SortTest.java b/hibernate-core/src/test/java/org/hibernate/test/sorted/SortTest.java index c78277ccb3..b63695cc2f 100755 --- a/hibernate-core/src/test/java/org/hibernate/test/sorted/SortTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/sorted/SortTest.java @@ -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 cats = new TreeSet(); + } + + @Entity + private static class Cat implements Comparable { + + @Id + @GeneratedValue + private long id; + + @ManyToOne + private Owner owner; + + private String name; + + @Override + public int compareTo(Cat cat) { + return this.name.compareTo( cat.name ); + } + } }