Java – how to create / invoke SQL views in Hibernate

This is in document hbm. Views created in 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 do I call this view in my method?

Try calling like this

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

It's over

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 other way to invoke SQL view in Hibernate?

In short, is there a way to call the view just like a stored procedure??, Without creating a new entity?

Solution

You can use the DB view as if it were a regular entity table Use @ entity annotation or equivalent XML and any subset of view columns as fields to define entity classes

It is important that you do not change values in entities because the view is read-only

Editor: I don't know how to use views like stored procedures If the purpose of a stored procedure is to query multiple entities implied in a comment, you can:

>Make the view "broad" enough to contain all the necessary attributes of the desired entity > relate to the relevant entity using the foreign key column in the view, and define @ * to * comments for the entity mapped to the view

I'm afraid this won't take you far because you still need to use native SQL or define an entity

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>