如何在Hibernate中创建/调用SQL视图 [英] How to create/call a sql view in Hibernate

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

问题描述

以下是在 document.hbm.xml中创建的视图

 <数据库对象> 
< create><![CDATA [CREATE VIEW docView
AS
SELECT * from document;
GO]]>< / create>
< drop> DROP VIEW docView< / drop>
< dialect-scope name ='org.hibernate.dialect.SQLServerDialect'/>
< / database-object>

现在如何在我的方法中调用这个视图



试过像这样调用

  Session session = sessFactory.openSession(); 
Query query = session.createSQLQuery(docView);
列表<?> list = query.list();

结束于

 引起:java.sql.SQLException:过程'docView'的请求失败,因为'docView'是一个视图对象。 
at net.sourceforge.jtds.jdbc.SQLDiagnostic.addDiagnostic(SQLDiagnostic.java:368)
at net.sourceforge.jtds.jdbc.TdsCore.tdsErrorToken(TdsCore.java:2820)
at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2258)
at net.sourceforge.jtds.jdbc.TdsCore.getMoreResults(TdsCore.java:632)

$ p

$ p

任何想法或任何其他方法可以在hibernate中调用sql视图吗?有没有什么办法可以调用 view 就像存储过程一样,不需要创建新的实体

您可以像处理常规实体表一样使用数据库视图。定义一个实体类,或者使用@Entity注解或者等价的XML,以及视图的列的任意子集作为字段。



重要的一点是,您不应该更改实体中的值,因为视图是只读的。

编辑:我不知道像存储过程一样使用视图的方法。如果您的存储过程的目的是针对您的评论中隐含的多个实体进行查询,那么您可以:


  • 使视图'广泛'足以包含所需实体的所有必需属性
  • 与在视图中使用外键列的相关实体相关,并且常规 @ * To * 标注映射到视图的实体。



恐怕这不会让你走得很远,因为你仍然必须使用本机SQL或定义实体。


Here is the view created in document.hbm.xml

<database-object>
    <create><![CDATA[CREATE VIEW docView
     AS
     SELECT * from document;
     GO]]></create>
    <drop>DROP VIEW docView</drop>
    <dialect-scope name='org.hibernate.dialect.SQLServerDialect' />
</database-object> 

Now how to call this view in my method

Tried calling like this

Session session = sessFactory.openSession();
Query query = session.createSQLQuery("docView");
List<?> list = query.list();

Ended up with

Caused by: java.sql.SQLException: The request for procedure 'docView' failed because 'docView' is a view object.
at net.sourceforge.jtds.jdbc.SQLDiagnostic.addDiagnostic(SQLDiagnostic.java:368)
at net.sourceforge.jtds.jdbc.TdsCore.tdsErrorToken(TdsCore.java:2820)
at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2258)
at net.sourceforge.jtds.jdbc.TdsCore.getMoreResults(TdsCore.java:632)

Any Idea or any otherway to call sql view in hibernate?

In short is there a way like Is there any way to call view just like a stored procedure??, without creating a new entity??

解决方案

You can work with a DB view as if it were a regular entity table. Define an entity class, either with the @Entity annotation or an equivalent XML and an arbitrary subset of the view's columns as fields.

An important point is that you should not change the values in the entity, as the view is read-only.

EDIT: I am not aware of a way to use a view like a stored procedure. If the purpose of your stored procedure is querying over multiple entities as implied in your comment, you could either:

  • make the view 'broad' enough to contain all the necessary attributes of the needed entities
  • relate to the relevant entities using foreign key columns in the view and regular @*To* annotations for the entity that is mapped to the view.

I am afraid this does not bring you very far, since you still have to either use native SQL or define an entity.

这篇关于如何在Hibernate中创建/调用SQL视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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