user569125
user569125

Reputation: 1463

view with hibernate

How to map a view with multiple entiites using Hibernate?

Regards, chaitu

Upvotes: 9

Views: 16758

Answers (3)

morteza khosravi
morteza khosravi

Reputation: 1619

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

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

Upvotes: 6

user538167
user538167

Reputation:

you can use a @subselect annotation

here are 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; }
    ...
}

Upvotes: 4

jpkroehling
jpkroehling

Reputation: 14061

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

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

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

There's also an example on how to do that using XML.

Upvotes: 8

Related Questions