1
//$Id: ManyToOneTest.java 14736 2008-06-04 14:23:42Z hardy.ferentschik $
2
package org.hibernate.test.annotations.manytoone;
4
import java.util.ArrayList;
5
import java.util.Collection;
8
import org.hibernate.Hibernate;
9
import org.hibernate.Query;
10
import org.hibernate.Session;
11
import org.hibernate.Transaction;
12
import org.hibernate.test.annotations.Company;
13
import org.hibernate.test.annotations.Customer;
14
import org.hibernate.test.annotations.Discount;
15
import org.hibernate.test.annotations.Flight;
16
import org.hibernate.test.annotations.Passport;
17
import org.hibernate.test.annotations.TestCase;
18
import org.hibernate.test.annotations.Ticket;
21
* @author Emmanuel Bernard
23
public class ManyToOneTest extends TestCase {
25
public ManyToOneTest(String x) {
29
public void testEager() throws Exception {
33
tx = s.beginTransaction();
34
Color c = new Color();
35
c.setName( "Yellow" );
38
car.setBodyColor( c );
44
tx = s.beginTransaction();
45
car = (Car) s.get( Car.class, car.getId() );
49
assertNotNull( car.getBodyColor() );
50
assertEquals( "Yellow", car.getBodyColor().getName() );
54
public void testDefaultMetadata() throws Exception {
58
tx = s.beginTransaction();
59
Color c = new Color();
63
car.setBodyColor( c );
69
tx = s.beginTransaction();
70
car = (Car) s.get( Car.class, car.getId() );
72
assertNotNull( car.getBodyColor() );
73
assertEquals( c.getId(), car.getBodyColor().getId() );
78
public void testCreate() throws Exception {
82
tx = s.beginTransaction();
83
Flight firstOne = new Flight();
84
firstOne.setId( new Long( 1 ) );
85
firstOne.setName( "AF0101" );
86
firstOne.setDuration( new Long( 1000 ) );
87
Company frenchOne = new Company();
88
frenchOne.setName( "Air France" );
89
firstOne.setCompany( frenchOne );
90
s.persist( firstOne );
93
assertNotNull( "identity id should work", frenchOne.getId() );
96
tx = s.beginTransaction();
97
firstOne = (Flight) s.get( Flight.class, new Long( 1 ) );
98
assertNotNull( firstOne.getCompany() );
99
assertEquals( frenchOne.getName(), firstOne.getCompany().getName() );
104
public void testCascade() throws Exception {
108
tx = s.beginTransaction();
109
Discount discount = new Discount();
110
discount.setDiscount( 20.12 );
111
Customer customer = new Customer();
112
Collection discounts = new ArrayList();
113
discounts.add( discount );
114
customer.setName( "Quentin Tarantino" );
115
discount.setOwner( customer );
116
customer.setDiscountTickets( discounts );
117
s.persist( discount );
122
tx = s.beginTransaction();
123
discount = (Discount) s.get( Discount.class, discount.getId() );
124
assertNotNull( discount );
125
assertEquals( 20.12, discount.getDiscount() );
126
assertNotNull( discount.getOwner() );
127
customer = new Customer();
128
customer.setName( "Clooney" );
129
discount.setOwner( customer );
130
discounts = new ArrayList();
131
discounts.add( discount );
132
customer.setDiscountTickets( discounts );
137
tx = s.beginTransaction();
138
discount = (Discount) s.get( Discount.class, discount.getId() );
139
assertNotNull( discount );
140
assertNotNull( discount.getOwner() );
141
assertEquals( "Clooney", discount.getOwner().getName() );
146
tx = s.beginTransaction();
147
customer = (Customer) s.get( Customer.class, customer.getId() );
148
s.delete( customer );
153
public void testFetch() throws Exception {
157
tx = s.beginTransaction();
158
Discount discount = new Discount();
159
discount.setDiscount( 20 );
160
Customer customer = new Customer();
161
Collection discounts = new ArrayList();
162
discounts.add( discount );
163
customer.setName( "Quentin Tarantino" );
164
discount.setOwner( customer );
165
customer.setDiscountTickets( discounts );
166
s.persist( discount );
171
tx = s.beginTransaction();
172
discount = (Discount) s.get( Discount.class, discount.getId() );
173
assertNotNull( discount );
174
assertFalse( Hibernate.isInitialized( discount.getOwner() ) );
178
tx = s.beginTransaction();
179
discount = (Discount) s.load( Discount.class, discount.getId() );
180
assertNotNull( discount );
181
assertFalse( Hibernate.isInitialized( discount.getOwner() ) );
185
tx = s.beginTransaction();
186
s.delete( s.get( Discount.class, discount.getId() ) );
191
public void testCompositeFK() throws Exception {
195
tx = s.beginTransaction();
196
ParentPk ppk = new ParentPk();
197
ppk.firstName = "John";
198
ppk.lastName = "Doe";
199
Parent p = new Parent();
203
Child c = new Child();
210
tx = s.beginTransaction();
211
//FIXME: fix this when the small parser bug will be fixed
212
Query q = s.createQuery( "from " + Child.class.getName() ); //+ " c where c.parent.id.lastName = :lastName");
213
//q.setString("lastName", p.id.lastName);
214
List result = q.list();
215
assertEquals( 1, result.size() );
216
Child c2 = (Child) result.get( 0 );
217
assertEquals( c2.id, c.id );
222
public void testImplicitCompositeFk() throws Exception {
226
tx = s.beginTransaction();
227
Node n1 = new Node();
228
n1.setDescription( "Parent" );
229
NodePk n1pk = new NodePk();
231
n1pk.setName( "Root" );
233
Node n2 = new Node();
234
NodePk n2pk = new NodePk();
236
n2pk.setName( "Level 1: A" );
243
tx = s.beginTransaction();
244
n2 = (Node) s.get( Node.class, n2pk );
246
assertNotNull( n2.getParent() );
247
assertEquals( 1, n2.getParent().getId().getLevel() );
252
public void testManyToOneAndIdClass() throws Exception {
256
public void testManyToOneNonPk() throws Exception {
257
Session s = openSession();
258
Transaction tx = s.beginTransaction();
259
Order order = new Order();
260
order.setOrderNbr( "123" );
262
OrderLine ol = new OrderLine();
263
ol.setItem( "Mouse" );
264
ol.setOrder( order );
268
ol = (OrderLine) s.get( OrderLine.class, ol.getId() );
269
assertNotNull( ol.getOrder() );
270
assertEquals( "123", ol.getOrder().getOrderNbr() );
271
assertTrue( ol.getOrder().getOrderLines().contains( ol ) );
276
public void testTwoManyToOneNonPk() throws Exception {
277
//2 many to one non pk pointing to the same referencedColumnName should not fail
278
Session s = openSession();
279
Transaction tx = s.beginTransaction();
280
org.hibernate.test.annotations.manytoone.Customer customer = new org.hibernate.test.annotations.manytoone.Customer();
281
customer.userId="123";
282
org.hibernate.test.annotations.manytoone.Customer customer2 = new org.hibernate.test.annotations.manytoone.Customer();
283
customer2.userId="124";
284
s.persist( customer2 );
285
s.persist( customer );
286
Deal deal = new Deal();
287
deal.from = customer;
292
deal = (Deal) s.get( Deal.class, deal.id );
293
assertNotNull( deal.from );
294
assertNotNull( deal.to );
299
public void testFormulaOnOtherSide() throws Exception {
300
Session s = openSession();
301
Transaction tx = s.beginTransaction();
302
Frame frame = new Frame();
303
frame.setName( "Prada" );
315
frame = (Frame) s.get( Frame.class, frame.getId() );
316
assertEquals( 2, frame.getLenses().size() );
317
assertTrue( frame.getLenses().iterator().next().getLength() <= 1/1.2f );
318
assertTrue( frame.getLenses().iterator().next().getLength() >= 1/2.5f );
324
* @see org.hibernate.test.annotations.TestCase#getMappings()
326
protected java.lang.Class[] getMappings() {
327
return new java.lang.Class[]{
329
org.hibernate.test.annotations.manytoone.Customer.class,
b'\\ No newline at end of file'