Basic Hibernate issues using and SQL query
I have an java Object Person
with 3 properties firstname
, lastname
and username
.
I have an Oracle stored procedure returning a result set with the 3 columns. All works fine for that.
Now I have another stored procedure that will only return firstname and lastname but not username.
I get the following error:
Could not read column value from result set username
Hibernate tries to fetch the username property from the resultset. If I remove the property username, then it works.
My config:
<sql-query name="normalise" callable="true" >
<return alias="val" class="com.nbfg.sipc.model.Person">
<return-property name="firstname" column="FIRST_NAME"/>
<return-property name="lastname" column="LASTNAME_NAME"/>
</return>
{call SCHSIPC.PKG_SIPC_APP.PRC_SIPC_NORMALISE_RS(?, ?, ?, ?, ?) }
</sql-query>
My Pojo (no annotation)
@Entity
public class Person {
private String firstname;
private String lastname;
private String username;
...
The call:
private Value call(String app, String cat, String col, String valeure, String query) {
try {
Query q = getSessionFactory().openStatelessSession().getNamedQuery(query);
开发者_StackOverflow社区 q.setString(0, app).setString(1, cat).setString(2, col).setString(3, valeure);
return (Person) q.list().get(0);
} catch (org.hibernate.QueryTimeoutException ex) {
throw new IllegalArgumentException(ex.getCause().getMessage());
}
}
It all works fine if I remove the property username
from my Pojo. I can I reuse the same PoJo?
Thank you
Thank you for the code. I am not 100% certain on this, but I believe that with Hibernate SQL queries must return all fields of the object you want the query to instantiate.
An alternative solution you may want to try is to rely on HQL and instantiate the object as needed. For example, SELECT new Person(firstName, lastName) from Person p where p.username = ... This actually allows you to use any type (with fields of matching types) in your queries.
This would solve your immediate problem, but I am not sure how to address the use of stored procedures in this scenario. Hope this helps.
Well I guest if we are saying that there is no way to tell Hibernate to read only certain Columns in a ResultSet return by a StoreProcedur and that I need to create a separate POJO for each Type of result set, then this the answer.
If this right?
精彩评论