brancz
brancz

Reputation: 451

JPA Lazy Fetch Custom Query

I am using JPA/JFreeChart to display data I collected with a microcontroller, however, I measure 14 sensors every 10 seconds. I have been measuring for over 2 months and I have over 7000000 sets of data.

Now to my actual problem, since I don't want to load 7000000 rows every time I start my program, I only want to use average values by minutes/hours. I have thought of using a NamedQuery however I don't know how to keep the relationship within it and make JPA use it since up until now the loading of the data has been done by JPA itself. Maybe I can just solve this by adding more annotations to this?

@OneToMany(mappedBy="sensor")
@OrderBy("timestamp ASC")
public List<Value> getValues() {
    return this.values;
}

Thanks in advance!

Best Regards

Upvotes: 1

Views: 588

Answers (1)

Chris
Chris

Reputation: 21145

Straight JPA does not allow filtering results, since this means that the entity's relationship no longer reflects exactly what is in the database, and it would have to standardize behavior on what is done when adding an entity to the relationship that isn't in the collection, but already exists in the database.

The easiest way for this mapping though would be to mark the attribute as @Transient. You can then use the get method to read the values from the database using when needed, and cache them in the entity if you want.

Many providers do allow adding filters to the queries used to bring in mappings, for instance EclipseLink allows setting @AdditionalCriteria on the mapping as described here: http://wiki.eclipse.org/EclipseLink/Development/AdditionalCriteria Or you can modify the mapping directly as shown here: http://wiki.eclipse.org/EclipseLink/Examples/JPA/MappingSelectionCriteria

Upvotes: 1

Related Questions