Java 如何在 Hibernate 中创建/调用 sql 视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21255016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 07:41:56  来源:igfitidea点击:

How to create/call a sql view in Hibernate

javasqlhibernateorm

提问by Suresh Atta

Here is the view created in document.hbm.xml

这是创建的视图 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?

任何想法或任何其他方式在休眠中调用 sql 视图?

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

简而言之,有没有办法像view存储过程一样调用,不创建新实体??

回答by Pratik Shelar

Hibernate view is not a named query. You can create the view in you DB and then create the entity pojos with the view in mind. Hibernate will treat these entities as mapped to a view and then you can perform you operation as you normally do for a hibernate entity.

Hibernate 视图不是命名查询。您可以在您的数据库中创建视图,然后在考虑视图的情况下创建实体 pojo。Hibernate 会将这些实体视为映射到视图,然后您可以像通常对 Hibernate 实体所做的那样执行操作。

  1. You are writing a create DDL query to create the view which will only be called if the hbm2ddl property is set correctly.
  2. Create view is a DDl query and it does not return a list of values. You cannot call the create view query as if its a named sql query
  3. Once the view is created you can write your named sql queries which can retrieve data from the view. For that all you need is a POJO which maps to the view and the named query to get the data.
  1. 您正在编写一个 create DDL 查询来创建视图,只有在 hbm2ddl 属性设置正确时才会调用该视图。
  2. 创建视图是一个 DDl 查询,它不返回值列表。您不能像调用已命名的 sql 查询一样调用创建视图查询
  3. 创建视图后,您可以编写可以从视图中检索数据的命名 sql 查询。为此,您只需要一个 POJO,它映射到视图和命名查询以获取数据。

回答by fujy

Create an entity to map it to your view, then use it for querying your view

创建一个实体以将其映射到您的视图,然后使用它来查询您的视图

@Entity
@Table(name = "docView")
public class DocView {

    // Put all fields that you use in your view
    documentField1;
    documentField2;
    .
    .
}

Then you could make your query like this:

然后你可以像这样进行查询:

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

回答by kostja

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.

您可以像处理常规实体表一样使用 DB 视图。定义一个实体类,使用 @Entity 注释或等效的 XML 和视图列的任意子集作为字段。

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.
  • 使视图足够“广泛”以包含所需实体的所有必要属性
  • 使用视图中的外键列和@*To*映射到视图的实体的常规注释与相关实体相关联。

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

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