views:

152

answers:

3

Hello

I have an java Object ‘Person’ with 3 properties firstname,lastname and username. I have an Oracle Store procedure returning a Resultset with the 3 column. All works fine for that. Now I have another StoreProcedure 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);
        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 Proprety username from my Pojo. I can I reuse the same PoJo?

Thank you

A: 

Some code for what you are trying to do would help to understand your question.

mttr
I haded some config sample. Thanks in advance
Aerosteak
This should be posted as comment, this is not an answer.
Pascal Thivent
True, but I can only flag the original question. I don't have the option to comment
mttr
+2  A: 

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.

mttr
If you are working with a simplistic stored proc that just does a simple select and returns the table schema (like you would with .NET), I would dispense with the stored procedure entirely. Let hibernate handle the associations, and generate the proper SQL.
RMorrisey
A: 

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?

Aerosteak
As far as I know that's correct. But read the Hibernate documentation... The question then becomes, do you really need to rely on the stored procedures? You definitely don't want to have one POJO per query, all more or less representing the same object. Have your POJOs, let them define useful queries in HQL and let for eg a SLSB offer clients to execute these queries. Clean and simple. Unless, you have to have the stored procs.
mttr