HHH-18321 Add test for issue

This commit is contained in:
Marco Belladelli 2024-07-01 10:13:05 +02:00
parent 26495fb600
commit 5ca841c184
1 changed files with 107 additions and 45 deletions

View File

@ -4,15 +4,24 @@
*/ */
package org.hibernate.orm.test.jpa.criteria; package org.hibernate.orm.test.jpa.criteria;
import java.math.BigDecimal;
import java.util.List;
import org.hibernate.testing.orm.junit.JiraKey; import org.hibernate.testing.orm.junit.JiraKey;
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
import org.hibernate.testing.orm.junit.Jira;
import org.hibernate.testing.orm.junit.Jpa; import org.hibernate.testing.orm.junit.Jpa;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import jakarta.persistence.Column; import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;
import jakarta.persistence.Embedded;
import jakarta.persistence.Entity; import jakarta.persistence.Entity;
import jakarta.persistence.Id; import jakarta.persistence.Id;
import jakarta.persistence.Tuple;
import jakarta.persistence.TypedQuery; import jakarta.persistence.TypedQuery;
import jakarta.persistence.criteria.CriteriaBuilder; import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery; import jakarta.persistence.criteria.CriteriaQuery;
@ -20,13 +29,18 @@ import jakarta.persistence.criteria.Expression;
import jakarta.persistence.criteria.ParameterExpression; import jakarta.persistence.criteria.ParameterExpression;
import jakarta.persistence.criteria.Root; import jakarta.persistence.criteria.Root;
import static org.assertj.core.api.Assertions.assertThat;
/** /**
* @author Will Dazy * @author Will Dazy
*/ */
@Jpa(annotatedClasses = CoalesceTest.HHH15291Entity.class) @Jpa( annotatedClasses = {
CoalesceTest.HHH15291Entity.class,
CoalesceTest.ComponentEntity.class,
CoalesceTest.ComponentA.class,
} )
@JiraKey( value = "HHH-15291") @JiraKey( value = "HHH-15291")
public class CoalesceTest { public class CoalesceTest {
@Test @Test
public void hhh15291JPQL1Test(EntityManagerFactoryScope scope) { public void hhh15291JPQL1Test(EntityManagerFactoryScope scope) {
scope.inEntityManager( scope.inEntityManager(
@ -95,9 +109,74 @@ public class CoalesceTest {
); );
} }
@Test
@Jira( "https://hibernate.atlassian.net/browse/HHH-18321" )
public void testCoalesceInBinaryArithmetic(EntityManagerFactoryScope scope) {
scope.inTransaction( entityManager -> {
final CriteriaBuilder cb = entityManager.getCriteriaBuilder();
final CriteriaQuery<Tuple> cquery = cb.createTupleQuery();
final Root<ComponentEntity> root = cquery.from( ComponentEntity.class );
cquery.select( cb.tuple(
root.get( "id" ),
cb.diff(
cb.coalesce( root.get( "componentA" ).get( "income" ), BigDecimal.ZERO ),
cb.coalesce( root.get( "componentA" ).get( "expense" ), BigDecimal.ZERO )
)
) );
final List<Tuple> resultList = entityManager.createQuery( cquery ).getResultList();
assertThat( resultList ).hasSize( 2 );
for ( Tuple result : resultList ) {
final Long id = result.get( 0, Long.class );
assertThat( result.get( 1, BigDecimal.class ).intValue() ).isEqualTo( id == 1L ? 0 : 1 );
}
} );
}
@Test
@Jira( "https://hibernate.atlassian.net/browse/HHH-18321" )
public void testCoalesceInBinaryArithmeticParam(EntityManagerFactoryScope scope) {
scope.inTransaction( entityManager -> {
final CriteriaBuilder cb = entityManager.getCriteriaBuilder();
final CriteriaQuery<Tuple> cquery = cb.createTupleQuery();
final Root<ComponentEntity> root = cquery.from( ComponentEntity.class );
final ParameterExpression<BigDecimal> defaultValue = cb.parameter( BigDecimal.class, "default-value" );
cquery.select( cb.tuple(
root.get( "id" ),
cb.diff(
defaultValue,
cb.coalesce( root.get( "componentA" ).get( "expense" ), defaultValue )
)
) );
final List<Tuple> resultList = entityManager.createQuery( cquery )
.setParameter( "default-value", BigDecimal.ZERO ).getResultList();
assertThat( resultList ).hasSize( 2 );
for ( Tuple result : resultList ) {
final Long id = result.get( 0, Long.class );
assertThat( result.get( 1, BigDecimal.class ).intValue() ).isEqualTo( id == 1L ? -1 : 0 );
}
} );
}
@BeforeAll
public void setUp(EntityManagerFactoryScope scope) {
scope.inTransaction( entityManager -> {
entityManager.persist( new ComponentEntity( 1L, new ComponentA( BigDecimal.ONE, BigDecimal.ONE ) ) );
entityManager.persist( new ComponentEntity( 2L, new ComponentA( BigDecimal.ONE, null ) ) );
} );
}
@AfterAll
public void tearDown(EntityManagerFactoryScope scope) {
scope.inTransaction( entityManager -> entityManager.createQuery( "delete from ComponentEntity" ).executeUpdate() );
}
@Entity(name = "HHH15291Entity") @Entity(name = "HHH15291Entity")
public static class HHH15291Entity { public static class HHH15291Entity {
@Id @Id
@Column(name = "KEY_CHAR") @Column(name = "KEY_CHAR")
private String KeyString; private String KeyString;
@ -116,53 +195,36 @@ public class CoalesceTest {
@Column(name = "ITEM_INTEGER1") @Column(name = "ITEM_INTEGER1")
private Integer itemInteger1; private Integer itemInteger1;
public String getKeyString() {
return KeyString;
} }
public void setKeyString(String keyString) { @Entity( name = "ComponentEntity" )
KeyString = keyString; static class ComponentEntity {
@Id
private Long id;
@Embedded
private ComponentA componentA;
public ComponentEntity() {
} }
public String getItemString1() { public ComponentEntity(Long id, ComponentA componentA) {
return itemString1; this.id = id;
this.componentA = componentA;
}
} }
public void setItemString1(String itemString1) { @Embeddable
this.itemString1 = itemString1; static class ComponentA {
private BigDecimal income;
private BigDecimal expense;
public ComponentA() {
} }
public String getItemString2() { public ComponentA(BigDecimal income, BigDecimal expense) {
return itemString2; this.income = income;
} this.expense = expense;
public void setItemString2(String itemString2) {
this.itemString2 = itemString2;
}
public String getItemString3() {
return itemString3;
}
public void setItemString3(String itemString3) {
this.itemString3 = itemString3;
}
public String getItemString4() {
return itemString4;
}
public void setItemString4(String itemString4) {
this.itemString4 = itemString4;
}
public Integer getItemInteger1() {
return itemInteger1;
}
public void setItemInteger1(Integer itemInteger1) {
this.itemInteger1 = itemInteger1;
} }
} }
} }