diff --git a/hibernate-core/src/test/java/org/hibernate/test/collection/list/LineItem.java b/hibernate-core/src/test/java/org/hibernate/test/collection/list/LineItem.java new file mode 100644 index 0000000000..e44fb5523b --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/collection/list/LineItem.java @@ -0,0 +1,87 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2013, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.test.collection.list; + +import java.math.BigDecimal; + +/** + * @author Steve Ebersole + */ +public class LineItem { + private Integer id; + private Order order; + private String productCode; + private BigDecimal costPer; + private int quantity; + + public LineItem() { + } + + public LineItem(Order order, String productCode, int quantity, BigDecimal costPer) { + this.order = order; + this.productCode = productCode; + this.quantity = quantity; + this.costPer = costPer; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public Order getOrder() { + return order; + } + + public void setOrder(Order order) { + this.order = order; + } + + public String getProductCode() { + return productCode; + } + + public void setProductCode(String productCode) { + this.productCode = productCode; + } + + public BigDecimal getCostPer() { + return costPer; + } + + public void setCostPer(BigDecimal costPer) { + this.costPer = costPer; + } + + public int getQuantity() { + return quantity; + } + + public void setQuantity(int quantity) { + this.quantity = quantity; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/collection/list/Mappings.hbm.xml b/hibernate-core/src/test/java/org/hibernate/test/collection/list/Mappings.hbm.xml index 2832963db2..af85b5ab54 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/collection/list/Mappings.hbm.xml +++ b/hibernate-core/src/test/java/org/hibernate/test/collection/list/Mappings.hbm.xml @@ -18,4 +18,26 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/hibernate-core/src/test/java/org/hibernate/test/collection/list/Order.java b/hibernate-core/src/test/java/org/hibernate/test/collection/list/Order.java new file mode 100644 index 0000000000..4fcd8eab93 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/collection/list/Order.java @@ -0,0 +1,74 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2013, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.test.collection.list; + +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.List; + +/** + * @author Steve Ebersole + */ +public class Order { + private Integer id; + private String code; + private List lineItems = new ArrayList(); + + public Order() { + } + + public Order(String code) { + this.code = code; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getCode() { + return code; + } + + public void setCode(String code) { + this.code = code; + } + + public List getLineItems() { + return lineItems; + } + + public void setLineItems(List lineItems) { + this.lineItems = lineItems; + } + + public LineItem addLineItem(String productCode, int quantity, BigDecimal costPer) { + LineItem lineItem = new LineItem( this, productCode, quantity, costPer ); + lineItems.add( lineItem ); + return lineItem; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/collection/list/PersistentListTest.java b/hibernate-core/src/test/java/org/hibernate/test/collection/list/PersistentListTest.java index d1f0e466a6..c4bbb4257b 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/collection/list/PersistentListTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/collection/list/PersistentListTest.java @@ -27,6 +27,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import java.math.BigDecimal; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -120,6 +121,63 @@ public class PersistentListTest extends BaseCoreFunctionalTestCase { session2.close(); } + @Test + @TestForIssue( jiraKey = "HHH-5732" ) + @FailureExpected( jiraKey = "HHH-5732" ) + public void testInverseListIndexColumnWritten2() { + // make sure no one changes the mapping + final CollectionPersister collectionPersister = sessionFactory().getCollectionPersister( Order.class.getName() + ".lineItems" ); + assertTrue( collectionPersister.isInverse() ); + + // do some creations... + Session session = openSession(); + session.beginTransaction(); + + Order order = new Order( "acme-1" ); + order.addLineItem( "abc", 2, new BigDecimal( 16.1 ) ); + order.addLineItem( "def", 200, new BigDecimal( .01 ) ); + order.addLineItem( "ghi", 13, new BigDecimal( 12.9 ) ); + session.save( order ); + session.getTransaction().commit(); + session.close(); + + // now, make sure the list-index column gotten written... + final Session session2 = openSession(); + session2.beginTransaction(); + session2.doWork( + new Work() { + @Override + public void execute(Connection connection) throws SQLException { + final QueryableCollection queryableCollection = (QueryableCollection) collectionPersister; + SimpleSelect select = new SimpleSelect( getDialect() ) + .setTableName( queryableCollection.getTableName() ) + .addColumn( "ORDER_ID" ) + .addColumn( "INDX" ) + .addColumn( "PRD_CODE" ); + PreparedStatement preparedStatement = ((SessionImplementor)session2).getTransactionCoordinator().getJdbcCoordinator().getStatementPreparer().prepareStatement( select.toStatementString() ); + ResultSet resultSet = ((SessionImplementor)session2).getTransactionCoordinator().getJdbcCoordinator().getResultSetReturn().extract( preparedStatement ); + Map valueMap = new HashMap(); + while ( resultSet.next() ) { + final int fk = resultSet.getInt( 1 ); + assertFalse( "Collection key (FK) column was null", resultSet.wasNull() ); + final int indx = resultSet.getInt( 2 ); + assertFalse( "List index column was null", resultSet.wasNull() ); + final String prodCode = resultSet.getString( 3 ); + assertFalse( "Prod code column was null", resultSet.wasNull() ); + valueMap.put( prodCode, indx ); + } + assertEquals( 3, valueMap.size() ); + assertEquals( Integer.valueOf( 0 ), valueMap.get( "abc" ) ); + assertEquals( Integer.valueOf( 1 ), valueMap.get( "def" ) ); + assertEquals( Integer.valueOf( 2 ), valueMap.get( "ghi" ) ); + } + } + ); + session2.delete( order ); + session2.getTransaction().commit(); + session2.close(); + } + @Test public void testWriteMethodDirtying() { ListOwner parent = new ListOwner( "root" );