eclipselink jpa criteria reference unmapped column

梦想与她 提交于 2019-12-12 01:26:44

问题


when writing jpql queries I can reference not mapped columns with COLUMN() (I know it is eclipselink specific). Is there any way I can reference unmapped column when building criteria using javax.persistence.criteria.CriteriaBuilder, javax.persistence.criteria.Predicate, etc?

The problem I am facing: I have postgresql table with full text search column. I do not want it to be mapped to entity objects but I qould like to use is in queries (and I need also queries using CriteriaBuilder).


Edit: code example

I have a table: X (id integer, name varchar, fts tsvector)

"fts" column is full text search index data. I need entity class without "fts" attribute, like:

@Entity class X {
    @Id Integer id;
    @Column String name;
}

so that fts data is never fetched (for performance), but I need to be able to query that column. I can do this with jpql: SELECT t FROM X t WHERE COLUMN('fts', t) IS NOT NULL;

but how can I reference such column when building criteria like:

Specification<Fts> spec = new Specification<Fts>() {
        @Override
        public Predicate toPredicate(Root<Fts> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
            return builder.notNull(root.<String>get("fts"));
        }
    };

other choice: How to add attribute in entity class that reference table column but is never fetched? I tried adding @Basic(fetchType = FetchType.LAZY) but it does not work and value is fetched upon query...


回答1:


Ok. I managed to solve it this way:

Specification<Fts> spec = new Specification<Fts>() {
        @Override
        public Predicate toPredicate(Root<Fts> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
            JpaCriteriaBuilder cb = (JpaCriteriaBuilder) builder;
            List<String> args = new ArrayList();
            args.add("FTS query");

            javax.persistence.criteria.Expression<Boolean> expr = cb.fromExpression(
              cb.toExpression(
                cb.function("", Boolean.class, cb.fromExpression(((RootImpl) root).getCurrentNode().getField("fts")))
              )
              .sql("? @@ to_tsquery(?)", args)
            );
            // getField() allows to reference column not mapped to Entity

            query.where(expr);

            return null; // or return cb.isTrue(expr); and then not need to set query.where()
        }
    };


来源:https://stackoverflow.com/questions/29509261/eclipselink-jpa-criteria-reference-unmapped-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!