You can define a relationship in EclipseLink based on any EclipseLink Expression. There is currently no JPA annotations for this, but you can use a DescriptorCustomizer to add the mapping using the mapping API and setting a selectionCriteria on the mapping.
Other options within JPA would be to query for the results instead of mapping them.
See,
http://en.wikibooks.org/wiki/Java_Persistence/Relationships#Filtering.2C_Complex_Joins
philk wrote:
Hello,
I am working with a legacy database where the warehouse order progress is
calculated depending on many line items are finished. So I have 2 Entities:
WarehouseOrder
LineItem
the orders progress is calculated using the number of LineItems that have
their state field set to anything but 1.
I would have a transient field in WarehouseOrder that holds the progress.
I checked the @PostLoad listeners, but they have no session context so I
can not execute any query. In Hibernate there is the @Formula annotation
that allows you to specify a query for filling in the annotated field.
I tried this:
@SqlResultSetMapping(name = "progress", columns = @ColumnResult(name =
"progress"))
@NamedNativeQuery(name = "progress", query = "select 100 * (select
count(p) from LegacyOrderPosition p, LegacyPickOrder o where p.id=o.id and
p.status!=1) / count(p) as progress", resultSetMapping = "scalar")
But to no avail. Of course I got it wrong somehow, I know :)
Any ideas how this could be solved in EL? Maybe using an embedded Entity?
Thanks for any input,
Phil