spring Hibernate 6 for Sping Boot 6.2不支持unaccent

dluptydi  于 12个月前  发布在  Spring
关注(0)|答案(1)|浏览(140)

当尝试从Hibernate 5切换到Hibernate 6时,不带重音的搜索不起作用,导致错误:
原因:java.lang.IllegalArgumentException:org.hibernate.query.SemanticException:“like”的操作数属于“java.lang.Object”类型,但不是字符串(它不是“java.lang.String”或“char[String]”的示例)
Hibernate 6将unaccent的结果检测为Object而不是String,这意味着我们不能对它做LIKE。
代码:

@Query(value = "SELECT myentity FROM MyEntity myentity WHERE unaccent(LOWER(myentity.name)) like CONCAT('%', :name, '%')")
List<MyEntity> searchByName(String search);

字符串
我试图删除CONCAT或更低,问题真的来自unaccent

@Query(value = "SELECT myentity FROM MyEntity myentity WHERE unaccent(myentity.name) like :search")
List<MyEntity> searchByName(String search);


Thanks in advance,Regards,BrandonFL

a64a0gku

a64a0gku1#

unaccent不知道PSQL是否会返回字符串,因此SQM返回错误,因为LIKE仅适用于字符串
https://docs.jboss.org/hibernate/orm/6.0/migration-guide/migration-guide.html#query-sqmhttps://docs.jboss.org/hibernate/orm/6.0/userguide/html_single/Hibernate_User_Guide.html#hql-functions-typecasts
我们在Hibernate中注册函数来解决这个问题

import org.hibernate.boot.model.FunctionContributions;
import org.hibernate.boot.model.FunctionContributor;
import org.hibernate.type.StandardBasicTypes;

/**
 * Register the unaccent function for Postgres.
 */
public class UnaccentPsqlFunction implements FunctionContributor  {
    @Override
    public void contributeFunctions(FunctionContributions functionContributions) {
        functionContributions.getFunctionRegistry().registerNamed(
            "unaccent",
            functionContributions.getTypeConfiguration().getBasicTypeRegistry().resolve(StandardBasicTypes.STRING)
        );
    }
}

字符串
然后创建一个文件META-INF/services/org.hibernate.boot.model.FunctionContributor,并在其中添加对类的引用:

com.foo.UnaccentPsqlFunction

相关问题