-->
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.  [ 5 posts ] 
Author Message
 Post subject: How do you use the HQL "select new" clause?
PostPosted: Fri Oct 28, 2005 12:00 pm 
Senior
Senior

Joined: Thu Jun 02, 2005 5:03 pm
Posts: 135
Location: Paris
I'm trying to use the "select new [classname]" syntax to load some data into a data transfer class as described in the HQL section of the Hibernate manual but I can't seem to get it to work, so I must be missing a step somewhere along the line.

The select statement I'm using is:

Quote:
SELECT new TestClass(u.Id, u.Name, u.email) FROM User u


The error I'm getting is:

Quote:
class not found: TestClass [SELECT new TestClass(u.Id, u.Name, u.email) FROM Framework.Core.Entities.User u]

Stack Trace:
at NHibernate.Hql.SelectParser.Token(String token, QueryTranslator q)
at NHibernate.Hql.ClauseParser.Token(String token, QueryTranslator q)
at NHibernate.Hql.ClauseParser.End(QueryTranslator q)
at NHibernate.Hql.PreprocessingParser.End(QueryTranslator q)
at NHibernate.Hql.ParserHelper.Parse(IParser p, String text, String seperators, QueryTranslator q)
at NHibernate.Hql.QueryTranslator.Compile()
at NHibernate.Hql.QueryTranslator.Compile(ISessionFactoryImplementor factory, IDictionary replacements, Boolean scalar)
at NHibernate.Impl.SessionFactoryImpl.GetQuery(String queryString, Boolean shallow)
at NHibernate.Impl.SessionImpl.GetQueries(String query, Boolean scalar)
at NHibernate.Impl.SessionImpl.Find(String query, QueryParameters parameters)
at NHibernate.Impl.SessionImpl.Find(String query, Object[] values, IType[] types)
at NHibernate.Impl.SessionImpl.Find(String query)


When I had a look at the NHibernate code it seems like the class 'TestClass' needs to be loaded into the session factory. I've got it in the assembly - do I also need a mapping file? That doesn't seem to make sense... Does the class even have to be in the assembly?

Can someone get me on the right track?

Cheers,

Symon.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 28, 2005 12:55 pm 
Newbie

Joined: Thu Sep 08, 2005 12:15 pm
Posts: 17
Quote:
I've got it in the assembly - do I also need a mapping file?


yes you need to got it in the assembly, and yes you need a mapping file.

I didn't find any docs about it but here is how i got it to works.
btw if anybody know a better way or if Im doing something wrong please tell me!

1. Create the Datatransfert class ( TestClass in your case dont forget to write a constructor with the Id, name and email parameters)
2. Create a mapping file for this class but don't put the table attribute in the class tag and don't put the column attribute ( make it an embedded ressource as the other mapping file)
3 Write your select


So I think that the class must have a mapping file that just tell the session whats his properties. But the mapping file shouldn't map to any table/column of your database.

Hope this help

Simon


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 28, 2005 1:27 pm 
Senior
Senior

Joined: Thu Jun 02, 2005 5:03 pm
Posts: 135
Location: Paris
Thanks Simon,

That provides me with a short term workaround for an existing problem, but isn't my holy grail. :)

I was hoping that using mapping files wasn't necessary as I am wrapping NHibernate into my own framework and was hoping that the users of my framework could use their own DTCs.

I've just returned from doing some reading on the Hibernate forums and found the following post (http://forum.hibernate.org/viewtopic.php?p=2220008) which seems to indicate that in the Hibernate equivalent you don't need a mapping file and can get away with simply using a fully qualified name in your query.

Sadly I can't get this to work in NHibernate - and the code doesn't *look* like it's supposed to support it. I'm using the full namespace, but it doesn't seem to work even if I have the class in the main assembly (I tried it even though it's not what I'm looking for).

Is this a bug, C# limitation or is there something else I'm missing that will enable me to use DTCs from another assembly without mapping files?

Cheers,

Symon.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 28, 2005 2:50 pm 
Contributor
Contributor

Joined: Thu May 12, 2005 8:45 am
Posts: 226
All you need is an import. See http://nhibernate.sourceforge.net/forum/viewtopic.php?t=743&highlight=import.

It may be possible to add an import to the Configuration programmatically, but I'm not sure.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 29, 2005 6:32 am 
Senior
Senior

Joined: Thu Jun 02, 2005 5:03 pm
Posts: 135
Location: Paris
Thanks k-dub,

I'll give that a try. Looks like you can probably do it programmatically using Configuration.CreateMappings().AddImport([classname], [rename]).

In the post that you referred to Sergey said...

Quote:
Yes it does, but due to .NET type name syntax (the type must include the assembly name, separated by a comma) the only practical way to make it work is to use an <import> element in your mappings, with the fully qualified name.


...which would seem to indicate that you don't need to use an <import> directive, but I've taken a look at the code for NH 1.0 and it very specifically limits the class names you can use to ones in the import list.

Is that a bug, or does the requirement of having a comma between the class name and the assembly name in the HQL break the HQL parsing?

Cheers,

Symon.


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