Transformers for HQL not support yet? (Hibernate3.2 dose)

75 views
Skip to first unread message

crabo

unread,
Sep 11, 2008, 9:35:51 PM9/11/08
to nhusers
I really like the following query expression, but I have no success
with my NUnit test.


check out : http://blog.hibernate.org/2133.lace

Hibernate 3.2: Transformers for HQL and SQL

HQL Transformers
Now you can get the value injected via property methods or fields
instead, removing the need for explicit constructors.

List resultWithAliasedBean = s.createQuery(
"select e.student.name as studentName," +
" e.course.description as courseDescription" +
"from Enrolment as e")
.setResultTransformer( Transformers.aliasToBean(StudentDTO.class))
.list();

StudentDTO dto = (StudentDTO) resultWithAliasedBean.get(0);

Ken Egozi

unread,
Sep 12, 2008, 3:36:26 AM9/12/08
to nhu...@googlegroups.com
s.CreateQuery(hql)
   .SetResultTransforemer(Transformers.AliasToBean(typeof(DTO))
   .List<DTO>();

crabo

unread,
Sep 16, 2008, 4:36:06 AM9/16/08
to nhusers
Have you ever tried? I have never successed.

s.CreateQuery("select u from User u")
.SetResultTransformer(Transformers.AliasToBean(typeof(User))
.List();

I'm not using the generic dao.
Test.UserTest.TestHqlTransformer :
Spring.Data.NHibernate.HibernateSystemException : Could not execute
query[SQL: SQL not available]
----> NHibernate.Exceptions.GenericADOException : Could not execute
query[SQL: SQL not available]
----> System.NullReferenceException : 未将对象引用设置到对象的实例。
> Ken Egozi.http://www.kenegozi.com/bloghttp://www.musicglue.comhttp://www.castleproject.orghttp://www.gotfriends.co.il- 隐藏被引用文字 -
>
> - 显示引用的文字 -

Ken Egozi

unread,
Sep 16, 2008, 3:18:04 PM9/16/08
to nhu...@googlegroups.com
if User is a mapped entity then you need not the ResultTransformer.
simply use:

var users = s.CreateQuery("from User u").List<User>();


however, assuming you had a DTO like the following:
class UserInfo
{
   string Name {get; set; }
   string Email {get; set; }
}

then you can do:

var userInfos = s.CreateQuery("select u.Name, u.Email from User u")
 .SetResultTransformer(Transformers.AliasToBean(typeof(UserInfo))
 .List<UserInfo>();




2008/9/16 crabo <crabo...@gmail.com>

Fabio Maulo

unread,
Sep 16, 2008, 3:27:12 PM9/16/08
to nhu...@googlegroups.com
Or
s.CreateQuery("select u.Name, count(*) from User u group by u.Name order by u.Name")
.SetResultTrasformer(new YourTransformer<YourType>())
.List<YourType>();

Or
s.CreateQuery("select u.Name, count(*) from User u group by u.Name order by u.Name")
.SetResultTrasformer(new PositionalToBeanResultTransformer(typeof(Something), new string[]{"name","count"}))
.List<YourType>();



2008/9/16 Ken Egozi <ego...@gmail.com>



--
Fabio Maulo

crabo

unread,
Sep 16, 2008, 8:49:00 PM9/16/08
to nhusers
Sorry for my poor description above. I have use Hibernate and
NHibernate for more than 1 years .
so I do know the SetResultTransformer API. and also the HQL or
mapping classed means.

You got my point:
var userInfos = s.CreateQuery("select u.Name, u.Email from User u")
.SetResultTransformer(Transformers.AliasToBean(typeof(UserInfo))
.List<UserInfo>();

I really want a " partial object query" . the different is ,I want my
DTO is just the mapping entity.
so I test:
var userInfos = s.CreateQuery("select u.Name, u.Email from User u")
.SetResultTransformer(Transformers.AliasToBean(typeof(User))
.List()

But also encounter the NULL exception above.

Thanks very much!

On 9月17日, 上午3时27分, "Fabio Maulo" <fabioma...@gmail.com> wrote:
> Ors.CreateQuery("select u.Name, count(*) from User u group by u.Name order
> by u.Name")
> .SetResultTrasformer(new YourTransformer<YourType>())
> .List<YourType>();
>
> Or
> s.CreateQuery("select u.Name, count(*) from User u group by u.Name order by
> u.Name")
> .SetResultTrasformer(new PositionalToBeanResultTransformer(typeof(Something),
> new string[]{"name","count"}))
> .List<YourType>();
>
> 2008/9/16 Ken Egozi <egoz...@gmail.com>
>
>
>
>
>
> > if User is a mapped entity then you need not the ResultTransformer.
> > simply use:
>
> > var users = s.CreateQuery("from User u").List<User>();
>
> > however, assuming you had a DTO like the following:
> > class UserInfo
> > {
> > string Name {get; set; }
> > string Email {get; set; }
> > }
>
> > then you can do:
>
> > var userInfos = s.CreateQuery("select u.Name, u.Email from User u")
> > .SetResultTransformer(Transformers.AliasToBean(typeof(UserInfo))
> > .List<UserInfo>();
>
> > 2008/9/16 crabo <crabo.Y...@gmail.com>
> >>http://www.kenegozi.com/bloghttp://www.musicglue.comhttp://www.castle...藏被引用文字 -
>
> >> > - 显示引用的文字 -
>
> > --
> > Ken Egozi.
> >http://www.kenegozi.com/blog
> >http://www.musicglue.com
> >http://www.castleproject.org
> >http://www.gotfriends.co.il
>
> --
> Fabio Maulo- 隐藏被引用文字 -
>
> - 显示引用的文字 -

Fabio Maulo

unread,
Sep 17, 2008, 12:51:27 AM9/17/08
to nhu...@googlegroups.com
2008/9/16 crabo <crabo...@gmail.com>

I really want a " partial object query" . the different is ,I want my
DTO is just the mapping entity.
so I test:
var userInfos = s.CreateQuery("select u.Name, u.Email from User u")
 .SetResultTransformer(Transformers.AliasToBean(typeof(User))
 .List()

Here is some other problem out of IT matters...


Fabio Maulo

unread,
Sep 17, 2008, 1:09:27 AM9/17/08
to nhu...@googlegroups.com
Perhaps I understand what you are looking for... 
First of all, please don't create a JIRA for this matter because we know it and we have more than one issue about it.
What you want (but not needed by your last example) is the support of aliases in HQL select clause.

NH don't support aliases in select-clause and to support it we are waiting an AST based HQL parser.

If you want, you can use PositionalToBeanResultTransformer, available in uNhAddIns and in Burrow.AppBlock, or you can create your own trasformer.

But note that it is a completely different matter than Transformers support.

Bye.
Fabio Maulo
P.S. hope lord are helping me to close this issue between forum and JIRA

2008/9/17 Fabio Maulo <fabio...@gmail.com>



--
Fabio Maulo

crabo

unread,
Sep 17, 2008, 3:19:15 AM9/17/08
to nhusers
Yes, this is what i'm talking about.

Ok, I will wait for AST based HQL parser.

Or, I would create my own transfomer.

I'm so hurry because I found Hibernate 3.2 does support, and want to
know if our .NET world have some excellent solution.

Thank you for your efforts, really!
>
> --
> Fabio Maulo
Reply all
Reply to author
Forward
0 new messages