Hello.
I have a database schema with one central transactions table and multiple views of that table. For simplicity, let's call the transactions table "TRANSACTIONS" and the views would be simple filters like "SUCCESSFUL_TRANSACTIONS" and "TRANSACTIONS_THAT_FAILED_WITH_ERROR_30" or something. All of these views have the same columns as the main TRANSACTIONS table; they're really just (complex) filters.
The application I am writing/redesigning takes user-inputted control files to choose which view it needs to use. The views somewhat independent of this program and their logic is best defined and kept on the database side. Further, the application doesn't necessarily know what or how many views there are, it just gets the name of the view.
Back in the bad old days when I composed by own SQL (last week), I could use a concept like this:
Code:
select * from ${users_view} where blah=blahblah
where users_view would get popped in. Then I'd go about the arduous task of manually mapping the fields out.
Now that I have Hibernate in my life, everything is great, mapping is easy, and the winter rainbows are shining. However, I'm not sure how to switch between views now.
I have an annotated entity, let's say it looks like this:
Code:
@Entity @Table(name="TRANSACTIONS")
public class TransactionEntry implements Serializable {
/* Properties for ID, timestamps, return codes, etc
*/
}
Is there some what I can programmatically override the Table name..?
After some Googling on this issue, I saw an interesting suggestion to use @MappedSuperclass for the main TransactionEntry and subclasses for the different views. However, as views are added from time to time as needed, it would result in additional maintenance to create a new subclass for each one. Obviously, I'd like to avoid this.
I've also been thinking about hijacking Criteria output to feed it back into native SQL with the appropriate view inserted, but this is philosophically undesirable and negates most of the benefits of Hibernate.
In short, I want to share the same TransactionEntry Entity class between multiple views.
Am I overlooking anything obvious? Is there some way to explicitly specify which view I want to select from when slurping up Entities?
Hopefully I've explained my issue adequately.