处理hibernate实体上的数据库视图的优雅方法? [英] Elegant ways to handle database views on hibernate entities?

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

问题描述

我使用Hibernate的一个主要原因是它提供了切换到另一个数据库的灵活性,而不必重写任何代码。



但是直到现在我还没有找到一个好的方法来定义我的hibernate实体匹配的表上的其他视图;我仍然使用简单的SQL脚本。有没有更优雅的方式来定义由hibernate管理的表的视图?



理想情况下,我想使用HQL或其他通用方法来完成这项工作,这样我就不必担心我的SQL脚本与其他类型的数据库。



如果有办法做到这一点,第二个问题就是从这些视图获得'合成'只读实例,这应该使它更容易



看起来很像好像我没有使问题清楚,所以这里是我想做的:我想编写的代码独立于使用的数据库。因为我使用hibernate,我只需要更改方言配置文件,然后可以使用另一个DBMS。



问题:如何在我的hibernate实体上创建视图,而不需要依赖于特定的SQL方言便携),甚至HQL?如果可能,我可以使用HQL来查询这些视图,即创建只读聚合实体?有什么额外的hibernate插件来帮助我吗?到目前为止还没有发现任何东西...: - /

解决方案

Hibernate不会自动为你创建视图,方言只支持底层数据库的数据定义语言(DDL)的有限子集。基本上,它支持足够的DDL来生成一个工作模式,但不足以处理创建额外对象,如视图。



Hibernate允许您自己在XML映射文件中创建(和删除)其他数据库对象,并且这些对象可以限定为特定的方言。例如,我可以有这样的映射:

 < hibernate-mapping> 
< class name ='com.mycompany.myproduct.Customer'table ='tbl_customer'>
< id name ='id'column ='customer_id'>
< generator class ='native'/>
< / id>
< property name ='name'length = '50'unique ='true'not-null ='true'/>
< / class>

< database-object>
< create>创建或替换视图read_only_cust ...< / create>
< drop> drop view read_only_cust< / drop>
< dialect-scope name ='org.hibernate.dialect.Oracle9Dialect'/>
< / database-object>
< / hibernate-mapping>

您可以通过添加更多的数据库对象部分来创建任何额外的视图。你必须为你想要支持的每个数据库自己编写SQL(DDL),但是由于它们的范围是方言,Hibernate将只为在模式导出时选择的方言执行SQL。


One of the main reasons I use Hibernate is that it provides the flexibility to switch to another database without having to rewrite any code.

But until now I did not figure out a good way to define additional views on the tables to which my hibernate entities are matched; I am still using simple SQL scripts for that. Is there a more elegant way to define views on tables managed by hibernate?

Ideally I would like to use HQL or another generic method to do the job, so that I don't have to worry about my SQL scripts being incompatible with other kinds of databases.

If there's a way to do that, a second issue would then be to get 'synthetic' read-only instances from these views, which should make it much easier to feed the aggregated data into a UI.

EDIT:

It seems as if I didn't make the problem clear enough, so here's what i am trying to do: I want to write code that is independent of the used database. Since I use hibernate, I would just have to change the dialect configuration file and could then use another DBMS.

Question: how to create views on my hibernate entities without relying on a specific SQL dialect (to keep everything portable), or even HQL? And if that's possible, can I use HQL to also query these views, i.e. to create read-only aggregate entities? Is there any additional hibernate plug-in to help me with that? Haven't found anything so far... :-/

解决方案

Hibernate will not automatically create the views for you, as each dialect supports only a limited subset of the data-definition language (DDL) of the underlying database. Basically, it supports enough DDL to generate a working schema, but not enough to handle creation of "extra" objects like views.

All is not lost, though. Hibernate does give you the ability to create (and drop) additional database objects yourself in the XML mapping files, and those objects can be scoped to a particular dialect. For example, I could have a mapping like this:

<hibernate-mapping>
  <class name='com.mycompany.myproduct.Customer' table='tbl_customer'>
    <id name='id' column='customer_id'>
      <generator class='native'/>
    </id>
    <property name='name' length='50' unique='true' not-null='true' />
  </class>

  <database-object>
    <create>create or replace view read_only_cust...</create>
    <drop>drop view read_only_cust</drop>
    <dialect-scope name='org.hibernate.dialect.Oracle9Dialect' />
  </database-object>
</hibernate-mapping>

You're free to create whatever additional views you want by adding more "database-object" sections. You have to write the SQL (DDL) yourself for each database you want to support, but since they're scoped to the dialect, Hibernate will only execute the SQL for the dialect chosen at schema export time.

这篇关于处理hibernate实体上的数据库视图的优雅方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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