I want to create a querydsl
OrderSpecifier
expression that orders by the minutes difference of two sql TIME
values:
@Generated
public class QStock extends EntityPathBase<Stock> {
public final TimePath<java.time.LocalTime> time1 = createTime(..);
public final TimePath<java.time.LocalTime> time2 = createTime(..);
}
Pseudocode:
QStock.stock.time1.minutesTo(QStock.stock.time2).desc();
Of course the minutesTo()
method does not exist. But how can I achieve this?
Of course there is no need that the difference is calculated in minutes. I just want to put the biggest difference between those 2 times sorted topdown.
I tried as follows, but getting an exception:
Expressions.numberOperation(Integer.class, Ops.DateTimeOps.DIFF_MINUTES, QStock.stock.time1, QStock.stock.time2)
Caused by: java.sql.SQLSyntaxErrorException: (conn=1326) FUNCTION table.diff_minutes does not exist
at org.mariadb.jdbc.internal.util.exceptions.ExceptionMapper.get(ExceptionMapper.java:236) ~[mariadb-java-client-2.3.0.jar:?]
at org.mariadb.jdbc.internal.util.exceptions.ExceptionMapper.getException(ExceptionMapper.java:165) ~[mariadb-java-client-2.3.0.jar:?]
So I tried adding the TIMEDIFF
function string explicit to querydsl:
//inspired by Ops.DateTimeOps
enum MysqlDateTimeOps implements Operator {
TIMEDIFF(Comparable.class);
private final Class<?> type;
MysqlDateTimeOps(Class<?> type) {
this.type = type;
}
@Override
public Class<?> getType() {
return type;
}
}
Using:
Expressions.numberOperation(Integer.class, MysqlDateTimeOps.TIMEDIFF, QStock.stock.time1, QStock.stock.time2)
Result:
org.springframework.dao.InvalidDataAccessApiUsageException: No pattern found for TIMEDIFF; nested exception is java.lang.IllegalArgumentException: No pattern found for TIMEDIFF
at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:373) ~[spring-orm-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:255) ~[spring-orm-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:527) ~[spring-orm-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61) ~[spring-tx-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242) ~[spring-tx-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:153) ~[spring-tx-5.1.9.RELEASE.jar:5.1.9.RELEASE]
So how can I use the TIMEDIFF
function then in querydsl?
尽管我希望有一个更简单的解决方案,但仍可以进行以下工作:
Expressions.timeTemplate(Integer.class, "TIMEDIFF({0}, {1})", Arrays.asList(QStock.stock.time1, QStock.stock.time2));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.