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.  [ 6 posts ] 
Author Message
 Post subject: sql query to load an entity
PostPosted: Wed Aug 19, 2009 3:58 pm 
Regular
Regular

Joined: Tue Jun 26, 2007 11:50 am
Posts: 105
Hello,

I would like to use a sql query to load an entity. I work with nhibernate 2.1.0 and a MySQL database.

I followed the instructions in the documentation (http://nhforge.org/
doc/nh/en/index.html#querysql-load)

But I get this error:
Table 'magento131.Shipping' doesn't exist

Here is my mapping file:

<class name="Shipping" lazy="false">
<id name="Id">
<generator class="assigned" />
</id>
<property name="Label" update="false" />
<loader query-ref="shipping"/>
</class>

<sql-query name="shipping">
<return alias="ship" class="Shipping" />
select distinct shipping_method as {ship.Id}, REPLACE
(shipping_method , '_', ' ') as {ship.Label}
from delivery_days
</sql-query>

What am I doing wrong?

Thank you in advance for your help.

mathmax


Top
 Profile  
 
 Post subject: Re: sql query to load an entity
PostPosted: Thu Aug 20, 2009 2:08 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
1. Make sure the mapping file is "Embedded Resource"
2. Use the fully qualified classname or specify this information on the mapping tag:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="sx.business.partner" assembly="sx.business">
3. You have no tablename on the class mapping. I suppose it's mandatory, because ICriteria and IQuery don't use the custom sql.
4. No clue .....

Mapping itself looks fine.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: sql query to load an entity
PostPosted: Fri Aug 21, 2009 4:39 pm 
Regular
Regular

Joined: Tue Jun 26, 2007 11:50 am
Posts: 105
ok, I have to use this to get the data.

var data = _session.GetNamedQuery("shipping").List();

But why don't ICriteria, IQuery and NHibernate.Linq use the query instead of looking for a table since the tag "<loader query-ref="shipping"/>" says that the entity should be loaded via the query?


Top
 Profile  
 
 Post subject: Re: sql query to load an entity
PostPosted: Sat Aug 22, 2009 11:03 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Just think of what a variety of queries you can construct with ICriteria and IQuery. There is no way to make sure that all these will work on top of a custom SQL. The custom sql is jut for session.Get and session.Load.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: sql query to load an entity
PostPosted: Sat Aug 22, 2009 11:23 am 
Regular
Regular

Joined: Tue Jun 26, 2007 11:50 am
Posts: 105
mmh... it's possible to use ICriteria and IQuery when an entity is loaded via a view that is stored on the database side. Thus, Nhibernate see this view as a Table. Why would not it be also possible when the view is defined in the mapping file ? The view could be seen be nh as a table too, couldn't it?


Top
 Profile  
 
 Post subject: Re: sql query to load an entity
PostPosted: Mon Aug 24, 2009 3:26 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
I'm not saying that it is possible in some or even a lot of cases. But there are also cases where it will probably not work, like with a stored procedure.

_________________
--Wolfgang


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.