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.  [ 9 posts ] 
Author Message
 Post subject: custom sql for loading a collection
PostPosted: Thu Mar 13, 2008 6:51 pm 
Newbie

Joined: Thu Mar 13, 2008 6:30 pm
Posts: 5
Hi,

I'm a new user here stuck trying to load a collection using a custom sql. I would appreciate any help on this. Here is the simplified version of my mapping:

<hibernate-mapping>

<class name="net.newfoundcomm.gps.dao.user.User" table="users">
<id name="id" column="user_id" type="java.lang.Long">
<generator class="native"/>
</id>

<set name="items" lazy="false">
<key column="item_id"/>
<one-to-many class="net.newfoundcomm.gps.dao.item.Item"/>
<loader query-ref="getItems"/>
</set>

</class>

<sql-query name="getItems">
<load-collection alias="i" role="User.items"/>
select {i.*} from items i
</sql-query>

</hibernate-mapping>

Here is the exception I get:
Caused by: org.hibernate.HibernateException: Errors in named queries: getItems

I tried searching on the internet but couldn't find any solution. I'd greatly appreciate any input on this.

Thanks.


Top
 Profile  
 
 Post subject: Re: custom sql for loading a collection
PostPosted: Thu Mar 13, 2008 6:56 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
Read this one. It might give you an idea:


http://forum.hibernate.org/viewtopic.php?t=982986



Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 13, 2008 7:07 pm 
Newbie

Joined: Thu Mar 13, 2008 6:30 pm
Posts: 5
Thanks for your reply. I'm trying to load the collection along with the Parent class 'User'. My mapping looks similar to the one in that post and it looks very much like the any thing you would find in the docs.

But not sure what is wrong with the sql. Do you see anything wrong there?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 13, 2008 9:19 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
Have you tried the following?

Code:
<sql-query name="getItems">
<load-collection alias="i" role="net.newfoundcomm.gps.dao.user.User.items"/>
select i.* from items i where i.userId = ?
</sql-query>



Remember you will need at least one parameter.


Let me know how it goes and give me the whole exception if this breaks.

Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 14, 2008 11:53 am 
Newbie

Joined: Thu Mar 13, 2008 6:30 pm
Posts: 5
And should I be passing the parameter value to that query? Here is the entire stack trace:

Caused by: org.hibernate.HibernateException: Errors in named queries: getItems
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:365)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1300)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:805)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:745)
at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:134)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1203)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1172)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:427)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:249)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:155)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:246)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:160)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:267)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:110)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1100)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:862)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:423)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:249)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:155)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:246)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:160)
at net.newfoundcomm.gps.util.test.Test.getUser(Test.java:92)
at net.newfoundcomm.gps.util.test.Test.main(Test.java:43)


Thanks for your help.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 14, 2008 11:56 am 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
No Hibernate does it for you. This is way before that anyways. Which hibernate version are you using? I did an example last night and it worked for me. I am using 3.2.5 and I can send you the example later in the day, when I get home.



Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 14, 2008 12:07 pm 
Newbie

Joined: Thu Mar 13, 2008 6:30 pm
Posts: 5
I'm using 3.2.6. An example would be great. Thanks a lot.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 16, 2008 7:58 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
nitind wrote:
I'm using 3.2.6. An example would be great. Thanks a lot.


There you go:

Code:
<hibernate-mapping>
    <class name="test.model.data.Person" table="Person" mutable="true" >

        <id name="id" type="long" unsaved-value="null">
            <column name="ID"/>
            <generator class="identity"/>
        </id>

        <property name="name" type="string">
            <column name="Name"/>
        </property>

        <set name="areas">
            <key name="PersonID"/>
            <one-to-many class="test.model.data.Area"/>
            <loader query-ref="getAreas" />
        </set>

    </class>

    <sql-query name="getAreas">
        <load-collection alias="a" role="test.model.data.Person.areas"/>
        select a.* from Area a inner join PersonArea pa on a.id = pa.areaid
        where pa.PersonID = ?       
    </sql-query>
</hibernate-mapping>



Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 18, 2008 3:52 pm 
Newbie

Joined: Thu Mar 13, 2008 6:30 pm
Posts: 5
Thank you. I'll try this out and let you know how it goes.


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

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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.