在 Hibernate 中使用数据库视图 [英] Use database Views with Hibernate

查看:29
本文介绍了在 Hibernate 中使用数据库视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库中有一个名为 Samples 的表,它有一个名为 baseline 的列,它是一个布尔变量.我想在 baseline 设置为 1 的行中搜索,所以我在数据库(MySQL)中创建了一个视图.现在,我想查询此视图(名称为 Baselines),而不是查询 Samples 表.

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

我需要在 hibernate cfg 文件中为这个视图添加一个声明吗?另外,我是否需要为此创建另一个名为 Baselines 的 Java 类?

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?

推荐答案

Hibernate 可以像对待任何表一样对待视图.只需根据该视图定义一个实体类(Baselines,如您所说).

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

视图最常见的困难是某些数据库引擎无法处理视图的插入或更新,因此如果您的应用程序试图修改数据,请注意这一点.不知道 MySQL 有没有这个能力.

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.

使用视图的替代方法是使用 Hibernate 过滤器,它们做很多相同的事情,但在 Hibernate 会话级别.

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

这篇关于在 Hibernate 中使用数据库视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆