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.  [ 11 posts ] 
Author Message
 Post subject: Problem with HQL SELECT....Help me.
PostPosted: Wed Aug 16, 2006 10:18 am 
Newbie

Joined: Tue Jul 18, 2006 2:29 pm
Posts: 10
I'm a newbie with NHibernate.
I have a problem when execute a Hibernate Query Language (HQL) with SELECT.

This is my HQL:
string iquery ="SELECT blog.BlogId ,blog.BlogName,count(blogItem) " +"from Blog as blog left join blog.BlogItems as blogItem " +
"group by blog.BlogName, blog.BlogId " +
"order by max(blogItem.ItemTime)"

No error occur, but everything I see in DataGrid is:

Rank IsReadOnly IsSynchronized IsFixedSize Length LongLength
1 False False True 3 3
1 False False True 3 3

How can I display the field in my HQL SELECT.....Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 16, 2006 11:07 am 
Beginner
Beginner

Joined: Wed Apr 19, 2006 9:16 am
Posts: 24
I'm by no means an expert on databinding, but I suspect what happened is this: The return value in the case of your query is a list that contains object arrays. What you've done is bind the resulting list to the datagrid. When the the elementtype of the list is inspected for databinding, the elementtype is determined to be of type System.Array, and the properties of System.Array get bound to the grid.

I've no idea if there's a way to bind individual array items to a grid (anyone else?), but what you can do instead is this:

"SELECT new GridBlog(blog.BlogId ,blog.BlogName,count(blogItem)) from Blog as..." <etc>

Where GridBlog is a custom class that has a constructor with the signature
(int, string, int). The type of the elements in the list are then of type "GridBlog", and the properties of that class will be bound to the grid.

_________________
regards,
Willem van Rumpt


Top
 Profile  
 
 Post subject: Please show me....
PostPosted: Thu Aug 17, 2006 10:01 am 
Newbie

Joined: Tue Jul 18, 2006 2:29 pm
Posts: 10
I created a class as your guide, but I receive error: class not found. All the files are in the same folder....

How can I create this class...Does it has a *.hbm.xml ? You can show me by small example...Thanks so much...:)


Last edited by texudo on Thu Aug 17, 2006 12:51 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Please show me....
PostPosted: Thu Aug 17, 2006 10:49 am 
Beginner
Beginner

Joined: Wed Apr 19, 2006 9:16 am
Posts: 24
texudo wrote:
I created a class as your guide, but I receive error: class not found. All the files is in the same folder....

How can I create this class...Does it has a *.hbm.xml ? You can show me by small example...Thanks so much...:)


Sorry, I forget to mention: You have to import the class with an import element in one of your mapping files (doesn't matter which one), somehow I can't get it to work with using attributes, so you have to manually insert it in one of *.hbm.xml files:

Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
  <import class="<AssemblyQualifiedName of the GridBlog class>" />
  ...
  other mappings
  ...
</hibernate-mapping>


As an alternative you can use a class that is mapped, in which case it should work without the import.

_________________
regards,
Willem van Rumpt


Top
 Profile  
 
 Post subject: Thanks
PostPosted: Thu Aug 17, 2006 12:42 pm 
Newbie

Joined: Tue Jul 18, 2006 2:29 pm
Posts: 10
I have solved this problem . Thanks so much.....
Regards.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 17, 2006 4:10 pm 
Newbie

Joined: Thu Jun 15, 2006 5:27 am
Posts: 13
Please could you explain how you solved the problem? I have the same problem and I use NHibernate Mapping Attributes. Could you give a like code example.

Thanks a lot


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 18, 2006 2:57 am 
Beginner
Beginner

Joined: Wed Apr 19, 2006 9:16 am
Posts: 24
benny7 wrote:
Please could you explain how you solved the problem? I have the same problem and I use NHibernate Mapping Attributes. Could you give a like code example.

Thanks a lot


Willem van Rumpt wrote:
<snip>...I can't get it to work with using attributes, so you have to manually insert it in one of *.hbm.xml files...<snip>


As an alternative, create a separate hbm.xml file to contain the import, since the ones that map to classes will be overwritten if you regenerate them.

I'm working on a fix to get the importattribute to work.

_________________
regards,
Willem van Rumpt


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 18, 2006 3:38 am 
Newbie

Joined: Thu Jun 15, 2006 5:27 am
Posts: 13
Ok thanks. So, apart from the import, should I leave mapping instructions in the .hbm.xml file?

could you give a code so as for me to understand better?

This is my HQL query:
Code:
Select new Pieces(p.CodePiece, p.LibPiece) from Pieces as p where p.CodeDomaine is null


Pieces is a mapped class that has NHibernate Mapping Attributes in it, with a constructor like:
Code:
public Pieces(string codePiece, string libPiece)


So, when I execute this query, I get no error but all the fields of my table are shown while I only want to see 2 fields. What can I do please?

What do you thik about it?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 18, 2006 7:04 am 
Beginner
Beginner

Joined: Wed Apr 19, 2006 9:16 am
Posts: 24
benny7 wrote:
This is my HQL query:
Code:
Select new Pieces(p.CodePiece, p.LibPiece) from Pieces as p where p.CodeDomaine is null


Pieces is a mapped class that has NHibernate Mapping Attributes in it, with a constructor like:
Code:
public Pieces(string codePiece, string libPiece)


So, when I execute this query, I get no error but all the fields of my table are shown while I only want to see 2 fields.


That's because the select statement you use returns a list of Piece instances. If you're only interested in a some of the fields, and want a class that wraps those fields, and only those two fields, you have to go the import way.

Define a new class:
Code:
public class PieceWrapper
{
   public PieceWrapper(string piece, string libPiece)
   
   // Define properties that expose the piece and libPiece, etc.
}


create a new hbm.xml file that contains the import (i.e. "imports.hbm.xml"):

Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
  <import class="<AssemblyQualifiedName of the PieceWrapper class>" />
</hibernate-mapping>


and change the select statement to

Code:
select new PieceWrapper(p.CodePiece, p.LibPiece) from Pieces p where p.CodeDomaine is null

_________________
regards,
Willem van Rumpt


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 18, 2006 9:06 am 
Newbie

Joined: Thu Jun 15, 2006 5:27 am
Posts: 13
Thanks for your help. I want to know if there is another way of solving this problem beacause I'm working on a big application an I've got many table; so, if for each table I have to create a class, if won't be good.

Thanks again


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 18, 2006 10:13 am 
Beginner
Beginner

Joined: Wed Apr 19, 2006 9:16 am
Posts: 24
benny7 wrote:
Thanks for your help. I want to know if there is another way of solving this problem beacause I'm working on a big application an I've got many table; so, if for each table I have to create a class, if won't be good.


The alternative is to accept that querying returns an IList with object arrays as its elements, and find a way to bind the elements in an object array to a datasource.

What you need is a column bound to IList[i][j] , and I simply don't know if that's possible

_________________
regards,
Willem van Rumpt


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