我有以下Hibernate forumla查询,我可以在mysql workbanch中执行该查询。
select group_concat(distinct t.column_1_name SEPARATOR ', ') from table_name t and t.fk_record_id = record_id
使用Hibernate执行此查询时,hibernate会将父表追加到SEPRATOR关键字,如以下查询所示。
select group_concat(distinct t.column_1_name parent_table.SEPARATOR ', ') from table_name t and t.fk_record_id = record_id
在这里,hibernate不将SEPRATOR当作关键字。有人对此有任何想法吗?
您可以添加SEPARATOR为关键字。实现自己DialectResolver的关键字,并将关键字 以小写形式 添加到所得的方言中:
SEPARATOR
DialectResolver
public class MyDialectResolver implements DialectResolver { public Dialect resolveDialect(DialectResolutionInfo info) { for (Database database : Database.values()) { Dialect dialect = database.resolveDialect(info); if (dialect != null) { dialect.getKeywords().add("separator"); return dialect; } } return null; } }
对于5.2.13 / 5.3.0 之前的 Hibernate版本相同:
public class MyDialectResolver extends StandardDialectResolver { protected Dialect resolveDialectInternal(DatabaseMetaData metaData) throws SQLException { Dialect dialect = super.resolveDialectInternal(metaData); dialect.getKeywords().add("separator"); return dialect; } }
然后,您将不得不告诉Hibernate使用您的方言解析器。例如,在JPA中,您可以在persistence.xml中执行此操作:
<persistence> <persistence-unit> ... <property name="hibernate.dialect_resolvers" value="mypackage.MyDialectResolver"/> </persistence-unit> </persistence>
其他方言中的汇总功能也是如此。例如,在Oracle中,WITHIN缺少关键字。
WITHIN
还有另一种选择,它更独立于数据库(我更喜欢)。创建以下内容SQLFunction:
SQLFunction
public class ListAggFunction implements SQLFunction { /** * The pattern that describes how the function is build in SQL. * * Replacements: * {path} - is replaced with the path of the list attribute * {separator} - is replaced with the separator (defaults to '') * {orderByPath} - is replaced by the path that is used for ordering the elements of the list */ private String pattern; /** * Creates a new ListAggFunction definition which uses the ANSI SQL:2016 syntax. */ public ListAggFunction() { this("LISTAGG(DISTINCT {path}, {separator}) WITHIN GROUP(ORDER BY {orderByPath})"); } /** * Creates a new ListAggFunction definition which uses a database specific syntax. * * @param pattern The pattern that describes how the function is build in SQL. */ public ListAggFunction(String pattern) { this.pattern = pattern; } public Type getReturnType(Type firstArgumentType, Mapping mapping) throws QueryException { return StringType.INSTANCE; } public boolean hasArguments() { return true; } public boolean hasParenthesesIfNoArguments() { return true; } public String render(Type firstArgumentType, List arguments, SessionFactoryImplementor factory) throws QueryException { if (arguments.isEmpty() || arguments.size() > 3) { throw new IllegalArgumentException( "Expected arguments for 'listagg': path [, separator [, order by path]]"); } String path = (String) arguments.get(0); String separator = arguments.size() < 2 ? "''" : (String) arguments.get(1); String orderByPath = arguments.size() <= 2 ? path : (String) arguments.get(2); return StringUtils.replaceEach(this.pattern, new String[] { "{path}", "{separator}", "{orderByPath}" }, new String[] { path, separator, orderByPath }); } }
您可以使用与上面的关键字相同的方式在DialectResolver中注册此功能:
if ("MySQL".equals(info.getDatabaseName()) || "H2".equals(info.getDatabaseName())) { dialect.getFunctions().put("listagg", new ListAggFunction("GROUP_CONCAT(DISTINCT {path} ORDER BY {orderByPath} SEPARATOR {separator})")); } else { dialect.getFunctions().put("listagg", new ListAggFunction()); }
现在,您可以在JPQL / HQL / Criteria查询中使用此功能,而无需考虑方言的语法:
SELECT e.group, listagg(e.stringProperty, ', ') FROM Entity e GROUP BY e.group