Is there a Java library that will allow me to execute JPQL queries against a basic Java Collection, effectively treating the collection as an in-memory database?
I am currently writing custom code for each query variant, which is error prone and hard to maintain. Due to the need to inter-operate with other code, I can't switch from collections to a true in-memory database like HSQLDB.
There are two workarounds I can think of. Because these collections are transient and processed frequently and in parallel, these workarounds seem like they would add too much overhead (but feel free to convince me otherwise). These are:
- Each time I need to run a JPQL query against a collection, stand up a temporary in-memory database, populate it from the collection, and run the query against the database. - The overhead I believe this would introduce isn't justified by the problem I'm trying to solve.
- Like #1 but keep the in-memory database around to cut down on overhead. - The transient nature of the collections means this isn't much better than #1 if I keep a database per collection. The parallel nature of these queries would make it non-trivial to reuse the same in-memory database instance across different collection queries.
Edit:
Here is the exact use case:
We are using Drools to perform validation on data as it changes on our server. Our validation rules necessitate a large amount of supporting data be loaded into the knowledge session.
For simplicity, we tried standing up a knowledge session each time it was needed so that we did not need to worry about stale data, however the time performance of this was unacceptable.
We're now switching to a knowledge session that we keep in sync with the database. The knowledge session is initially populated the first time it is accessed. A JPA callback listener records entity additions into a collection, and these entities are incorporated into the knowledge session at the next opportunity if they are applicable.
The wrinkle in this is that the design has to allow for rule changes and additions at a later date on a production system; rule changes may change the data needed for validation so the data loading mechanism has to be equally flexible.
From our first attempt we already have a mechanism in place whereby the rules define, via JPQL statements, what data should be loaded and we execute these JPQL statements to initially populate the knowledge session.
I am now leveraging these same JPQL statements to determine which entities from the JPA callback listener should be added to the knowledge session. To do so, I am writing code to interpret this JPQL and apply the where clause filters to the entities coming from the JPA callback listener. I would prefer to use a third-party library to do this for me.
If you think we should be tackling this completely differently, feel free to suggest a change. The design is convoluted, but unfortunately much of that is driven by customer requirements and can't be avoided.