JPA - How to select from a database function?
<p>I have the following SQL query in T-SQL:</p>

<pre><code>SELECT a.ReportDate,
    a.Value,
    a.Quantity,
    a.ID,
    a.Code
FROM AQF.fCalc(@data) a
</code></pre>

<p><code>@data</code> is a <code>varbinary</code>. I'm trying to replicate this query using JPA. (I'm actually using Spring Data JPA 1.10.2.) I have the following defined in <code>MyRepository</code>:</p>

<pre><code>@Query("SELECT a "
        + "FROM function('AQF.fCalc', :data) a "
       )
List&lt;MyClass&gt; getCalcData(@Param("data") byte[] data);
</code></pre>

<p><code>MyClass</code> is as follows:</p>

<pre><code>@Entity
@Data // using lombok to create setters, getters, etc.
public class MyClass {

    @Id
    private Long id;

    private Date reportDate;
    private BigDecimal value;
    private BigDecimal quantity;
    private String code;

}
</code></pre>

<p>When I call getCalcData, I receive the following error: </p>

<pre><code>antlr.NoViableAltException: unexpected token: function
at org.hibernate.hql.internal.antlr.HqlBaseParser.fromRange(HqlBaseParser.java:1501) [hibernate-core-5.0.9.Final.jar:5.0.9.Final]...
</code></pre>

<p>How can I call the function AQF.fCalc (and pass in the byte array) using JPA?</p>

<p><strong>Update 1</strong></p>

<p>I've now tried using a native query too, but now I get an error stating:</p>

<pre><code>java.sql.SQLException: An error occurred while getting new row from user defined Table Valued Function : 
System.Xml.XmlException: Unexpected end of file has occurred. The following elements are not closed: Data, Report. Line 24, position 804.
</code></pre>

<p>Here's the test code for the native query in <code>MyRepository</code>:</p>

<pre><code>@Query(value = "SELECT a.code "
        + "FROM AQF.fCalc(?1) a",
       nativeQuery = true)
List&lt;String&gt; getCalcData(byte[] data);
</code></pre>

<p><strong>Update 2</strong></p>

<p>I was able to resolve the error noted in "Update 1" above. The byte array of data in the <code>getCalcData</code> was corrupted. Once I resolved that issue, the error no longer appeared. So, now I can run as a native query. However, I would still like to know how to use JPQL to make this function call.</p>