Sleep View

How to map a view to multiple entiites using hibernate?

Regards, chaitu

+8
hibernate
source share
3 answers

See section 5.1.3 "Class" in the Hibernate documentation immediately before the "Id" section:

There is no difference between the view and the base table for Hibernate mapping. This is transparent at the database level, although some DBMSs do not support views properly, especially with updates. Sometimes you want to use a view, but you cannot create it in a database (i.e. With an inherited schema).

http://docs.jboss.org/hibernate/core/3.5/reference/en/html/mapping.html#mapping-declaration-class

There is also an example of how to do this using XML.

+8
source share

Subselect is your natural choice. Here is a working example: let's say we have a view named "view1" in the DBMS. You don't need anything, although if the view is not updatable, using @Immutable would be nice for performance issues. Note that you must have an identifier column in your class and in the view

@Entity @Subselect("select * from view1") public class EventView { @Id @GeneratedValue private int id; 
+5
source share

you can use @subselect annotation

here is an example of official documentation:

 @Entity @Subselect("select item.name, max(bid.amount), count(*) " + "from item " + "join bid on bid.item_id = item.id " + "group by item.name") @Synchronize( {"item", "bid"} ) //tables impacted public class Summary { @Id public String getId() { return id; } ... } 
+4
source share

All Articles