org.hibernate.QueryException: CAST function should only have 2 arguments

By looking at the source code of Hibernate 5, this looks like a limitation of the SQL rendering when the function is unknown. I’d suggest you register the function JSON_EXTRACT into your Dialect, then it should work fine e.g.

public class JsonExtract implements SQLFunction {

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

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

    @Override
    public Type getReturnType(Type firstArgumentType, Mapping mapping) throws QueryException {
        return StandardBasicTypes.STRING;
    }

    @Override
    public String render(Type firstArgumentType, List args, SessionFactoryImplementor factory) throws QueryException {
        return "json_extract(" + args.get(0) + "," + args.get(1) + ")";
    }
}
2 Likes