gpt4 book ai didi

java - 不同字段值长度的 Hibernate 标准

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:45:21 26 4
gpt4 key购买 nike

我有一个实体,它有一个大小为 10 的(字符串)字段

@Column(length = 10)
String code;

但是,字段的值长度可以是 5 或 10。我想创建一个 hibernate 条件来根据字段的长度匹配该字段的值,例如:

final List<String> codesLong= new ArrayList<String>();
final List<String> codesShort= new ArrayList<String>();
...
criteria.add(Restrictions.or(
Restrictions.and(Restrictions.in("code", codesShort),
Restrictions.eq("code.length", 5)),
Restrictions.and(Restrictions.in("code", codesLong),
Restrictions.eq("code.length", 10)))
);

然而...以上看起来不错,但显然行不通。我不知道如何处理那个问题。如果有任何提示,我将不胜感激。

谢谢

//解决方案(感谢 Stanislav!)实体:

@Column(length = 10)
String code;

@Formula(" length(code) ")
int codelength; // create GETter method as well

标准:

final List<String> codesLong= new ArrayList<String>();
final List<String> codesShort= new ArrayList<String>();
...
criteria.add(Restrictions.or(
Restrictions.and(Restrictions.in("code", codesShort),
Restrictions.eq("codelength", 5)),
Restrictions.and(Restrictions.in("code", codesLong),
Restrictions.eq("codelength", 10)))
);

最佳答案

你可以引入一个人工的 Entity 字段 codeLength 并使用 @Formula 注解(例如 here )

并在标准中使用 codeLength。

关于java - 不同字段值长度的 Hibernate 标准,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28235541/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com