I never used - and liked - so much database views, but after last experience I had to change my mind. Now I think that database views can be very helpful in application development, and system integration.
Our last application is a sort of web based Console to an heterogeneous systems, in which great part of the job is done by the Console itself.
The Console is also responsible to generate reports on its behaviour and external systems states. Systems communicate thru sockets and RPC calls and share data thru database tables: this can be implemented by database links and other tricky stuff that most of dba knows very well.
Systems receiving informations from users write data to some tables with many fields, and those tables are visible to our Console application.
We used Hibernate as persistent layer since the start of the development and it has been a great experience. But, as far as I know, Hibernate does not support natively database views. To solve the problem, we simply mapped our objects to handly created views, and used them as they were common tables. Using hibernate query language we filter and aggregate informations according system administrator needs. It worked greatly.
A big advantage on using database views, is that you can use proprietary DB functions to manipulate data to be used, whitout being locked to the specific database. For example, we needed a way to group by date, but in this table we had timestamps:
CREATE TABLE ORIGINAL_TABLE ( name varchar, surname varchar, reg_time timestamp ); CREATE VIEW OUR_VIEW AS SELECT name, surname, TRUNC(reg_time) AS reg_date, reg_time FROM ORIGINAL_TABLE;
In this way we can create reports thru Hibernate using OUR_VIEW and grouping by reg_date without having to call or reference the trunc function in our source code. Trunc is supported by hibernate natively - it is translated to various db dialects - but this approach could be also more useful to avoid calling proprietary db functions, in particular when the db function is not supported by hibernate. Let's suppose that trunc was not supported by hibernate, in this case the only reference to trunc is inside the DDL of the view; nothing in our code. Using another DB we have just to create the view using the appropriate function to truncate dates, and no code intervention will be needed.
The only problem is that we are accustomed to let hibernate generate the schema for the tables using the schema generator. But, as we mapped our views as tables, it also tries to generate the DDL for our views as "CREATE TABE ...", so that we have to manually remove those DDL and replace them with "CREATE VIEW ...". I consider this a problem, as doing that manually could be the occasion for possible errors.
It would be nice if hibernate was supporting views. Unfortunately, AFAIK, it does not do that. (If I am wrong, I would be happy to have some advice in comments of this post.)
I think it would be trivial to implement this: we've seen that Tables and Views works in the same manner, for hibernate (at least when used in read-only): it's just only a matter of the schema-generation, then. A view could be mapped with XML as in following:
<class name="ClassName" view="VIEW_NAME"> <view-hql>HQL query to generate the view</view-hql> <property name="propertyName" column="VIEW_COLUMN" type="..." /> ... </class>
In this way, the schema generator should only translate the hql query to specific sql and append "CREATE VIEW <view-name> AS ..." in front of the generated query.
Maybe it could be even be better to let the programmer to write a raw SQL, so that we can map views using proprietary function. The problem, in this case, is that we are going to lock our mapping XMLs to a specific vendor. But it's not that problem comparing to the flexibility gained.
I know, nothing is easy as it intitially seems. There could be some reason why hibernate does not implement this, but I didn't find this reason until now.
2 Responses to “Hibernate and Database Views”
Leave a Reply
Search
Calendar
| M | T | W | T | F | S | S |
|---|---|---|---|---|---|---|
| « Jun | Aug » | |||||
| 1 | 2 | 3 | ||||
| 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 25 | 26 | 27 | 28 | 29 | 30 | 31 |
Archives
Categories
- Android (3)
- Apple (26)
- Books (7)
- Eclipse (14)
- Errors (3)
- Firefox (7)
- Git (2)
- Hardware (16)
- Horror Code (8)
- Internet (18)
- Java (98)
- JavaScript (9)
- Life, universe and everything (45)
- Lifehacks (25)
- Linux (50)
- Opinions (25)
- OSX (4)
- Python (1)
- Software (27)
- Speeches and Conferences (8)
- Unix (3)
- Web (21)
- Windows (19)
Tag Cloud
Android apple architecture Bash colors configuration CSS Development Düsseldorf Eclipse germany Git Google Hardware hdr How-To Java JAXB job junit Karmic Linux MacBook music night Open Source Opinion oracle OSX patterns Pitfalls Practices Resume Security Software Suspend TDD Testing tip tonemapped Tricks Ubuntu video Web XML
WP Cumulus Flash tag cloud by Roy Tanck and Luke Morton requires Flash Player 9 or better.
Blog License
Blogs I like
Books on the desk
Friends' Blogs
- Antonio Terreno & Valter Bernardini
- Bruno Bossola
- Daniele Galluccio
- Domenico Ventura
- Ed Schepis
- Fabrizio Gianneschi
- Luca Grulla
- Luigi Zanderighi
- Marcello Teodori
- Mida Boghetich
- Muralidharan Chandrasekaran
- Piero Ricca
- Renzo Borgatti
- Simone Bordet
- Simone Bruno
- Uberto Barbini
- Valvolog
- Webtide blogs (Greg Wilkins & Jan Bartel)
Links




















I think it is very good idea.
I also suggest next logic:
SQL query to generate the view
SQL query to generate the view
How to use bot Table & view for the same class?
I want to get some data from a view & save it into the Table specified in Class Tag