From a1444f2a10d890fbfaab8fcfcbe64134912f5081 Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Fri, 25 Sep 2015 11:18:10 -0500 Subject: [PATCH] HHH-10099 - @OrderColumn never generates the column in the schema --- .../annotations/list/ListMappingTest.java | 187 ++++++++++++++++++ 1 file changed, 187 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/list/ListMappingTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/list/ListMappingTest.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/list/ListMappingTest.java new file mode 100644 index 0000000000..c706909908 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/list/ListMappingTest.java @@ -0,0 +1,187 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.test.annotations.list; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.OneToMany; +import javax.persistence.OrderColumn; +import javax.persistence.Table; + +import org.hibernate.boot.Metadata; +import org.hibernate.boot.MetadataSources; +import org.hibernate.boot.registry.StandardServiceRegistry; +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; +import org.hibernate.mapping.Collection; +import org.hibernate.mapping.Column; +import org.hibernate.tool.schema.internal.TargetStdoutImpl; +import org.hibernate.tool.schema.spi.SchemaManagementTool; +import org.hibernate.tool.schema.spi.Target; + +import org.hibernate.testing.junit4.BaseUnitTestCase; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.instanceOf; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertTrue; + +/** + * Originally developed to help diagnose HHH-10099 which reports a problem with @OrderColumn + * not being bound + * + * @author Steve Ebersole + */ +public class ListMappingTest extends BaseUnitTestCase { + private StandardServiceRegistry ssr; + + @Before + public void before() { + ssr = new StandardServiceRegistryBuilder() + .build(); + } + + @After + public void after() { + if ( ssr != null ) { + StandardServiceRegistryBuilder.destroy( ssr ); + } + } + + @Test + public void testOrderColumnInNormalBiDirectonalModel() { + Metadata metadata = new MetadataSources( ssr ) + .addAnnotatedClass( Order.class ) + .addAnnotatedClass( LineItem.class ) + .buildMetadata(); + + Collection lineItemsBinding = metadata.getCollectionBindings().iterator().next(); + + // make sure it was interpreted as a List (aka, as having an OrderColumn at all) + assertThat( lineItemsBinding, instanceOf( org.hibernate.mapping.List.class ) ); + org.hibernate.mapping.List asList = (org.hibernate.mapping.List) lineItemsBinding; + + // assert the OrderColumn details + final Column positionColumn = (Column) asList.getIndex().getColumnIterator().next(); + assertThat( positionColumn.getName(), equalTo( "position" ) ); + + // make sure the OrderColumn is part of the collection table + assertTrue( asList.getCollectionTable().containsColumn( positionColumn ) ); + + class TargetImpl extends TargetStdoutImpl { + boolean found = false; + @Override + public void accept(String action) { + super.accept( action ); + if ( action.startsWith( "create table t_line_item" ) ) { + if ( action.contains( "position" ) ) { + found = true; + } + } + } + } + + TargetImpl target = new TargetImpl(); + + ssr.getService( SchemaManagementTool.class ).getSchemaCreator( Collections.emptyMap() ).doCreation( + metadata, + true, + Collections.singletonList( target ) + ); + + assertTrue( target.found ); + } + + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + // Bi-directional model + + @Entity( name = "Order" ) + @Table( name = "t_order" ) + public static class Order { + private Integer id; + private List lineItems = new ArrayList(); + + @Id + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + @OneToMany( mappedBy = "order" ) + @OrderColumn( name = "position" ) + public List getLineItems() { + return lineItems; + } + + public void setLineItems(List lineItems) { + this.lineItems = lineItems; + } + } + + @Entity( name = "LineItem" ) + @Table( name = "t_line_item" ) + public static class LineItem { + private Integer id; + private Order order; + private String product; + private int quantity; + private String discountCode; + + @Id + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + @ManyToOne( optional = false ) + @JoinColumn( name = "order_id" ) + public Order getOrder() { + return order; + } + + public void setOrder(Order order) { + this.order = order; + } + + public String getProduct() { + return product; + } + + public void setProduct(String product) { + this.product = product; + } + + public int getQuantity() { + return quantity; + } + + public void setQuantity(int quantity) { + this.quantity = quantity; + } + + public String getDiscountCode() { + return discountCode; + } + + public void setDiscountCode(String discountCode) { + this.discountCode = discountCode; + } + } +}