JPA - defining a class property of an object from a computed column?

I am just dealing with JPA in a simple Java web application running on Glassfish 3 (the Persistence provider is EclipseLink). So far I really liked it (errors regarding netbeans / glassfish aside), but there is something that I want to be able to do this, I'm not sure how to do it.

I have an entity class (article) that is mapped to a database table (article). I am trying to make a query in a database that returns a computed column, but I cannot figure out how to set the property of the Article class so that the property is populated with the column value when the query is called.

If I make a regular request "select id, title, body from article", I get a list of article objects in order, with id, title and body properties filled. It works great.

However, if I do the following:

Query q = 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",Article.class);

(this is a full-text search using tsearch2 in Postgres - this is a function specific to db, so I use NativeQuery)

You can see that I am getting a calculated column called a header. How to add a title property to the Article class so that it is populated with this query?

So far, I have been trying to set it to @Transient, but that just ends up being zero.

+5
source share
2 answers

There are probably no good ways to do this, 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;
    ...
}
+7

AFAIK, JPA . Hibernate Formula, EclipseLink . Re: (@Formula of Hibernate), :

( ), , .

EclipseLink , .

SQL CRUD , DescriptorQueryManager.

VIEW , .

get/set .

, DescriptorEventListener .

, JPA.

+2

All Articles