• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate and views

 
John Eric Hamacher
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cameron:

My company uses database views in many situations. Views don't normally have primary keys. Besides using createSQLQuery(), is there any way I can query views with Hibernate?

Thanks
Eric
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're not updating or creating new rows in the view, you can simply map an entity to it. That's usually the easiest thing to do.

Here's some info from Hibernate.org:


Does Hibernate support database views?

Of course. From the point of view of Hibernate, a view is just like any other table (except that you might not be able to update or insert to a view). You can easily map a class to a view by specifying table="view_name".

In Hibernate3, you may even map an entity class to a SQL query result set using the <subselect> mapping element. This is useful if you are unable to define new views in your database.


Using Hibernate and Database Views
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Views don't normally have primary keys

It is, however, very easy to define a view that has a surrogate key. I'd recommend this to easy the pain of mapping it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic