HHH-4688 Make sure @OrderBy works for @ElementCollection. added test case

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@18420 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Scott Marlow 2010-01-06 17:01:04 +00:00
parent 71d9459a20
commit 99e43e9d14
3 changed files with 134 additions and 0 deletions

View File

@ -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<Widgets> set = new HashSet<Widgets>();
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();
}
}

View File

@ -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> widgets;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Set<Widgets> getWidgets() {
return widgets;
}
public void setWidgets(Set<Widgets> widgets) {
this.widgets = widgets;
}
}

View File

@ -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;
}
}