Use database Views with Hibernate

2019-02-10 16:02发布

I have a table named Samples in my DB,and it has a column named baseline which is a boolean variable. I wanted to search among the rows which have baseline set as 1, so I created a view in the DB (MySQL). Now, instead of querying the Samples table, I want to query this view (names as Baselines).

Do I need to add a declaration for this view in the hibernate cfg file? Also, do I need to create another Java class named Baselines for this?

2条回答
甜甜的少女心
2楼-- · 2019-02-10 16:10

Hibernate can treat views like it does any table. Just define an entity class based on that view (Baselines, as you say).

The most common difficulty with views is that some database engines can't handle inserts or updates on views, so be aware of that if your application tries to modify the data. I don't know if MySQL is capable of this.

An alternative to using a view is to use Hibernate Filters, which do much the same thing, but at the Hibernate session level.

查看更多
Fickle 薄情
3楼-- · 2019-02-10 16:10

I have done something similar between NHiberNate and SQL Server.

I treated the view the same as the normal tables.

查看更多
登录 后发表回答