JPA - Setting entity class property from calculated column?

后端 未结 2 1429
有刺的猬
有刺的猬 2021-01-01 03:00

I\'m just getting to grips with JPA in a simple Java web app running on Glassfish 3 (Persistence provider is EclipseLink). So far, I\'m really liking it (bugs in netbeans/gl

2条回答
  •  灰色年华
    2021-01-01 03:38

    There are probably no good ways to do it, only manually:

    Object[] r = (Object[]) em.createNativeQuery(
        "select id,title,shorttitle,datestamp,body,true as published, ts_headline(body,q,'ShortWord=0') as headline, type from articles,to_tsquery('english',?) as q where idxfti @@ q order by ts_rank(idxfti,q) desc","ArticleWithHeadline")
        .setParameter(...).getSingleResult();
    
    Article a = (Article) r[0];
    a.setHeadline((String) r[1]);
    

    -

    @Entity
    @SqlResultSetMapping(
        name = "ArticleWithHeadline",
        entities = @EntityResult(entityClass = Article.class),
        columns = @ColumnResult(name = "HEADLINE"))
    public class Article {
        @Transient
        private String headline;
        ...
    }
    

提交回复
热议问题