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.  [ 8 posts ] 
Author Message
 Post subject: "table"attribute: Specify query instead of tbl name
PostPosted: Mon Aug 31, 2009 6:38 am 
Newbie

Joined: Mon Aug 31, 2009 6:30 am
Posts: 9
Location: Bolzano, Italy
Hi all,

I have the following problem: I need to select an entity House from a view HOUSES_VIEW. However, the ID of HOUSE is not unique in that view. I would like to pre-select using e.g. SELECT DISTINCT ID, NAME FROM HOUSES_VIEW as value for the "table" attribute, instead of only the table name. Is this possible? I already tried, but nhibernate issues me an ADOException that it cannot be executed. I know, I could create simply another view and select from that one, but that should be avoided, if possible.

Are there other solutions? Is the "subselect" attribute the way to go? Below is an example.

unique in theory, but not working:
<class name="MyCode.House" mutable="false" table="(SELECT DISTINCT ID, NAME FROM HOUSES_VIEW)">

non-unique, original:
<class name="MyCode.House" mutable="false" table="HOUSES_VIEW">

Thanks in advance!

Best regards,
Martin

_________________
Tutorials and solved problems under:
http://angler.wordpress.com


Top
 Profile  
 
 Post subject: Re: "table"attribute: Specify query instead of tbl name
PostPosted: Mon Aug 31, 2009 7:11 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
You can specifiy custom SQL statements for load/ubdateinsert/delete:

http://nhforge.org/doc/nh/en/index.html#querysql-cud

That's the only way you can do what you want with hibernate (except the view you already mentioned).

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: "table"attribute: Specify query instead of tbl name
PostPosted: Mon Aug 31, 2009 10:45 am 
Newbie

Joined: Mon Aug 31, 2009 6:30 am
Posts: 9
Location: Bolzano, Italy
Hello,

Thanks a lot! It seems to be the way to go. However, I have a few issues implementing it:
Having:
<sql-query name="load" read-only="true">
<return class="MyCode.House, MyCode"></return>
<![CDATA[
select distinct ID, NAME FROM HOUSE
]]>
</sql-query>

and specified :
<loader query-ref="load"/>

it produces approximately the same SQL code, but specifying at the end ...FROM HOUSE instead of: ... FROM HOUSES_VIEW. Clearly, since HOUSE is not a table, it cannot be accessed and a NHibernateADOException is issued, specifying the table does not exist.

Any ideas?

Thanks a lot!
Martin

_________________
Tutorials and solved problems under:
http://angler.wordpress.com


Top
 Profile  
 
 Post subject: Re: "table"attribute: Specify query instead of tbl name
PostPosted: Tue Sep 01, 2009 1:24 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
You have to use the view instead of the table: select distinct ID, NAME FROM HOUSE_VIEW.

There's one thing you have to remember when usin custom sql for loading. It only is used for session.Get or session.Load. HQL and criterias will still use the table that is specified in the table attribute of the class.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: "table"attribute: Specify query instead of tbl name
PostPosted: Tue Sep 01, 2009 5:53 am 
Newbie

Joined: Mon Aug 31, 2009 6:30 am
Posts: 9
Location: Bolzano, Italy
Hi,

Sorry, apparently I made a mistake in my previous post.

I did already use HOUSES_VIEW in the named sql query. When executing the query, it still won't work, issuing an SQL command using ... FROM House hous_0. An NHibernateADOException is issued, specifying the table does not exist.

Sorry for the mistake.

I still can't figure out what the problem is there. Do you have any more ideas?

Thanks a lot and best regards,
Martin

_________________
Tutorials and solved problems under:
http://angler.wordpress.com


Top
 Profile  
 
 Post subject: Re: "table"attribute: Specify query instead of tbl name
PostPosted: Tue Sep 01, 2009 5:55 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Can you post the complete mapping file ?

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: "table"attribute: Specify query instead of tbl name
PostPosted: Tue Sep 01, 2009 6:29 am 
Newbie

Joined: Mon Aug 31, 2009 6:30 am
Posts: 9
Location: Bolzano, Italy
Sure! Here it is:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="MyCode.House, MyCode" mutable="false" lazy="false" >
<id name="Id" column="ID" type="System.Int32"/>
<property name="Name" column="NAME" type="System.String" />
<loader query-ref="load"/>
</class>
<query name="GetHouses">
<![CDATA[
select h from House h
]]>
</query>
<sql-query name="load" read-only="true">
<return class="MyCode.House, MyCode"></return>
<![CDATA[
select distinct ID, NAME FROM HOUSES_VIEW
]]>
</sql-query>
</hibernate-mapping>

_________________
Tutorials and solved problems under:
http://angler.wordpress.com


Top
 Profile  
 
 Post subject: Re: "table"attribute: Specify query instead of tbl name
PostPosted: Tue Sep 01, 2009 7:38 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Try this:

Code:
<sql-query name="load" read-only="true">
<return alias="h" class="MyCode.House, MyCode"></return>
<![CDATA[
select distinct ID as {h.Id}, NAME as {h.Name} FROM HOUSES_VIEW
]]>
</sql-query>


Quote:
I did already use HOUSES_VIEW in the named sql query. When executing the query, it still won't work, issuing an SQL command using ... FROM House hous_0. An NHibernateADOException is issued, specifying the table does not exist.


What do you mean with query ? As I said, when you use HQL or a criteria, the custom SQL will NOT be used. If you need to be able to run flexible queries against the view, you have to map the view itself (or in your case define another view with unique rows).

_________________
--Wolfgang


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 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.