mysql – SEPARATOR关键字在Hibernate Formula中无法正常工作
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了mysql – SEPARATOR关键字在Hibernate Formula中无法正常工作,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3671字,纯文字阅读大概需要6分钟。
内容图文
![mysql – SEPARATOR关键字在Hibernate Formula中无法正常工作](/upload/InfoBanner/zyjiaocheng/904/de33de825a55461e93435eea2ac3e7d0.jpg)
我有以下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并将结果以小写形式添加到生成的方言中:
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关键字.
还有另一种选择,它更加独立于数据库(我更喜欢).创建以下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
内容总结
以上是互联网集市为您收集整理的mysql – SEPARATOR关键字在Hibernate Formula中无法正常工作全部内容,希望文章能够帮你解决mysql – SEPARATOR关键字在Hibernate Formula中无法正常工作所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。