mirror of
https://github.com/hibernate/hibernate-orm
synced 2025-02-18 09:05:21 +00:00
HHH-8827 corrected @SortNatural and @SortComparator, test case
This commit is contained in:
parent
ff12d5cdf1
commit
2f636e4a52
@ -23,6 +23,12 @@
|
|||||||
*/
|
*/
|
||||||
package org.hibernate.annotations;
|
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;
|
import java.util.Comparator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -38,6 +44,8 @@
|
|||||||
*
|
*
|
||||||
* @author Steve Ebersole
|
* @author Steve Ebersole
|
||||||
*/
|
*/
|
||||||
|
@Target({METHOD, FIELD})
|
||||||
|
@Retention(RUNTIME)
|
||||||
public @interface SortComparator {
|
public @interface SortComparator {
|
||||||
/**
|
/**
|
||||||
* Specifies the comparator class to use.
|
* Specifies the comparator class to use.
|
||||||
|
@ -23,6 +23,13 @@
|
|||||||
*/
|
*/
|
||||||
package org.hibernate.annotations;
|
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.
|
* Specifies in-memory Set/Map sorting using natural sorting.
|
||||||
*
|
*
|
||||||
@ -36,5 +43,7 @@
|
|||||||
*
|
*
|
||||||
* @author Steve Ebersole
|
* @author Steve Ebersole
|
||||||
*/
|
*/
|
||||||
|
@Target({METHOD, FIELD})
|
||||||
|
@Retention(RUNTIME)
|
||||||
public @interface SortNatural {
|
public @interface SortNatural {
|
||||||
}
|
}
|
||||||
|
@ -23,27 +23,46 @@
|
|||||||
*/
|
*/
|
||||||
package org.hibernate.test.sorted;
|
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.FetchMode;
|
||||||
import org.hibernate.Hibernate;
|
import org.hibernate.Hibernate;
|
||||||
import org.hibernate.Session;
|
import org.hibernate.Session;
|
||||||
import org.hibernate.Transaction;
|
import org.hibernate.Transaction;
|
||||||
|
import org.hibernate.annotations.SortNatural;
|
||||||
|
import org.hibernate.testing.TestForIssue;
|
||||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
||||||
|
import org.junit.Test;
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import static org.junit.Assert.assertFalse;
|
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Gavin King
|
* @author Gavin King
|
||||||
|
* @author Brett Meyer
|
||||||
*/
|
*/
|
||||||
public class SortTest extends BaseCoreFunctionalTestCase {
|
public class SortTest extends BaseCoreFunctionalTestCase {
|
||||||
public String[] getMappings() {
|
|
||||||
|
@Override
|
||||||
|
protected String[] getMappings() {
|
||||||
return new String[] { "sorted/Search.hbm.xml" };
|
return new String[] { "sorted/Search.hbm.xml" };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Class<?>[] getAnnotatedClasses() {
|
||||||
|
return new Class<?>[] { Owner.class, Cat.class };
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@SuppressWarnings( {"unchecked"})
|
@SuppressWarnings( {"unchecked"})
|
||||||
@ -92,6 +111,68 @@ public void testOrderBy() {
|
|||||||
tx.commit();
|
tx.commit();
|
||||||
sess.close();
|
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 );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user