I am trying to issue a native query using hibernate 5.0 and spring data jpa.
My orm.xml file is as following:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_2_1.xsd"
version="2.1">
<sql-result-set-mapping name="OAuthToken">
<entity-result entity-class="biocode.fims.entities.OAuthToken">
<field-result name="oAuthTokenId" column="t.oAuthTokenId"/>
<field-result name="token" column="t.token"/>
<field-result name="refreshToken" column="t.refreshToken"/>
<field-result name="ts" column="t.ts"/>
<field-result name="oAuthClient" column="t.clientId"/>
<field-result name="user" column="t.userId"/>
</entity-result>
<entity-result entity-class="biocode.fims.entities.OAuthClient">
<field-result name="clientId" column="cId"/>
<field-result name="clientSecret" column="c.clientSecret"/>
<field-result name="callback" column="c.callback"/>
</entity-result>
<entity-result entity-class="biocode.fims.entities.User">
<field-result name="userId" column="uId"/>
<field-result name="username" column="u.username"/>
<field-result name="password" column="u.password"/>
<field-result name="hasSetPassword" column="u.hasSetPassword"/>
<field-result name="email" column="u.email"/>
<field-result name="institution" column="u.institution"/>
<field-result name="enabled" column="u.enabled"/>
<field-result name="admin" column="u.admin"/>
<field-result name="firstName" column="u.firstName"/>
<field-result name="lastName" column="u.lastName"/>
<field-result name="passwordResetToken" column="u.passwordResetToken"/>
<field-result name="passwordResetExpiration" column="u.passwordResetExpiration"/>
</entity-result>
</sql-result-set-mapping>
</entity-mappings>
I am executing the query as follows:
Code:
return (OAuthToken) em.createNativeQuery("SELECT t.oAuthTokenId, t.token, t.refreshToken, t.ts, t.clientId, t.userId, " +
"c.clientSecret, c.callback, c.clientId as cId, " +
"u.userId as uId, u.username, u.password, u.hasSetPassword, u.email, u.institution, u.enabled, u.admin, u.firstName, " +
"u.lastName, u.passwordResetToken, u.passwordResetExpiration " +
"from oAuthTokens t " +
"inner join users u on t.userId = u.userId " +
"inner join oAuthClients c on c.clientId = t.clientId " +
"where t.refreshToken = ?1 and TIMESTAMPDIFF(SECOND, t.ts, CURRENT_TIMESTAMP) <= ?2", "OAuthToken")
.setParameter(1, refreshToken)
.setParameter(2, expirationInterval)
.getSingleResult();
However I keep getting the exception:
Caused by: java.sql.SQLException: Column 'oAuthCli1_4_1_' not found.
The sql hibernate logs is:
Code:
SELECT
t.oAuthTokenId,
t.token,
t.refreshToken,
t.ts,
t.clientId,
t.userId,
c.clientSecret,
c.callback,
c.clientId as cId,
u.userId as uId,
u.username,
u.password,
u.hasSetPassword,
u.email,
u.institution,
u.enabled,
u.admin,
u.firstName,
u.lastName,
u.passwordResetToken,
u.passwordResetExpiration
from
oAuthTokens t
inner join
users u
on t.userId = u.userId
inner join
oAuthClients c
on c.clientId = t.clientId
where
t.refreshToken = ?
and TIMESTAMPDIFF(SECOND, t.ts, CURRENT_TIMESTAMP) <= ?
Why is hibernate looking for the "oAuthCli1_4_1_" table when parsing the ResultsSet? How can I fix this?