KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > idbag > IdBagTest


1 //$Id: IdBagTest.java,v 1.2 2005/03/22 15:32:03 oneovthafew Exp $
2
package org.hibernate.test.idbag;
3
4 import java.sql.SQLException JavaDoc;
5 import java.util.List JavaDoc;
6
7 import junit.framework.Test;
8 import junit.framework.TestSuite;
9
10 import org.hibernate.Hibernate;
11 import org.hibernate.HibernateException;
12 import org.hibernate.Session;
13 import org.hibernate.Transaction;
14 import org.hibernate.test.TestCase;
15
16 /**
17  * @author Gavin King
18  */

19 public class IdBagTest extends TestCase {
20     
21     public IdBagTest(String JavaDoc str) {
22         super(str);
23     }
24
25     public void testUpdateIdBag() throws HibernateException, SQLException JavaDoc {
26         Session s = openSession();
27         Transaction t = s.beginTransaction();
28         User gavin = new User("gavin");
29         Group admins = new Group("admins");
30         Group plebs = new Group("plebs");
31         Group moderators = new Group("moderators");
32         Group banned = new Group("banned");
33         gavin.getGroups().add(plebs);
34         //gavin.getGroups().add(moderators);
35
s.persist(gavin);
36         s.persist(plebs);
37         s.persist(admins);
38         s.persist(moderators);
39         s.persist(banned);
40         t.commit();
41         s.close();
42         
43         s = openSession();
44         t = s.beginTransaction();
45         gavin = (User) s.createCriteria(User.class).uniqueResult();
46         admins = (Group) s.load(Group.class, "admins");
47         plebs = (Group) s.load(Group.class, "plebs");
48         banned = (Group) s.load(Group.class, "banned");
49         gavin.getGroups().add(admins);
50         gavin.getGroups().remove(plebs);
51         //gavin.getGroups().add(banned);
52

53         s.delete(plebs);
54         s.delete(banned);
55         s.delete(moderators);
56         s.delete(admins);
57         s.delete(gavin);
58         
59         t.commit();
60         s.close();
61     }
62     
63     public void testJoin() throws HibernateException, SQLException JavaDoc {
64         Session s = openSession();
65         Transaction t = s.beginTransaction();
66         User gavin = new User("gavin");
67         Group admins = new Group("admins");
68         Group plebs = new Group("plebs");
69         gavin.getGroups().add(plebs);
70         gavin.getGroups().add(admins);
71         s.persist(gavin);
72         s.persist(plebs);
73         s.persist(admins);
74         
75         List JavaDoc l = s.createQuery("from User u join u.groups g").list();
76         assertEquals( l.size(), 2 );
77         s.clear();
78         
79         gavin = (User) s.createQuery("from User u join fetch u.groups").uniqueResult();
80         assertTrue( Hibernate.isInitialized( gavin.getGroups() ) );
81         assertEquals( gavin.getGroups().size(), 2 );
82         assertEquals( ( (Group) gavin.getGroups().get(0) ).getName(), "admins" );
83         
84         s.delete( gavin.getGroups().get(0) );
85         s.delete( gavin.getGroups().get(1) );
86         s.delete(gavin);
87         
88         t.commit();
89         s.close();
90     }
91     
92     protected String JavaDoc[] getMappings() {
93         return new String JavaDoc[] { "idbag/UserGroup.hbm.xml" };
94     }
95
96     public static Test suite() {
97         return new TestSuite(IdBagTest.class);
98     }
99
100 }
101
102
Popular Tags