-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Using Stored Procedures to load entities
PostPosted: Thu Mar 03, 2011 11:06 am 
Newbie

Joined: Thu Mar 03, 2011 10:04 am
Posts: 1
Hi all,

I'm trying to convert one of our applications to Hibernate. One of the problems is that all DB access needs to be done via Stored Procedures, so I fear I'm diving in at the deep end here.

Anyway I'm trying to set up the custom SQL for loading an entity. I have a Stored Proc which takes the primary key for the entity, and returns a resultset with the other columns.

My mapping file looks like this:
Code:
...
     <class name="PricingScheduleImpl" table="pricingsched" lazy="true">
        <id name="pricingSchedId" column="pricingsched_id"/>
        <property name="currency" length="3" not-null="true"/>
        <property name="voiceauth_discfixed" precision="18" scale="3" not-null="true"/>
        <property name="voiceauth_discpct" type="float" not-null="true"/>
       
        <loader query-ref="pricingsched_details_list_sp"/>
    </class>

    <sql-query name="pricingsched_details_list_sp">
        <return alias="ps" class="PricingScheduleImpl"/>
        { call pricingsched_details_list(?) }
    </sql-query>
...


And I have some Java which goes:
Code:
        Session session = sessionFactory.openSession();
        PricingSchedule ps = (PricingSchedule)session.load(PricingScheduleImpl.class,
                                                           new Integer(3));




But I end up with an exception:
Code:
15:04:34,153  INFO main org.hibernate.event.def.DefaultLoadEventListener:111 - Error performing load command
org.hibernate.exception.SQLGrammarException: could not execute query
   at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
   at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
   at org.hibernate.loader.Loader.doList(Loader.java:2220)
   at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2108)
   at org.hibernate.loader.Loader.list(Loader.java:2103)
   at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:289)
   at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1696)
   at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:142)
   at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:152)
   at org.hibernate.persister.entity.NamedQueryLoader.load(NamedQueryLoader.java:57)
   at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3039)
   at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:399)
   at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:375)
   at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:139)
   at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:98)
   at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:879)
   at org.hibernate.impl.SessionImpl.immediateLoad(SessionImpl.java:837)
   at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:117)
   at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:150)
   at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:150)
   at com.voicecommerce.billing.dao.impl.PricingScheduleImpl$$EnhancerByCGLIB$$a02100c5.toString(<generated>)
   at java.lang.String.valueOf(String.java:2826)
   at java.lang.StringBuilder.append(StringBuilder.java:115)
   at com.voicecommerce.billing.PricingScheduleTest.main(PricingScheduleTest.java:25)
Caused by: java.sql.SQLException: S0022: Invalid column name 'pricingsched_id'.
   at com.sybase.jdbc3.jdbc.ErrorMessage.raiseError(Unknown Source)
   at com.sybase.jdbc3.tds.TdsResultSet.findColumnByLabel(Unknown Source)
   at com.sybase.jdbc3.jdbc.SybResultSet.findColumn(Unknown Source)
   at com.sybase.jdbc3.jdbc.SybResultSet.getInt(Unknown Source)
   at org.apache.commons.dbcp.DelegatingResultSet.getInt(DelegatingResultSet.java:275)
   at org.apache.commons.dbcp.DelegatingResultSet.getInt(DelegatingResultSet.java:275)
   at org.hibernate.type.IntegerType.get(IntegerType.java:28)
   at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:163)
   at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:154)
   at org.hibernate.loader.Loader.getKeyFromResultSet(Loader.java:1097)
   at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:565)
   at org.hibernate.loader.Loader.doQuery(Loader.java:701)
   at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
   at org.hibernate.loader.Loader.doList(Loader.java:2217)
   ... 21 more


I think the problem is that the Stored Proc in question doesn't return the primary key column (since it was passed in). Any suggestions how I can get this to work?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.