HHH-13201 - do not set empty text on collection join parent without queryable collection

This commit is contained in:
Moritz Becker 2019-01-12 19:19:30 +01:00 committed by Christian Beikov
parent ea24abd757
commit 19af434b21
3 changed files with 75 additions and 2 deletions

View File

@ -368,7 +368,9 @@ public class FromElementFactory implements SqlTokenTypes {
// origin.addDestination( destination );
// This was the cause of HHH-242
// origin.setType( FROM_FRAGMENT ); // Set the parent node type so that the AST is properly formed.
if ( origin.getQueryableCollection() != null ) {
origin.setText( "" ); // The destination node will have all the FROM text.
}
origin.setCollectionJoin( true ); // The parent node is a collection join too (voodoo - see JoinProcessor)
fromClause.addCollectionJoinFromElementByPath( path, destination );
fromClause.getWalker().addQuerySpaces( queryableCollection.getCollectionSpaces() );

View File

@ -193,7 +193,7 @@ public class SelectClause extends SelectExpressionList {
FromElement fromElement = (FromElement) iterator.next();
if ( fromElement.isFetch() ) {
FromElement origin = null;
FromElement origin;
if ( fromElement.getRealOrigin() == null ) {
// work around that crazy issue where the tree contains
// "empty" FromElements (no text); afaict, this is caused

View File

@ -0,0 +1,71 @@
package org.hibernate.test.hql;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.hibernate.testing.TestForIssue;
import org.junit.Test;
import javax.persistence.*;
import java.util.HashMap;
import java.util.Map;
import static javax.persistence.CascadeType.ALL;
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
/**
* @author Moritz Becker (moritz.becker@ordami.com)
* @date 12/01/2019
* @company ordami GmbH
*/
@TestForIssue(jiraKey = "HHH-13201")
public class FetchNonRootRelativeElementCollectionAndAssociationTest extends BaseEntityManagerFunctionalTestCase {
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] { ProductNaturalId.class, Product.class, ProductDetail.class };
}
@Test
public void testJoinedSubclassUpdateWithCorrelation() {
doInJPA( this::entityManagerFactory, entityManager -> {
// DO NOT CHANGE this query: it used to trigger an error caused
// by the origin FromElement for the association fetch being resolved to the wrong FromElement due to the
// presence of an element collection join.
String u = "select prod from ProductNaturalId nat inner join nat.product prod " +
"left join fetch prod.productDetail " +
"left join fetch prod.normalizedPricesByUnit";
Query query = entityManager.createQuery( u, Product.class );
query.getResultList();
} );
}
@Entity(name = "ProductNaturalId")
public class ProductNaturalId {
@Id
private String naturalId;
@OneToOne(optional = false)
private Product product;
}
@Entity(name = "Product")
public class Product {
@Id
private Long id;
@OneToOne(mappedBy = "product", cascade = ALL, fetch = FetchType.LAZY)
private ProductDetail productDetail;
@OneToOne(mappedBy = "product", cascade = ALL, fetch = FetchType.LAZY)
private ProductNaturalId naturalId;
@ElementCollection(fetch = FetchType.LAZY)
private Map<String, String> normalizedPricesByUnit = new HashMap<>();
}
@Entity(name = "ProductDetail")
public class ProductDetail {
@Id
private Long id;
@OneToOne(optional = false)
@JoinColumn(name = "id")
@MapsId
private Product product;
}
}