1 package org.hibernate.ejb.test; 3 4 import java.util.ArrayList ; 5 import java.util.List ; 6 import javax.persistence.EntityManager; 7 import javax.persistence.Query; 8 9 10 13 public class QueryTest extends TestCase { 14 15 public void testParameterList() throws Exception { 16 final Item item = new Item( "Mouse", "Micro$oft mouse" ); 17 final Item item2 = new Item( "Computer", "D€ll computer" ); 18 19 EntityManager em = factory.createEntityManager(); 20 em.getTransaction().begin(); 21 em.persist( item ); 22 em.persist( item2 ); 23 assertTrue( em.contains( item ) ); 24 em.getTransaction().commit(); 25 26 em.getTransaction().begin(); 27 Query q = em.createQuery( "select item from Item item where item.name in (:names)" ); 28 List params = new ArrayList (); 29 params.add( item.getName() ); 30 params.add( item2.getName() ); 31 q.setParameter( "names", params ); 32 List result = q.getResultList(); 33 assertNotNull( result ); 34 assertEquals( 2, result.size() ); 35 em.remove( result.get( 0 ) ); 36 em.remove( result.get( 1 ) ); 37 em.getTransaction().commit(); 38 39 em.close(); 40 } 41 42 75 public void testEscapeCharacter() throws Exception { 76 final Item item = new Item( "Mouse", "Micro_oft mouse" ); 77 final Item item2 = new Item( "Computer", "D€ll computer" ); 78 79 EntityManager em = factory.createEntityManager(); 80 em.getTransaction().begin(); 81 em.persist( item ); 82 em.persist( item2 ); 83 assertTrue( em.contains( item ) ); 84 em.getTransaction().commit(); 85 86 em.getTransaction().begin(); 87 Query q = em.createQuery( "select item from Item item where item.descr like 'Microk_oft mouse' escape 'k' " ); 88 List result = q.getResultList(); 89 assertNotNull( result ); 90 assertEquals( 1, result.size() ); 91 int deleted = em.createQuery( "delete from Item" ).executeUpdate(); 92 assertEquals( 2, deleted ); 93 em.getTransaction().commit(); 94 95 em.close(); 96 } 97 98 public void testNativeQueryByEntity() { 99 100 Item item = new Item( "Mouse", "Micro$oft mouse" ); 101 102 EntityManager em = factory.createEntityManager(); 103 em.getTransaction().begin(); 104 em.persist( item ); 105 assertTrue( em.contains( item ) ); 106 em.getTransaction().commit(); 107 108 em.getTransaction().begin(); 109 item = (Item) em.createNativeQuery( "select * from Item", Item.class ).getSingleResult(); 110 assertNotNull( item ); 111 assertEquals( "Micro$oft mouse", item.getDescr() ); 112 em.remove( item ); 113 em.getTransaction().commit(); 114 115 em.close(); 116 117 } 118 119 public void testNativeQueryByResultSet() { 120 121 Item item = new Item( "Mouse", "Micro$oft mouse" ); 122 123 EntityManager em = factory.createEntityManager(); 124 em.getTransaction().begin(); 125 em.persist( item ); 126 assertTrue( em.contains( item ) ); 127 em.getTransaction().commit(); 128 129 em.getTransaction().begin(); 130 item = (Item) em.createNativeQuery( "select name as itemname, descr as itemdescription from Item", "getItem" ) 131 .getSingleResult(); 132 assertNotNull( item ); 133 assertEquals( "Micro$oft mouse", item.getDescr() ); 134 em.remove( item ); 135 em.getTransaction().commit(); 136 137 em.close(); 138 139 } 140 141 public Class [] getAnnotatedClasses() { 142 return new Class []{ 143 Item.class, 144 Distributor.class 145 }; 146 } 147 } 148 | Popular Tags |