~ubuntu-branches/ubuntu/wily/libhibernate3-java/wily-proposed

« back to all changes in this revision

Viewing changes to test/org/hibernate/test/bidi/AuctionTest2.java

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2007-10-14 14:43:34 UTC
  • Revision ID: james.westby@ubuntu.com-20071014144334-eamc8i0q10gs1aro
Tags: upstream-3.2.5
ImportĀ upstreamĀ versionĀ 3.2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//$Id: AuctionTest2.java 10980 2006-12-13 00:13:43Z steve.ebersole@jboss.com $
 
2
package org.hibernate.test.bidi;
 
3
 
 
4
import java.math.BigDecimal;
 
5
import java.util.Date;
 
6
 
 
7
import org.hibernate.Hibernate;
 
8
import org.hibernate.Session;
 
9
import org.hibernate.Transaction;
 
10
import org.hibernate.junit.functional.FunctionalTestCase;
 
11
import org.hibernate.junit.functional.FunctionalTestClassTestSuite;
 
12
import org.hibernate.dialect.Oracle9Dialect;
 
13
 
 
14
import junit.framework.Test;
 
15
 
 
16
/**
 
17
 * @author Gavin King
 
18
 */
 
19
public class AuctionTest2 extends FunctionalTestCase {
 
20
 
 
21
        public AuctionTest2(String str) {
 
22
                super( str );
 
23
        }
 
24
 
 
25
        public String[] getMappings() {
 
26
                return new String[] { "bidi/Auction2.hbm.xml" };
 
27
        }
 
28
 
 
29
        public boolean createSchema() {
 
30
                return getDialect().supportsExistsInSelect();
 
31
        }
 
32
 
 
33
        public static Test suite() {
 
34
                return new FunctionalTestClassTestSuite( AuctionTest2.class );
 
35
        }
 
36
 
 
37
        public void testLazy() {
 
38
                if ( ! getDialect().supportsExistsInSelect() ) {
 
39
                        reportSkip( "dialect does not support exist fragments in the select clause", "bidi support" );
 
40
                        return;
 
41
                }
 
42
 
 
43
                Session s = openSession();
 
44
                Transaction t = s.beginTransaction();
 
45
                Auction a = new Auction();
 
46
                a.setDescription( "an auction for something" );
 
47
                a.setEnd( new Date() );
 
48
                Bid b = new Bid();
 
49
                b.setAmount( new BigDecimal( 123.34 ).setScale( 19, BigDecimal.ROUND_DOWN ) );
 
50
                b.setSuccessful( true );
 
51
                b.setDatetime( new Date() );
 
52
                b.setItem( a );
 
53
                a.getBids().add( b );
 
54
                a.setSuccessfulBid( b );
 
55
                s.persist( b );
 
56
                t.commit();
 
57
                s.close();
 
58
 
 
59
                Long aid = a.getId();
 
60
                Long bid = b.getId();
 
61
 
 
62
                s = openSession();
 
63
                t = s.beginTransaction();
 
64
                b = ( Bid ) s.load( Bid.class, bid );
 
65
                assertFalse( Hibernate.isInitialized( b ) );
 
66
                a = ( Auction ) s.get( Auction.class, aid );
 
67
                assertFalse( Hibernate.isInitialized( a.getBids() ) );
 
68
                assertFalse( Hibernate.isInitialized( a.getSuccessfulBid() ) );
 
69
                assertSame( a.getBids().iterator().next(), b );
 
70
                assertSame( b, a.getSuccessfulBid() );
 
71
                assertTrue( Hibernate.isInitialized( b ) );
 
72
                assertTrue( b.isSuccessful() );
 
73
                t.commit();
 
74
                s.close();
 
75
 
 
76
                s = openSession();
 
77
                t = s.beginTransaction();
 
78
                b = ( Bid ) s.load( Bid.class, bid );
 
79
                assertFalse( Hibernate.isInitialized( b ) );
 
80
                a = ( Auction ) s.createQuery( "from Auction a left join fetch a.bids" ).uniqueResult();
 
81
                assertTrue( Hibernate.isInitialized( b ) );
 
82
                assertTrue( Hibernate.isInitialized( a.getBids() ) );
 
83
                assertSame( b, a.getSuccessfulBid() );
 
84
                assertSame( a.getBids().iterator().next(), b );
 
85
                assertTrue( b.isSuccessful() );
 
86
                t.commit();
 
87
                s.close();
 
88
 
 
89
                s = openSession();
 
90
                t = s.beginTransaction();
 
91
                b = ( Bid ) s.load( Bid.class, bid );
 
92
                a = ( Auction ) s.load( Auction.class, aid );
 
93
                assertFalse( Hibernate.isInitialized( b ) );
 
94
                assertFalse( Hibernate.isInitialized( a ) );
 
95
                s.createQuery( "from Auction a left join fetch a.successfulBid" ).list();
 
96
                assertTrue( Hibernate.isInitialized( b ) );
 
97
                assertTrue( Hibernate.isInitialized( a ) );
 
98
                assertSame( b, a.getSuccessfulBid() );
 
99
                assertFalse( Hibernate.isInitialized( a.getBids() ) );
 
100
                assertSame( a.getBids().iterator().next(), b );
 
101
                assertTrue( b.isSuccessful() );
 
102
                t.commit();
 
103
                s.close();
 
104
 
 
105
                s = openSession();
 
106
                t = s.beginTransaction();
 
107
                b = ( Bid ) s.load( Bid.class, bid );
 
108
                a = ( Auction ) s.load( Auction.class, aid );
 
109
                assertFalse( Hibernate.isInitialized( b ) );
 
110
                assertFalse( Hibernate.isInitialized( a ) );
 
111
                assertSame( s.get( Bid.class, bid ), b );
 
112
                assertTrue( Hibernate.isInitialized( b ) );
 
113
                assertSame( s.get( Auction.class, aid ), a );
 
114
                assertTrue( Hibernate.isInitialized( a ) );
 
115
                assertSame( b, a.getSuccessfulBid() );
 
116
                assertFalse( Hibernate.isInitialized( a.getBids() ) );
 
117
                assertSame( a.getBids().iterator().next(), b );
 
118
                assertTrue( b.isSuccessful() );
 
119
                t.commit();
 
120
                s.close();
 
121
        }
 
122
 
 
123
}