views:

29

answers:

1

Hi,

I'm trying to learn JPA. I created a simple entity class. This entity has 3 columns: ID, NAME, NUMBER_A. It's working fine: I can persist, update and delete without problems.

What I'm trying to do is to create a calculated/computed column. I want to retrieve NUMBER_A, use it to calculate a new value and set the calculated column (let's say CALC_COLUMN).

This calculated column will not be persisted. It's only necessary on runtime.

I tried to create a simple double object and it's getter and setter, but when I run the application, it throws an error telling that column CALC_COLUMN doesn't exist because JPA/EclipseLink is trying to use it as a valid database column (Column 'CALC_COLUMN' is either not in any table in the FROM list...).

So, is it possible to do it?

TIA, Bob

+1  A: 

What I'm trying to do is to create a calculated/computed column.

JPA doesn't provide standard support for this. Some providers have specific extensions like Hibernate's @Formula but EclipseLink doesn't have a direct equivalent (although it does offer alternatives, see the linked answer).

This calculated column will not be persisted. It's only necessary on runtime.

Then why are you talking about a column for it? :)

I tried to create a simple double object and it's getter and setter, but when I run the application, it throws an error telling that column CALC_COLUMN doesn't exist because JPA/EclipseLink is trying to use it as a valid database column (Column 'CALC_COLUMN' is either not in any table in the FROM list...).

From your description, I'm not sure you need anything more fancy than an additional getter (without any field) that would return the computed value. Make it @Transient if required. But maybe it was just an example. In that case, check the mentioned link (and the Request for Enhancement).

Pascal Thivent