HHH-16305 Add test for issue

This commit is contained in:
Marco Belladelli 2023-03-22 11:28:18 +01:00
parent aafb7cdd45
commit 4543113e68
1 changed files with 35 additions and 26 deletions

View File

@ -6,61 +6,70 @@
*/
package org.hibernate.orm.test.query;
import org.hibernate.testing.orm.domain.gambit.BasicEntity;
import java.time.LocalDate;
import org.hibernate.testing.orm.domain.gambit.EntityOfBasics;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.JiraKey;
import org.hibernate.testing.orm.junit.Jira;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import jakarta.persistence.Tuple;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* @author Marco Belladelli
*/
@SessionFactory
@DomainModel(annotatedClasses = BasicEntity.class)
@JiraKey("HHH-16137")
@DomainModel( annotatedClasses = EntityOfBasics.class )
public class NamedParameterInSelectAndWhereTest {
@BeforeAll
public void setUp(SessionFactoryScope scope) {
scope.inTransaction( session -> {
BasicEntity be1 = new BasicEntity( 1, "one" );
session.persist( be1 );
BasicEntity be2 = new BasicEntity( 2, "two" );
session.persist( be2 );
final EntityOfBasics e1 = new EntityOfBasics( 1 );
final EntityOfBasics e2 = new EntityOfBasics( 2 );
e2.setTheLocalDate( LocalDate.EPOCH );
session.persist( e1 );
session.persist( e2 );
} );
}
@AfterAll
public void tearDown(SessionFactoryScope scope) {
scope.inTransaction( session -> session.createMutationQuery( "delete from BasicEntity" ).executeUpdate() );
scope.inTransaction( session -> session.createMutationQuery( "delete from EntityOfBasics" ).executeUpdate() );
}
@Test
@Jira( "https://hibernate.atlassian.net/browse/HHH-16137" )
public void testSelectAndWhere(SessionFactoryScope scope) {
scope.inTransaction( session -> assertEquals(
1,
session.createQuery( "SELECT :param FROM BasicEntity be WHERE be.id > :param", Integer.class )
.setParameter( "param", 1 )
.getSingleResult()
) );
scope.inTransaction( session -> assertEquals( 1, session.createQuery(
"select :param from EntityOfBasics e where e.id > :param",
Integer.class
)
.setParameter( "param", 1 )
.getSingleResult() ) );
}
@Test
@Jira( "https://hibernate.atlassian.net/browse/HHH-16137" )
public void testSelectAndWhereIsNull(SessionFactoryScope scope) {
scope.inTransaction( session -> assertEquals(
1,
session.createQuery(
"SELECT :param FROM BasicEntity be WHERE :param is null or be.id > :param",
Integer.class
)
.setParameter( "param", 1 )
.getSingleResult()
) );
scope.inTransaction( session -> assertEquals( 1, session.createQuery(
"select :param from EntityOfBasics be where :param is null or be.id > :param",
Integer.class
).setParameter( "param", 1 )
.getSingleResult() ) );
}
@Test
@Jira( "https://hibernate.atlassian.net/browse/HHH-16305" )
public void testSelectFunctionAndWhere(SessionFactoryScope scope) {
scope.inTransaction( session -> assertEquals( 0, session.createQuery(
"select timestampdiff(year, e.theLocalDate, :date) from EntityOfBasics e where e.theLocalDate <= :date",
Long.class
)
.setParameter( "date", LocalDate.EPOCH )
.getSingleResult() ) );
}
}