KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > readonly > ReadOnlyTest


1 //$Id: ReadOnlyTest.java,v 1.2 2005/06/22 20:30:20 oneovthafew Exp $
2
package org.hibernate.test.readonly;
3
4 import java.math.BigDecimal JavaDoc;
5 import java.util.List JavaDoc;
6
7 import junit.framework.Test;
8 import junit.framework.TestSuite;
9
10 import org.hibernate.CacheMode;
11 import org.hibernate.ScrollMode;
12 import org.hibernate.ScrollableResults;
13 import org.hibernate.Session;
14 import org.hibernate.Transaction;
15 import org.hibernate.cfg.Configuration;
16 import org.hibernate.cfg.Environment;
17 import org.hibernate.test.TestCase;
18
19 /**
20  * This is how to do batch processing in Hibernate.
21  * Remember to enable JDBC batch updates, or this
22  * test will take a Very Long Time!
23  *
24  * @author Gavin King
25  */

26 public class ReadOnlyTest extends TestCase {
27     
28     public ReadOnlyTest(String JavaDoc str) {
29         super(str);
30     }
31     
32     public void testReadOnlyMode() {
33         
34         Session s = openSession();
35         s.setCacheMode(CacheMode.IGNORE);
36         Transaction t = s.beginTransaction();
37         for ( int i=0; i<100; i++ ) {
38             DataPoint dp = new DataPoint();
39             dp.setX( new BigDecimal JavaDoc(i * 0.1d).setScale(19, BigDecimal.ROUND_DOWN) );
40             dp.setY( new BigDecimal JavaDoc( Math.cos( dp.getX().doubleValue() ) ).setScale(19, BigDecimal.ROUND_DOWN) );
41             s.save(dp);
42         }
43         t.commit();
44         s.close();
45         
46         s = openSession();
47         s.setCacheMode(CacheMode.IGNORE);
48         t = s.beginTransaction();
49         int i = 0;
50         ScrollableResults sr = s.createQuery("from DataPoint dp order by dp.x asc")
51                 .setReadOnly(true)
52                 .scroll(ScrollMode.FORWARD_ONLY);
53         while ( sr.next() ) {
54             DataPoint dp = (DataPoint) sr.get(0);
55             if (++i==50) {
56                 s.setReadOnly(dp, false);
57             }
58             dp.setDescription("done!");
59         }
60         t.commit();
61         s.clear();
62         t = s.beginTransaction();
63         List JavaDoc single = s.createQuery("from DataPoint where description='done!'").list();
64         assertEquals( single.size(), 1 );
65         s.createQuery("delete from DataPoint").executeUpdate();
66         t.commit();
67         s.close();
68         
69     }
70     
71     protected void configure(Configuration cfg) {
72         cfg.setProperty(Environment.STATEMENT_BATCH_SIZE, "20");
73     }
74
75     protected String JavaDoc[] getMappings() {
76         return new String JavaDoc[] { "readonly/DataPoint.hbm.xml" };
77     }
78
79     public static Test suite() {
80         return new TestSuite(ReadOnlyTest.class);
81     }
82
83     public String JavaDoc getCacheConcurrencyStrategy() {
84         return null;
85     }
86
87 }
88
89
Popular Tags