1 package org.hibernate.test.mapcompelem; 3 4 import java.util.List ; 5 6 import junit.framework.Test; 7 import junit.framework.TestSuite; 8 9 import org.hibernate.Session; 10 import org.hibernate.Transaction; 11 import org.hibernate.test.TestCase; 12 13 16 public class MapCompositeElementTest extends TestCase { 17 18 public MapCompositeElementTest(String str) { 19 super(str); 20 } 21 22 public void testMapCompositeElementWithFormula() { 23 Session s = openSession(); 24 Transaction t = s.beginTransaction(); 25 Part top = new Part("top", "The top part"); 26 Part bottom = new Part("bottom", "The bottom part"); 27 Product prod = new Product("Some Thing"); 28 prod.getParts().put("Top", top); 29 prod.getParts().put("Bottom", bottom); 30 s.persist(prod); 31 t.commit(); 32 s.close(); 33 34 s = openSession(); 35 t = s.beginTransaction(); 36 prod = (Product) s.get(Product.class, "Some Thing"); 37 assertEquals( prod.getParts().size(), 2 ); 38 prod.getParts().remove("Bottom"); 39 t.commit(); 40 s.close(); 41 42 s = openSession(); 43 t = s.beginTransaction(); 44 prod = (Product) s.get(Product.class, "Some Thing"); 45 assertEquals( prod.getParts().size(), 1 ); 46 prod.getParts().put("Top", new Part("top", "The brand new top part")); 47 t.commit(); 48 s.close(); 49 50 s = openSession(); 51 t = s.beginTransaction(); 52 prod = (Product) s.get(Product.class, "Some Thing"); 53 assertEquals( prod.getParts().size(), 1 ); 54 assertEquals( ( (Part) prod.getParts().get("Top") ).getDescription(), "The brand new top part"); 55 s.delete(prod); 56 t.commit(); 57 s.close(); 58 } 59 60 public void testQueryMapCompositeElement() { 61 Session s = openSession(); 62 Transaction t = s.beginTransaction(); 63 64 Part top = new Part("top", "The top part"); 65 Part bottom = new Part("bottom", "The bottom part"); 66 Product prod = new Product("Some Thing"); 67 prod.getParts().put("Top", top); 68 prod.getParts().put("Bottom", bottom); 69 s.persist(prod); 70 71 Item item = new Item("123456", prod); 72 s.persist(item); 73 74 List list = s.createQuery("select new Part( part.name, part.description ) from Product prod join prod.parts part order by part.name desc").list(); 75 assertEquals( list.size(), 2 ); 76 assertTrue( list.get(0) instanceof Part ); 77 assertTrue( list.get(1) instanceof Part ); 78 Part part = (Part) list.get(0); 79 assertEquals( part.getName(), "top" ); 80 assertEquals( part.getDescription(), "The top part" ); 81 82 list = s.createQuery("select new Part( part.name, part.description ) from Product prod join prod.parts part where index(part) = 'Top'").list(); 83 assertEquals( list.size(), 1 ); 84 assertTrue( list.get(0) instanceof Part ); 85 part = (Part) list.get(0); 86 assertEquals( part.getName(), "top" ); 87 assertEquals( part.getDescription(), "The top part" ); 88 89 list = s.createQuery("from Product p where 'Top' in indices(p.parts)").list(); 90 assertEquals( list.size(), 1 ); 91 assertSame( list.get(0), prod ); 92 93 list = s.createQuery("select i from Item i join i.product p where 'Top' in indices(p.parts)").list(); 94 assertEquals( list.size(), 1 ); 95 assertSame( list.get(0), item ); 96 97 list = s.createQuery("from Item i where 'Top' in indices(i.product.parts)").list(); 98 assertEquals( list.size(), 1 ); 99 assertSame( list.get(0), item ); 100 101 s.delete(item); 102 s.delete(prod); 103 t.commit(); 104 s.close(); 105 } 106 107 protected String [] getMappings() { 108 return new String [] { "mapcompelem/ProductPart.hbm.xml" }; 109 } 110 111 public static Test suite() { 112 return new TestSuite(MapCompositeElementTest.class); 113 } 114 115 } 116 117 | Popular Tags |