1 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.dialect.Oracle9Dialect; 11 import org.hibernate.test.TestCase; 12 13 import junit.framework.Test; 14 import junit.framework.TestSuite; 15 16 19 public class AuctionTest2 extends TestCase { 20 21 public AuctionTest2(String str) { 22 super(str); 23 } 24 25 public void testLazy() { 26 27 if ( getDialect() instanceof Oracle9Dialect ) return; 29 Session s = openSession(); 30 Transaction t = s.beginTransaction(); 31 Auction a = new Auction(); 32 a.setDescription("an auction for something"); 33 a.setEnd( new Date () ); 34 Bid b = new Bid(); 35 b.setAmount( new BigDecimal (123.34) ); 36 b.setSuccessful(true); 37 b.setDatetime( new Date () ); 38 b.setItem(a); 39 a.getBids().add(b); 40 a.setSuccessfulBid(b); 41 s.persist(b); 42 t.commit(); 43 s.close(); 44 45 Long aid = a.getId(); 46 Long bid = b.getId(); 47 48 s = openSession(); 49 t = s.beginTransaction(); 50 b = (Bid) s.load( Bid.class, bid ); 51 assertFalse( Hibernate.isInitialized(b) ); 52 a = (Auction) s.get( Auction.class, aid ); 53 assertFalse( Hibernate.isInitialized( a.getBids() ) ); 54 assertFalse( Hibernate.isInitialized( a.getSuccessfulBid() ) ); 55 assertSame( a.getBids().iterator().next(), b ); 56 assertSame( b, a.getSuccessfulBid() ); 57 assertTrue( Hibernate.isInitialized(b) ); 58 assertTrue( b.isSuccessful() ); 59 t.commit(); 60 s.close(); 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.createQuery("from Auction a left join fetch a.bids").uniqueResult(); 67 assertTrue( Hibernate.isInitialized(b) ); 68 assertTrue( Hibernate.isInitialized( a.getBids() ) ); 69 assertSame( b, a.getSuccessfulBid() ); 70 assertSame( a.getBids().iterator().next(), b ); 71 assertTrue( b.isSuccessful() ); 72 t.commit(); 73 s.close(); 74 75 s = openSession(); 76 t = s.beginTransaction(); 77 b = (Bid) s.load( Bid.class, bid ); 78 a = (Auction) s.load( Auction.class, aid ); 79 assertFalse( Hibernate.isInitialized(b) ); 80 assertFalse( Hibernate.isInitialized(a) ); 81 s.createQuery("from Auction a left join fetch a.successfulBid").list(); 82 assertTrue( Hibernate.isInitialized(b) ); 83 assertTrue( Hibernate.isInitialized(a) ); 84 assertSame( b, a.getSuccessfulBid() ); 85 assertFalse( Hibernate.isInitialized( a.getBids() ) ); 86 assertSame( a.getBids().iterator().next(), b ); 87 assertTrue( b.isSuccessful() ); 88 t.commit(); 89 s.close(); 90 91 s = openSession(); 92 t = s.beginTransaction(); 93 b = (Bid) s.load( Bid.class, bid ); 94 a = (Auction) s.load( Auction.class, aid ); 95 assertFalse( Hibernate.isInitialized(b) ); 96 assertFalse( Hibernate.isInitialized(a) ); 97 assertSame( s.get( Bid.class, bid ), b ); 98 assertTrue( Hibernate.isInitialized(b) ); 99 assertSame( s.get(Auction.class, aid ), a ); 100 assertTrue( Hibernate.isInitialized(a) ); 101 assertSame( b, a.getSuccessfulBid() ); 102 assertFalse( Hibernate.isInitialized( a.getBids() ) ); 103 assertSame( a.getBids().iterator().next(), b ); 104 assertTrue( b.isSuccessful() ); 105 t.commit(); 106 s.close(); 107 } 108 109 protected String [] getMappings() { 110 return new String [] { "bidi/Auction2.hbm.xml" }; 111 } 112 113 public static Test suite() { 114 return new TestSuite(AuctionTest2.class); 115 } 116 117 } 118 | Popular Tags |