简体   繁体   中英

Any way to use custom function defined in the dialect in the @Formula or @CustomTransformer annotation

I have to support Redshift and Clickhose. I have two dialect extentions:

public class RedshiftDialect extends PostgreSQL94Dialect {
    public RedshiftDialect() {
        super();
        registerFunction("to_date",
                new SQLFunctionTemplate(
                        StandardBasicTypes.DATE,
                        "to_date(to_char(?1, 'YYYY-MM-DD'), 'YYYY-MM-DD')"));
    }
}

public class ClickHouseDialect extends Dialect {

    private static final LimitHandler LIMIT_HANDLER =
        new AbstractLimitHandler() {
            @Override
            public String processSql(String sql, RowSelection selection) {
                final boolean hasOffset = LimitHelper.hasFirstRow(selection);
                return sql + (hasOffset ? " limit ? offset ?" : " limit ?");
            }

            @Override
            public boolean supportsLimit() {
                return true;
            }

            @Override
            public boolean bindLimitParametersInReverseOrder() {
                return true;
            }
        };

    public ClickHouseDialect() {
        super();
        registerColumnType(Types.VARCHAR, "Nullable(String)");
        registerColumnType(Types.BIGINT, "Nullable(Int64)");
        registerColumnType(Types.DATE, "Nullable(Date)");
        registerColumnType(Types.TIMESTAMP, "Nullable(DateTime)");
        registerColumnType(Types.FLOAT, "Nullable(Float64)");
        registerFunction("to_date",
                new StandardSQLFunction(
                        "toDate",
                        StandardBasicTypes.DATE));
    }

    @Override
    public LimitHandler getLimitHandler() {
        return LIMIT_HANDLER;
    }
}


public class RedshiftDialect extends PostgreSQL94Dialect {
    public RedshiftDialect() {
        super();
        registerFunction("to_date",
                new SQLFunctionTemplate(
                        StandardBasicTypes.DATE,
                        "to_date(to_char(?1, 'YYYY-MM-DD'), 'YYYY-MM-DD')"));
    }
}

Here is my entity mapping:

@Immutable
@Entity(name = "ReportEntry")
@Table(name = "reporting")
public class ReportEntry implements Serializable {
    ...
    @Dimension
    @Formula("to_date(date)")
    @Column(nullable = false)
    private Instant date;
    ...
}

I'd like to that whenever I mention date attribute in my hql the to_date(date) expression evaluated depends on current dialect, but unfortunately @Formula annotation accepts only native sql expression. Any ideas how use custom function with formula?

Thank in advance

You probably better implement a custom type instead.

See http://docs.jboss.org/hibernate/orm/5.3/userguide/html_single/Hibernate_User_Guide.html#basic-custom-type

(You probably find tutorials which are a better resource to implement your own type).

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM