0

I have a criteria, which I'm trying to join to a database view. I'm wondering if it's possible and if so how to do it?

As I understand, createAlias and createCriteria only join to child objects of the root criteria. I read about detachedCriteria, but it seems that those need to be created on database entities, which the view is not.

The HQL equivalent would be something like this

    select * from root_criteria rc where rc.id in
(select view.id from DATABASE_VW view where view.field is not null)

Thanks in advance

4

2 回答 2

1

You can try XML mapping to create an entity class pointing the database view. Each field in the entity will be mapped to the one selected from the view.

Excerpts from the documentation :

  • 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.

  • subselect (optional): maps an immutable and read-only entity to a database subselect. This is useful if you want to have a view instead of a base table.

Below is the sample configuration, modify it accordingly.

 <class name="MappedClassName" mutable="false">
 <subselect>
 select view.id as viewId from DATABASE_VW view    
 </subselect>
 ...
 </class>
于 2013-04-10T06:35:26.513 回答
0

Yes it is possible to use Views in Criteria.

Create View. Create Java Object(Persistent) which will have mapping member variable for every column you are selecting from view. Create hbm.

You can now use the Java Object in the criteria as you can do it for table.

于 2013-04-10T08:09:20.040 回答