diff --git a/entitymanager/src/test/java/org/hibernate/ejb/test/ops/OrderByTest.java b/entitymanager/src/test/java/org/hibernate/ejb/test/ops/OrderByTest.java new file mode 100644 index 0000000000..c7bed8ec68 --- /dev/null +++ b/entitymanager/src/test/java/org/hibernate/ejb/test/ops/OrderByTest.java @@ -0,0 +1,61 @@ +package org.hibernate.ejb.test.ops; + +import org.hibernate.ejb.test.TestCase; + +import javax.persistence.EntityManager; +import java.util.HashSet; +import java.util.Iterator; + +public class OrderByTest extends TestCase { + + public Class[] getAnnotatedClasses() { + return new Class[]{ + Products.class, + Widgets.class + }; + } + + /** + * Test @OrderBy on the Widgets.name field. + * + */ + public void testOrderByName() throws Exception { + EntityManager em = getOrCreateEntityManager(); + em.getTransaction().begin(); + + Products p = new Products(); + HashSet set = new HashSet(); + + Widgets widget = new Widgets(); + widget.setName("hammer"); + set.add(widget); + em.persist(widget); + + widget = new Widgets(); + widget.setName("axel"); + set.add(widget); + em.persist(widget); + + widget = new Widgets(); + widget.setName("screwdriver"); + set.add(widget); + em.persist(widget); + + p.setWidgets(set); + em.persist(p); + em.getTransaction().commit(); + + em.getTransaction().begin(); + em.clear(); + p = em.find(Products.class,p.getId()); + assertTrue("has three Widgets", p.getWidgets().size() == 3); + Iterator iter = p.getWidgets().iterator(); + assertEquals( "axel", ((Widgets)iter.next()).getName() ); + assertEquals( "hammer", ((Widgets)iter.next()).getName() ); + assertEquals( "screwdriver", ((Widgets)iter.next()).getName() ); + em.getTransaction().commit(); + em.close(); + } + + +} diff --git a/entitymanager/src/test/java/org/hibernate/ejb/test/ops/Products.java b/entitymanager/src/test/java/org/hibernate/ejb/test/ops/Products.java new file mode 100644 index 0000000000..29a8a415fe --- /dev/null +++ b/entitymanager/src/test/java/org/hibernate/ejb/test/ops/Products.java @@ -0,0 +1,39 @@ +package org.hibernate.ejb.test.ops; + +import javax.persistence.ElementCollection; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.OrderBy; + +import java.util.Set; + +@SuppressWarnings({"unchecked", "serial"}) + +@Entity +public class Products { + @Id + @GeneratedValue + private Integer id; + + @ElementCollection + @OrderBy("name ASC") + private Set widgets; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public Set getWidgets() { + return widgets; + } + + public void setWidgets(Set widgets) { + this.widgets = widgets; + } + +} \ No newline at end of file diff --git a/entitymanager/src/test/java/org/hibernate/ejb/test/ops/Widgets.java b/entitymanager/src/test/java/org/hibernate/ejb/test/ops/Widgets.java new file mode 100644 index 0000000000..6f415009d2 --- /dev/null +++ b/entitymanager/src/test/java/org/hibernate/ejb/test/ops/Widgets.java @@ -0,0 +1,34 @@ +package org.hibernate.ejb.test.ops; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; + +@Entity +public class Widgets { + private String name; + private int id; + + public Widgets() { + + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Id + @GeneratedValue + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + +}