简体   繁体   English

Hibernate 5.x 中的自定义标量值函数

[英]Custom Scalar Valued function in Hibernate 5.x

I have a custom Scalar Valued function in SQL with the below details:我在 SQL 中有一个自定义标量值函数,详细信息如下:

  • Name : fn_WorkDays名称: fn_WorkDays
  • Returns : Int返回:整数
  • Parameters : 1. StartDate (type - datetime) 2. EndDate (type - datetime)参数: 1. StartDate (type - datetime) 2. EndDate (type - datetime)

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.

相关问题 Hibernate 5.x迁移建议脚本 - Hibernate 5.x migration suggestions script 集成器无法在Hibernate 5.x中正确加载 - Integrator not properly loading in Hibernate 5.x 使用H2数据库的Junit 4.x + Hibernate 5.x. - Junit 4.x + Hibernate 5.x with H2 database Hibernate 5.x + Spring 5.x,无法自动装配DAOImpl类中的SessionFactory - Hibernate 5.x + Spring 5.x, can't autowire SessionFactory in DAOImpl class 在Hibernate 5.x中使用本机查询(更新查询)执行查询 - Execute query with native query (update query) in Hibernate 5.x 是否可以使用Hibernate Search 5.X对@Id字段使用数字编码 - Is it possible to use a numeric encoding for @Id fields with Hibernate Search 5.X Hibernate 5.x 中的 HQL 和 Criteria API 有什么区别? - What is difference between HQL and Criteria API in Hibernate 5.x? SessionImplementor是否可以访问Hibernate 5.x中的TransactionManager? - Does SessionImplementor have access to a TransactionManager in Hibernate 5.x? Hibernate从4.3.x迁移到5.x,用于方法org.hibernate.cfg.Configuration.getClassMapping(className) - Hibernate Migration from 4.3.x to 5.x for method org.hibernate.cfg.Configuration.getClassMapping(className) 多线程时 Hibernate 从 4.x 升级到 5.x 和 Spring 4.x 时出现 ConcurrentModificationException - ConcurrentModificationException when Hibernate Upgrade from 4.x to 5.x and Spring 4.x for Multi-Threaded
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM