Skip to content

Commit cf47c8d

Browse files
committed
HHH-10099 - @OrderColumn never generates the column in the schema
(cherry picked from commit a1444f2)
1 parent e9f99a8 commit cf47c8d

File tree

1 file changed

+187
-0
lines changed

1 file changed

+187
-0
lines changed
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.test.annotations.list;
8+
9+
import java.util.ArrayList;
10+
import java.util.Collections;
11+
import java.util.List;
12+
import javax.persistence.Entity;
13+
import javax.persistence.Id;
14+
import javax.persistence.JoinColumn;
15+
import javax.persistence.ManyToOne;
16+
import javax.persistence.OneToMany;
17+
import javax.persistence.OrderColumn;
18+
import javax.persistence.Table;
19+
20+
import org.hibernate.boot.Metadata;
21+
import org.hibernate.boot.MetadataSources;
22+
import org.hibernate.boot.registry.StandardServiceRegistry;
23+
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
24+
import org.hibernate.mapping.Collection;
25+
import org.hibernate.mapping.Column;
26+
import org.hibernate.tool.schema.internal.TargetStdoutImpl;
27+
import org.hibernate.tool.schema.spi.SchemaManagementTool;
28+
import org.hibernate.tool.schema.spi.Target;
29+
30+
import org.hibernate.testing.junit4.BaseUnitTestCase;
31+
import org.junit.After;
32+
import org.junit.Before;
33+
import org.junit.Test;
34+
35+
import static org.hamcrest.CoreMatchers.equalTo;
36+
import static org.hamcrest.CoreMatchers.instanceOf;
37+
import static org.hamcrest.MatcherAssert.assertThat;
38+
import static org.junit.Assert.assertTrue;
39+
40+
/**
41+
* Originally developed to help diagnose HHH-10099 which reports a problem with @OrderColumn
42+
* not being bound
43+
*
44+
* @author Steve Ebersole
45+
*/
46+
public class ListMappingTest extends BaseUnitTestCase {
47+
private StandardServiceRegistry ssr;
48+
49+
@Before
50+
public void before() {
51+
ssr = new StandardServiceRegistryBuilder()
52+
.build();
53+
}
54+
55+
@After
56+
public void after() {
57+
if ( ssr != null ) {
58+
StandardServiceRegistryBuilder.destroy( ssr );
59+
}
60+
}
61+
62+
@Test
63+
public void testOrderColumnInNormalBiDirectonalModel() {
64+
Metadata metadata = new MetadataSources( ssr )
65+
.addAnnotatedClass( Order.class )
66+
.addAnnotatedClass( LineItem.class )
67+
.buildMetadata();
68+
69+
Collection lineItemsBinding = metadata.getCollectionBindings().iterator().next();
70+
71+
// make sure it was interpreted as a List (aka, as having an OrderColumn at all)
72+
assertThat( lineItemsBinding, instanceOf( org.hibernate.mapping.List.class ) );
73+
org.hibernate.mapping.List asList = (org.hibernate.mapping.List) lineItemsBinding;
74+
75+
// assert the OrderColumn details
76+
final Column positionColumn = (Column) asList.getIndex().getColumnIterator().next();
77+
assertThat( positionColumn.getName(), equalTo( "position" ) );
78+
79+
// make sure the OrderColumn is part of the collection table
80+
assertTrue( asList.getCollectionTable().containsColumn( positionColumn ) );
81+
82+
class TargetImpl extends TargetStdoutImpl {
83+
boolean found = false;
84+
@Override
85+
public void accept(String action) {
86+
super.accept( action );
87+
if ( action.startsWith( "create table t_line_item" ) ) {
88+
if ( action.contains( "position" ) ) {
89+
found = true;
90+
}
91+
}
92+
}
93+
}
94+
95+
TargetImpl target = new TargetImpl();
96+
97+
ssr.getService( SchemaManagementTool.class ).getSchemaCreator( Collections.emptyMap() ).doCreation(
98+
metadata,
99+
true,
100+
Collections.<Target>singletonList( target )
101+
);
102+
103+
assertTrue( target.found );
104+
}
105+
106+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
107+
// Bi-directional model
108+
109+
@Entity( name = "Order" )
110+
@Table( name = "t_order" )
111+
public static class Order {
112+
private Integer id;
113+
private List<LineItem> lineItems = new ArrayList<LineItem>();
114+
115+
@Id
116+
public Integer getId() {
117+
return id;
118+
}
119+
120+
public void setId(Integer id) {
121+
this.id = id;
122+
}
123+
124+
@OneToMany( mappedBy = "order" )
125+
@OrderColumn( name = "position" )
126+
public List<LineItem> getLineItems() {
127+
return lineItems;
128+
}
129+
130+
public void setLineItems(List<LineItem> lineItems) {
131+
this.lineItems = lineItems;
132+
}
133+
}
134+
135+
@Entity( name = "LineItem" )
136+
@Table( name = "t_line_item" )
137+
public static class LineItem {
138+
private Integer id;
139+
private Order order;
140+
private String product;
141+
private int quantity;
142+
private String discountCode;
143+
144+
@Id
145+
public Integer getId() {
146+
return id;
147+
}
148+
149+
public void setId(Integer id) {
150+
this.id = id;
151+
}
152+
153+
@ManyToOne( optional = false )
154+
@JoinColumn( name = "order_id" )
155+
public Order getOrder() {
156+
return order;
157+
}
158+
159+
public void setOrder(Order order) {
160+
this.order = order;
161+
}
162+
163+
public String getProduct() {
164+
return product;
165+
}
166+
167+
public void setProduct(String product) {
168+
this.product = product;
169+
}
170+
171+
public int getQuantity() {
172+
return quantity;
173+
}
174+
175+
public void setQuantity(int quantity) {
176+
this.quantity = quantity;
177+
}
178+
179+
public String getDiscountCode() {
180+
return discountCode;
181+
}
182+
183+
public void setDiscountCode(String discountCode) {
184+
this.discountCode = discountCode;
185+
}
186+
}
187+
}

0 commit comments

Comments
 (0)