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

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

问题描述

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

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

解决方案

Hibernate可以像处理任何表一样处理视图。就像你说的那样,定义一个基于该视图的实体类( Baselines )。



视图是某些数据库引擎无法处理视图上的插入或更新,因此请注意,如果您的应用程序尝试修改数据。我不知道MySQL是否可以做到这一点。



使用视图的另一种方法是使用 Hibernate Filters ,它们完成同样的事情,但是在Hibernate会话级别。


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?

解决方案

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.

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

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