cod*_*eek 3 postgresql hibernate hql hibernate-mapping spring-data-jpa
我的第一个实体类中有一个数组字段,如下所示:
class Entity1{
private Integer col1;
private String col2;
private Integer[] col3Arr;
}
Run Code Online (Sandbox Code Playgroud)
我有另一个实体类,如下所示:
class Entity2{
private Integer col1;
private String col2;
private Integer col3;
}
Run Code Online (Sandbox Code Playgroud)
我通过加入多个其他实体来获取记录,如果 col3Arr 包含来自实体 2 的值 col3,则我必须加入实体 1
使用 PSQL,我可以通过以下语句轻松实现这一点
//Other part of query
join Entity2 e2 on (//conditions from other joined tables//)
join Entity1 e1 on e2.col3=ANY(e1.col3Arr)
Run Code Online (Sandbox Code Playgroud)
ANY 的 HQL 等效项是什么?或者HQL中有没有其他方法来检查数组是否包含特定值?
为了映射数组,您将需要一个自定义类型。您可以使用 hibernate-types 项目来实现此目的:https://vladmihalcea.com/how-to-map-java-and-sql-arrays-with-jpa-and-hibernate/
您尝试使用e2.col3 = FUNCTION('ANY', e1.col3Arr)了吗?如果这不起作用,我建议您创建一个自定义SQLFunction来呈现您想要的 SQL,例如
public class ArrayAny 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 firstArgumentType;
}
@Override
public String render(Type firstArgumentType, List args, SessionFactoryImplementor factory) throws QueryException {
return "any(" + args.get(0) + ")";
}
}
Run Code Online (Sandbox Code Playgroud)
您必须在方言中注册该函数。
| 归档时间: |
|
| 查看次数: |
2611 次 |
| 最近记录: |