Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do I map to a database view

 
Howard Ralston
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy folks,

I have an existing app with an existing database. We have a report that is based upon a view with info from various tables. I am trying to generate the code to get the same report using Hibernate. The problem is that I cannot find any examples of how to do this. The docs don't really address the use of a view, only persistent classes based upon tables. Does anyone have a real example of the mapping class element and a snippet of code using the class based upon a view?

Thanks,

Howard
 
Scott Johnson
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a workaround, you could create a real table that matches the view, generate the mapping of it, then change the mapping's tablename to point to the view.

I've never mapped a view, but I would expect it's mapping would be the same as a real table.
[ January 12, 2006: Message edited by: Scott Johnson ]
 
Howard Ralston
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply Scott. I just tried a variation and it worked fine. In my mapping file I created and id element with the name of "report_id" and the table name was the view "v_summary_report". All I did was modify the view to include "1 as report_view" to add that to the view. After that it seemed to work fine.

Howard
 
Howard Ralston
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more followup:
When I tried to get a collection of items from the view I ran into a problem. Hibernate read 1 as the primary key and iterated the same row over again and again. I had to change the view to make report_id a unique composite of two rows. Then Hibernate would iterate through the entire resultset as needed. I did not use composite-id in the mapping, just left it as report_id.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic