JPA - Setting entity class property from calculated column?

Posted by growse on Stack Overflow See other posts from Stack Overflow or by growse
Published on 2010-05-28T09:20:53Z Indexed on 2010/05/28 14:21 UTC
Read the original article Hit count: 390

Filed under:
|
|
|

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/glassfish interaction aside) but there's a thing that I want to be able to do that I'm not sure how to do.

I've got an entity class (Article) that's mapped to a database table (article). I'm trying to do a query on the database that returns a calculated column, but I can't figure out how to set up a property of the Article class so that the property gets filled by the column value when I call the query.

If I do a regular "select id,title,body from article" query, I get a list of Article objects fine, with the id, title and body properties filled. This works fine.

However, if I do the below:

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 fulltext search using tsearch2 on Postgres - it's a db-specific function, so I'm using a NativeQuery)

You can see I'm fetching a calculated column, called headline. How do I add a headline property to my Article class so that it gets populated by this query?

So far, I've tried setting it to be @Transient, but that just ends up with it being null all the time.

© Stack Overflow or respective owner

Related posts about java

Related posts about postgresql