HHH-10998 : NullPointerException when using join with subselect in hbm mapping

(cherry picked from commit f34508e3f3)
This commit is contained in:
Gail Badner 2016-09-28 17:23:31 -07:00
parent d58f6d4192
commit ad769e54b5
3 changed files with 134 additions and 2 deletions

View File

@ -1817,8 +1817,11 @@ public class ModelBinder {
keyBinding.setForeignKeyName( secondaryTableSource.getExplicitForeignKeyName() );
secondaryTableJoin.createPrimaryKey();
secondaryTableJoin.createForeignKey();
// skip creating primary and foreign keys for a subselect.
if ( secondaryTable.getSubselect() == null ) {
secondaryTableJoin.createPrimaryKey();
secondaryTableJoin.createForeignKey();
}
}
private Property createEmbeddedAttribute(

View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.hibernate.test.subselect.join">
<class name="SubselectInJoinedTableTest$Order" table="`ORDER`" lazy="false">
<id name="orderId" column="id" type="java.lang.Long"/>
<list name="orderEntries">
<key column="orderId"/>
<index column="ipos"/>
<one-to-many class="SubselectInJoinedTableTest$OrderEntry"/>
</list>
<join table="ORDER_ENTRY" optional="true" inverse="true">
<subselect>select orderEntryId, orderId from ORDER_ENTRY where ipos = 0</subselect>
<key column="orderId"/>
<many-to-one name="firstOrderEntry" class="SubselectInJoinedTableTest$OrderEntry" column="orderEntryId" update="false" insert="false"/>
</join>
</class>
<class name="SubselectInJoinedTableTest$OrderEntry" table="ORDER_ENTRY">
<id name="orderEntryId"/>
</class>
</hibernate-mapping>

View File

@ -0,0 +1,104 @@
/*
* 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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.subselect.join;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import static org.junit.Assert.assertEquals;
public class SubselectInJoinedTableTest extends BaseCoreFunctionalTestCase {
@Override
protected String[] getMappings() {
return new String[] {
"subselect/join/Order.hbm.xml"
};
}
@Test
@TestForIssue( jiraKey = "HHH-10998")
public void testSubselectInJoinedTable() {
OrderEntry orderEntry1 = new OrderEntry();
orderEntry1.setOrderEntryId( 1L );
OrderEntry orderEntry2 = new OrderEntry();
orderEntry2.setOrderEntryId( 2L );
Order order = new Order();
order.setOrderId( 3L );
order.getOrderEntries().add( orderEntry1 );
order.getOrderEntries().add( orderEntry2 );
order.setFirstOrderEntry( orderEntry1 );
Session s = openSession();
Transaction tx = s.beginTransaction();
s.persist( orderEntry1 );
s.persist( orderEntry2 );
s.persist( order );
tx.commit();
s.close();
s = openSession();
s.getTransaction().begin();
order = (Order) s.get( Order.class, order.getOrderId() );
assertEquals( orderEntry1.getOrderEntryId(), order.getFirstOrderEntry().getOrderEntryId() );
assertEquals( 2, order.getOrderEntries().size() );
assertEquals( orderEntry1.getOrderEntryId(), order.getOrderEntries().get( 0 ).getOrderEntryId() );
assertEquals( orderEntry2.getOrderEntryId(), order.getOrderEntries().get( 1 ).getOrderEntryId() );
s.getTransaction().commit();
s.close();
}
public static class Order {
private Long orderId;
private OrderEntry firstOrderEntry;
private List<OrderEntry> orderEntries = new ArrayList<OrderEntry>();
public OrderEntry getFirstOrderEntry() {
return firstOrderEntry;
}
public void setFirstOrderEntry(OrderEntry firstOrderEntry) {
this.firstOrderEntry = firstOrderEntry;
}
public Long getOrderId() {
return orderId;
}
public void setOrderId(Long orderId) {
this.orderId = orderId;
}
public List<OrderEntry> getOrderEntries() {
return orderEntries;
}
public void setOrderEntries(List<OrderEntry> orderEntries) {
this.orderEntries = orderEntries;
}
}
public static class OrderEntry {
private Long orderEntryId;
public Long getOrderEntryId() {
return orderEntryId;
}
public void setOrderEntryId(Long orderEntryId) {
this.orderEntryId = orderEntryId;
}
}
}