Java 不同字段值长度的休眠条件

Java 不同字段值长度的休眠条件,java,hibernate,criteria,Java,Hibernate,Criteria,我有一个实体,它有一个大小为10的字符串字段 @Column(length = 10) String code; 但是,字段的值长度可以是5或10。我想创建一个hibernate条件,根据字段的长度匹配该字段的值,如: final List<String> codesLong= new ArrayList<String>(); final List<String> codesShort= new ArrayList<String>(); ...

我有一个实体,它有一个大小为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)))
);
标准:

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)))
);

您可以引入一个人工实体字段codeLength并使用@Formula注释示例


并在条件中使用codeLength。

code.length指的是字符串或其他内容中的字符数。code是一个简单的字符串代码。长度只是为了形象化我想归档的内容
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)))
);