KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > ejb > test > QueryTest


1 //$Id: QueryTest.java,v 1.5 2005/07/09 20:04:44 epbernard Exp $
2
package org.hibernate.ejb.test;
3
4 import java.util.ArrayList JavaDoc;
5 import java.util.List JavaDoc;
6 import javax.persistence.EntityManager;
7 import javax.persistence.Query;
8
9
10 /**
11  * @author Emmanuel Bernard
12  */

13 public class QueryTest extends TestCase {
14
15     public void testParameterList() throws Exception JavaDoc {
16         final Item item = new Item( "Mouse", "Micro$oft mouse" );
17         final Item item2 = new Item( "Computer", "Dll 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 JavaDoc params = new ArrayList JavaDoc();
29         params.add( item.getName() );
30         params.add( item2.getName() );
31         q.setParameter( "names", params );
32         List JavaDoc 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 // public void testDistinct() throws Exception {
43
// Item item = new Item("Mouse", "Micro_oft mouse");
44
// Distributor fnac = new Distributor();
45
// fnac.setName("Fnac");
46
// item.addDistributor(fnac);
47
// Distributor auchan = new Distributor();
48
// auchan.setName("Auchan");
49
// item.addDistributor(auchan);
50
//
51
// EntityManager em = factory.createEntityManager();
52
// em.getTransaction().begin();
53
// em.persist(fnac);
54
// em.persist(auchan);
55
// em.persist(item);
56
// em.getTransaction().commit();
57
//
58
// em.getTransaction().begin();
59
// Query q = em.createQuery("select distinct item from Item item join fetch item.distributors");
60
// List result = q.getResultList();
61
// assertNotNull(result);
62
// assertEquals( 1, result.size() );
63
// item = (Item) result.get(0);
64
// item.getDistributors().clear();
65
// em.flush();
66
// int deleted = em.createQuery("delete from Item").executeUpdate();
67
// assertEquals( 1, deleted );
68
// deleted = em.createQuery("delete from Distributor").executeUpdate();
69
// assertEquals( 2, deleted );
70
// em.getTransaction().commit();
71
//
72
// em.close();
73
// }
74

75     public void testEscapeCharacter() throws Exception JavaDoc {
76         final Item item = new Item( "Mouse", "Micro_oft mouse" );
77         final Item item2 = new Item( "Computer", "Dll 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 JavaDoc 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 JavaDoc[] getAnnotatedClasses() {
142         return new Class JavaDoc[]{
143             Item.class,
144             Distributor.class
145         };
146     }
147 }
148
Popular Tags