Hibernate Query 中 mysql RLIKE 运算符的等效项是什么?

What's the equivalent of mysql RLIKE operator in Hibernate Query?

我有以下查询 运行 完全符合 mysql。

SELECT * FROM Orders as o, Products as p  where o.productinfo RLIKE p.code;

这里我用 RLIKE 连接两个表 Orders 和 Products。

我正尝试在 Hibernate 中实现相同的功能。

Query query = session.createQuery("FROM Orders as o, Products as p  where o.productinfo RLIKE p.code");
List<Object[]> results = query.getResultList();

我在使用RLIKE的时候,在运行的时候抛出如下错误。

{"errormessage":"org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: RLIKE 

我尝试用 LIKE 查询实现相同的功能,并将其与“%p.code%”匹配。

Query query = session.createQuery("FROM Orders as o, Products as p  where o.productinfo LIKE '%p.code%'");

但它匹配字符串 "p.code" 而不是值。

HQL 中的 RLIKE 等价于什么? 在 HQL 中使用 LIKE 连接两个表有不同的方法吗?

谢谢。

@YCF_L的回答: 对于任何试图在 Hibernate (mysql) 中使用 like 运算符连接两个表的人,都可以通过以下方式进行。

SELECT * FROM Orders as o, Products as p  where o.productinfo LIKE CONCAT('%',p.code,'%');

What's the equivalent of mysql RLIKE operator in Hibernate Query?

RLIKEREGEXP 的同义词,因此您可以使用 REGEXP_LIKE 在休眠中实现它,您可以在这里查看:


I tried to implement the same with LIKE query and matched it with '%p.code%'.

..., Products as p  where o.productinfo LIKE '%p.code%'");

But it matches with the string "p.code" rather then the value.

这是真的,因为你没有传递 p.code 的正确值,你像字符串一样传递它,相反你有两种方式:

Query query = session.createQuery("....Products as p  where o.productinfo LIKE '%:code%'");
//------------------------------------------------------------------------------^^^^^^^
query.setParameter("code", p.code);

或者您可以将您的代码与您的查询连接起来,但第一个解决方案更好。

Query query = session.createQuery("....  where o.productinfo LIKE '%" + p.code + "%'");

编辑

您可以像这样使用 CONCAT 而无需指定 '' :

SELECT * FROM Orders as o, Products as p  where o.productinfo LIKE CONCAT('%',p.code,'%');

您可以像这样检查正则表达式:Restrictions.sqlRestriction(word REGEXP '^[A-Z][A-Za-z]*$')

请检查link,这对这种情况有帮助:Regular expression with criteria