HHH-13908 fix the issue MySQL's 'time' function issue in Criteria

This commit is contained in:
Nathan Xu 2020-08-28 13:35:46 -04:00 committed by Christian Beikov
parent 5b9ec29ecb
commit 41161f9fa9
3 changed files with 62 additions and 1 deletions

View File

@ -194,7 +194,7 @@ public class MySQLDialect extends Dialect {
registerFunction( "second", new StandardSQLFunction( "second", StandardBasicTypes.INTEGER ) );
registerFunction( "sec_to_time", new StandardSQLFunction( "sec_to_time", StandardBasicTypes.TIME ) );
registerFunction( "sysdate", new NoArgSQLFunction( "sysdate", StandardBasicTypes.TIMESTAMP ) );
registerFunction( "time", new StandardSQLFunction( "time", StandardBasicTypes.TIME ) );
registerFunction( "time", new StandardSQLFunction( "time", StandardBasicTypes.STRING ) );
registerFunction( "timestamp", new StandardSQLFunction( "timestamp", StandardBasicTypes.TIMESTAMP ) );
registerFunction( "time_to_sec", new StandardSQLFunction( "time_to_sec", StandardBasicTypes.INTEGER ) );
registerFunction( "to_days", new StandardSQLFunction( "to_days", StandardBasicTypes.LONG ) );

View File

@ -0,0 +1,13 @@
package org.hibernate.query.criteria.internal.hhh13908;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity(name = "Foo")
@Table(name = "Foo")
public class Foo {
@Id
Long id;
String startTime;
}

View File

@ -0,0 +1,48 @@
package org.hibernate.query.criteria.internal.hhh13908;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import org.hibernate.dialect.MySQLDialect;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.hibernate.testing.RequiresDialect;
import org.hibernate.testing.TestForIssue;
import org.junit.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
/**
* @author Archie Cobbs
* @author Nathan Xu
*/
@RequiresDialect( MySQLDialect.class )
public class HHH13908Test extends BaseEntityManagerFunctionalTestCase {
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] { Foo.class };
}
@Test
@TestForIssue( jiraKey = "HHH-13908" )
public void testTimeFunctionNotThrowException() {
doInJPA( this::entityManagerFactory, entityManager -> {
final CriteriaBuilder cb = entityManager.getCriteriaBuilder();
final CriteriaQuery<Foo> cq = cb.createQuery( Foo.class );
final Root<Foo> foo = cq.from( Foo.class );
cq.select( foo )
.where(
cb.lessThanOrEqualTo(
cb.function( "TIME", String.class, foo.get( Foo_.startTime ) ),
"17:00:00"
)
);
// without fixing, the following exception will be thrown:
// Parameter value [17:00:00] did not match expected type [java.util.Date (n/a)]
//java.lang.IllegalArgumentException: Parameter value [17:00:00] did not match expected type [java.util.Date (n/a)]
entityManager.createQuery( cq ).getResultList();
} );
}
}