[英]Custom Scalar Valued function in Hibernate 5.x
I have a custom Scalar Valued function in SQL with the below details:我在 SQL 中有一个自定义标量值函数,详细信息如下:
I'm using Hibernate 5.4.6 to fetch some records from the database.我正在使用 Hibernate 5.4.6 从数据库中获取一些记录。 I want to use the above function in the HQL (in the Where Clause), however currently facing issues.
我想在 HQL 中使用上述函数(在 Where 子句中),但是目前面临问题。
Query<Device> query = session.createQuery("Select ud from Device ud where fn_WorkDays(ua.created, current_date())>2", Device.class);
After going through some posts, I understand that the custom function needs to be registered before using it, hence have created the below class:经过一些帖子后,我了解到自定义函数需要在使用之前进行注册,因此创建了以下类:
public class MySQLServerDialect extends SQLServerDialect {
public MySQLServerDialect() {
super();
registerFunction("fn_WorkDays", new VarArgsSQLFunction(StandardBasicTypes.INTEGER, "fn_WorkDays(", ",", ")"));
}
}
Have updated the hibernate.cfg.xml with :用以下内容更新了 hibernate.cfg.xml:
<property name="dialect">com.test.service.utils.MySQLServerDialect</property>
I dont seem to understand how the registerFunction should be done as im getting the below error: ERROR SqlExceptionHelper:142 - 'fn_WorkDays' is not a recognized function name.我似乎不明白 registerFunction 应该如何完成,因为我收到以下错误:错误 SqlExceptionHelper:142 - 'fn_WorkDays' 不是一个可识别的函数名称。
Log4j2.xml Log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<!-- Console Appender -->
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout
pattern="%d{yyyy-MMM-dd HH:mm:ss a} [%t] %-5level %logger{36}:%L - %msg%n" />
</Console>
</Appenders>
<Loggers>
<!-- Log everything in hibernate -->
<Logger name="org.hibernate" level="debug" additivity="false">
<AppenderRef ref="Console" />
</Logger>
<!-- Log SQL statements -->
<Logger name="org.hibernate.SQL" level="debug" additivity="false">
<AppenderRef ref="Console" />
</Logger>
<!-- Log JDBC bind parameters -->
<Logger name="org.hibernate.type.descriptor.sql" level="trace" additivity="false">
<AppenderRef ref="Console" />
</Logger>
<!-- Log custom packages -->
<Logger name="com.test.service" level="debug" additivity="false">
<AppenderRef ref="Console" />
</Logger>
<Root level="error">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
Any help to resolve the above will be greatly appreciated.任何解决上述问题的帮助将不胜感激。
Functions that are not pre-defined in the JPQL/HQL are invoked like this: JPQL/HQL 中未预定义的函数的调用方式如下:
`function('fn_WorkDays' , ua.created, current_date())`
This is later translated to fn_WorkDays(ua.created, current_date())
in the native query.这稍后会在本机查询中转换为
fn_WorkDays(ua.created, current_date())
。 You still need to have it registered by the dialect as you have.你仍然需要像你一样用方言注册它。
ERROR SqlExceptionHelper:142 - 'fn_WorkDays' is not a recognized function name.
错误 SqlExceptionHelper:142 - 'fn_WorkDays' 不是可识别的函数名称。
The error already says what is wrong, the function does not exist in the database.错误已经说明出了什么问题,该函数在数据库中不存在。
Try this:尝试这个:
registerFunction("fn_WorkDays", new VarArgsSQLFunction(StandardBasicTypes.INTEGER, "dbo.fn_WorkDays(", ",", ")"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.