I have a table that maintains rows of products that are for sale (tbl_products
) using PostgreSQL 9.1. There are also several other tables that maintain ratings on the items, comments, etc. We're using JPA/Hibernate for ORM in a Seam application, and have the appropriate entities wired up properly. In an effort to provide better listings of these items, I've created a SQL VIEW
(v_product_summary
) that aggregates some of the basic product data (name, description, price, etc.) with data from the other tables (number of comments, average rating, etc.). This provides a nice concise view of the data, and I've created a corresponding JPA entity object that provides read-only access to the view data.
Everything is working fine with respect to running JPQL queries on either the Product
object (tbl_products
) or the ProductSummary
(v_product_summary
) objects. However, we'd like to provide a richer search experience using Hibernate Search and Lucene. The issue we're running into, though, is how do we query the ProductSummary
objects using Hibernate Search? They're not indexed upon creation, because they're never really "created". They're obtained as read-only objects from the v_product_summary
VIEW
. An index entry is only created on Product
when it's persisted to the database, and not for ProductSummary
since it's never persisted.
Our thought is that we should be able to:
- Persist our
Product
object to the database - Immediately query the corresponding
ProductSummary
object using the product's ID - Manually update the Hibernate Search index for the
ProductSummary
object
Is this possible? Is this even a good idea? I can see there will be a performance impact since we're executing a query for the ProductSummary
object every time a new Product is persisted. However, products are not added to the database at a high volume, so I don't think this will be a huge issue.
We'd really like to find a better, more efficient way of accomplishing this. Can anyone provide any tips or recommendations? If we do go the route of updating the search index manually, is that even doable? Can anyone provide a resource explaining how we can add a single ProductSummary to the index?
Any help you can provide is GREATLY appreciated.