Jpa规范不能调用包含函数

时间:2015-11-25 08:36:01

标签: jpa full-text-search contains

我有一个实体类

@Entity
@Table(name = "P_PERSON")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Document(indexName="p_person")
public class P_person implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Size(max = 255)
    @Column(name = "first_name", length = 255)
    private String firstName;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getFirst_name() {
        return firstName;
    }

    public void setFirst_name(String first_name) {
        this.firstName = first_name;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        P_person p_person = (P_person) o;

        if ( ! Objects.equals(id, p_person.id)) return false;

        return true;
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(id);
    }

    @Override
    public String toString() {
        return "P_person{" +
                "id=" + id +
                ", first_name='" + firstName + "'" +
                '}';
    }
}

和元模型

@Generated(value = "org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor")
@StaticMetamodel(P_person.class)
public abstract class P_person_ {
    public static volatile SingularAttribute<P_person, Long> id;
    public static volatile SingularAttribute<P_person, String> firstName;

}

存储库类

public interface P_personRepository extends JpaRepository<P_person,Long>, JpaSpecificationExecutor<P_person> {

}

和休息api功能

@RequestMapping(value = "/person_query",
    method = RequestMethod.GET)
@RolesAllowed(AuthoritiesConstants.USER)
@Timed
public ResponseEntity<List<P_person>> person_query(@RequestParam(value = "name") String name) throws URISyntaxException {
    Pageable pageable = PaginationUtil.generatePageRequest(offset, limit, direction, property);
    Page<P_person> page = p_personRepository.findAll(CustomSpecification.whereP_person_query(name), pageable);
    HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/person_query", offset, limit);
    return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);

}
CustomSpecification中的

我有生成规范的函数

public static Specification<P_person> whereP_person_query(String name) {
    return new Specification<P_person>() {
        @Override
        public Predicate toPredicate(Root<P_person> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {

            return criteriaBuilder.like(root.get(P_person_.firstName), "%" + name + "%");

        }
    };
}

当我在MS-SQL

中改变使用包含函数时,我的问题就开始了

来自

select * from P_PERSON where first_name like N'%John%'

select * from P_PERSON where  CONTAINS(first_name, N'John')

我尝试了几种方法,但它仍然失败

第一

public static Specification<P_person> whereP_person_query(String name) {
    return new Specification<P_person>() {
        @Override
        public Predicate toPredicate(Root<P_person> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {

            Expression<Boolean> contains = criteriaBuilder.function("CONTAINS", Boolean.class, root.get(P_person_.firstName), criteriaBuilder.literal(name));
            return criteriaBuilder.isTrue(contains);

        }
    };
}

这个生成sql:

select count(p_person0_.id) as col_0_0_ from P_PERSON p_person0_ where CONTAINS(p_person0_.first_name, ?)=1

数据库响应错误,“=”附近的语法不正确。

第二

public static Specification<P_person> whereP_person_query(String name) {
    return new Specification<P_person>() {
        @Override
        public Predicate toPredicate(Root<P_person> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {

            Expression<Boolean> contains = criteriaBuilder.function("CONTAINS", Boolean.class, root.get(P_person_.firstName), criteriaBuilder.literal(name));
            return criteriaQuery.where(contains).getRestriction();

        }
    };
}

这一个返回antlr.NoViableAltException:意外的AST节点:function(CONTAINS) 生成sql:

select generatedAlias0 from com.amlo.aers.domain.P_person as generatedAlias0
where function('CONTAINS', generatedAlias0.firstName, :param0) order by generatedAlias0.id asc

第三 我将返回类更改为Integer并希望它可以工作。

public static Specification<P_person> whereP_person_query(String name) {
    return new Specification<P_person>() {
        @Override
        public Predicate toPredicate(Root<P_person> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {

            Expression<Integer> contains = criteriaBuilder.function("CONTAINS", Integer.class, root.get(P_person_.firstName), criteriaBuilder.literal(name));
            return criteriaBuilder.greaterThan(contains, 0);

        }
    };
}

它生成sql:

select count(p_person0_.id) as col_0_0_ from P_PERSON p_person0_ where CONTAINS(p_person0_.first_name, ?)>0

返回错误org.hibernate.engine.jdbc.spi.SqlExceptionHelper - '&gt;'附近的语法不正确。

第四

public static Specification<P_person> whereP_person_query(String name) {
    return new Specification<P_person>() {
        @Override
        public Predicate toPredicate(Root<P_person> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {

            Predicate contains = (Predicate) criteriaBuilder.function("CONTAINS", Boolean.class, root.get(P_person_.firstName), criteriaBuilder.literal(name));
            return contains;

        }
    };
}

仍然是错误 java.lang.ClassCastException:org.hibernate.jpa.criteria.expression.function.ParameterizedFunctionExpression无法强制转换为javax.persistence.criteria.Predicate

第五

public static Specification<P_person> whereP_person_query(String name) {
    return new Specification<P_person>() {
        @Override
        public Predicate toPredicate(Root<P_person> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {

            Expression<Boolean> contains = criteriaBuilder.function("CONTAINS", Boolean.class, root.get(P_person_.firstName), criteriaBuilder.literal(name));
            return criteriaBuilder.not(criteriaBuilder.not(contains));

        }
    };
}

仍然返回antlr.NoViableAltException:意外的AST节点:function(CONTAINS)

第六 读完我找到的所有资源后。

public static Specification<P_person> whereP_person_query(String name) {
    return new Specification<P_person>() {
        @Override
        public Predicate toPredicate(Root<P_person> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {

            Expression<Boolean> contains = criteriaBuilder.function("CONTAINS", Boolean.class, root.get(P_person_.firstName), criteriaBuilder.literal(name));
            criteriaQuery.where(contains);
            return null;

        }
    };
}

仍然返回antlr.NoViableAltException:意外的AST节点:function(CONTAINS)

“whereP_person_query”可以轻松替换为本机查询。 但我的问题是有一些规范结合了许多标准。 所以我创建了“whereP_person_query”来证明在这个项目中使用全文搜索。

真的可以这样做吗?

提前致谢。

2 个答案:

答案 0 :(得分:1)

从这个问题https://hibernate.atlassian.net/browse/HHH-3992开始 我尝试应用它和这段代码。

public static Specification<P_person> whereP_person_query(String name) {
    return new Specification<P_person>() {
        @Override
        public Predicate toPredicate(Root<P_person> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
            return criteriaBuilder.and(criteriaBuilder.and(), criteriaBuilder.function("CONTAINS", Boolean.class, root.get(P_person_.firstName), criteriaBuilder.literal(name)));
        }
    };
}

但最终由于在一个SQL中执行多包含功能,我无法使用它。

所以我编写了另一个使用containsstable创建sql的类。

答案 1 :(得分:0)

您的第一个解决方案可行,但您应该删除调用criteriaBuilder.isTrue(contains) - 它会将对比与true添加到SQL中,因此在生成的SQL查询结束时得到= 1.

解决方案是将toPredicate方法的返回类型从谓词更改为表达式:

    @Override
    public Expression toPredicate(Root<P_person> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {

        Expression<Boolean> contains = criteriaBuilder.function("CONTAINS", Boolean.class, root.get(P_person_.firstName), criteriaBuilder.literal(name));
        return contains; // return

    }

或者,如果你绝对必须返回谓词(不能选择修改界面),你应该将表达式转换为谓词而不添加副作用 - 使用criteriaBuilder.add方法而不是{{ 1}}应该没问题:

isTrue