-->
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.  [ 12 posts ] 
Author Message
 Post subject: Projection.Property returns Property not found
PostPosted: Thu Oct 02, 2008 3:39 am 
Beginner
Beginner

Joined: Tue Dec 04, 2007 4:48 pm
Posts: 21
Hi,

I have two Classes (User and Line), which are in an n:m relationship. A Line has an Id, so I tried this query to get all the Line-Ids of an user:

Code:
DetachedCriteria lineOfUser = DetachedCriteria.For(typeof(User), "user")
                .CreateCriteria("Line", "userLines")
                .SetProjection(Projections.Property("userLines.Id"))
                .Add(Expression.Eq("user.Id", _CUSTOMER_ID));

lineOfUser.GetExecutableCriteria(session).List();


but this gives me an error:

NHibernate.QueryException: could not resolve property: Id of: NHibernateQueries.Line

So in desperation I did the following: I refactored Line, so that the Id is now called LineId and all over sudden this

Code:
DetachedCriteria lineOfUser = DetachedCriteria.For(typeof(User), "user")
                .CreateCriteria("Line", "userLines")
                .SetProjection(Projections.Property("userLines.LineId"))
                .Add(Expression.Eq("user.Id", _CUSTOMER_ID));


Does work! Interesting why the first doesn't work ...

BTW: I#m using NHibernate 1.2.1.400


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 02, 2008 4:25 am 
Expert
Expert

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

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 02, 2008 7:29 am 
Beginner
Beginner

Joined: Tue Dec 04, 2007 4:48 pm
Posts: 21
OK, these are my mappings:

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping
  xmlns="urn:nhibernate-mapping-2.2"
  assembly="NHibernateQueries"
  namespace="NHibernateQueries"
  default-lazy="true"
  default-access="property">
 
  <class name="User" table="Customer">
    <id name="Id" unsaved-value="0">
      <generator class="native"/>
    </id>
    <property name="Name"/>
    <set name="Line" cascade="all" table="CustomerLine" inverse="false">
      <key column="CustomerId" />
      <many-to-many class="Line" column="FkLineId" />
    </set>
  </class> 
 
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping
  xmlns="urn:nhibernate-mapping-2.2"
  assembly="NHibernateQueries"
  namespace="NHibernateQueries"
  default-lazy="true"
  default-access="property">
 
  <class name="Project" table="Project">
    <id name="Id">
      <generator class="native"/>
    </id>
    <property name="Name"/>
    <set cascade="all" name="Line" table="ProjectLine" inverse="false">
      <key column="ProjectId"/>
      <many-to-many class="Line" column="FkLineId"/>
    </set>
  </class> 
 
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping
  xmlns="urn:nhibernate-mapping-2.2"
  assembly="NHibernateQueries"
  namespace="NHibernateQueries"
  default-lazy="true"
  default-access="property">
 
  <class name="Line" table="Line">
    <id name="Id"  column="Id" type="Int32" unsaved-value="0">
      <generator class="native"/>
    </id>
    <property name="Name"/>
  </class> 
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 02, 2008 8:14 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Hmmm, when you do not name the projection explicitly hibernate creates an alias which in this case probably is "Id". Maybe hibernate gets confused with both the Id then. You can try and use either Projections.Id() or specifiy an explicit name for the projections.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 02, 2008 8:30 am 
Beginner
Beginner

Joined: Tue Dec 04, 2007 4:48 pm
Posts: 21
wolli wrote:
Hmmm, when you do not name the projection explicitly hibernate creates an alias which in this case probably is "Id". Maybe hibernate gets confused with both the Id then. You can try and use either Projections.Id() or specifiy an explicit name for the projections.


You mean NHibernate get's confused with the Id of the User and the Id of the line?

What do you mean by 'specify an explicit name for the projections'? I already tried:

Code:
.SetProjection(Projections.Property("userLines.Id"))


and Projections.Id() (without trying) shoud give me the Id of the User-object, not the Id of the Line :(


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 02, 2008 8:44 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
.SetProjection(Projections.Property("userLines.Id"),"lineid")

But that's nothing more than a guess. And you're probably right about this:

Quote:
and Projections.Id() (without trying) shoud give me the Id of the User-object, not the Id of the Line :(

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 02, 2008 8:53 am 
Beginner
Beginner

Joined: Tue Dec 04, 2007 4:48 pm
Posts: 21
wolli wrote:
.SetProjection(Projections.Property("userLines.Id"),"lineid")

But that's nothing more than a guess. And you're probably right about this:

Quote:
and Projections.Id() (without trying) shoud give me the Id of the User-object, not the Id of the Line :(


there is no such method

SetProjection(IProjection, string) ... at least Intellisense is complaining about it :


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 02, 2008 9:04 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Ah, sorry. Seems to be only possible if you use a projection list:

.SetProjection(Projections.ProjectionList().Add(IPrpjection,string))

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 02, 2008 9:47 am 
Beginner
Beginner

Joined: Tue Dec 04, 2007 4:48 pm
Posts: 21
wolli wrote:
Ah, sorry. Seems to be only possible if you use a projection list:

.SetProjection(Projections.ProjectionList().Add(IPrpjection,string))


Cool! That worked - although the query does look a little complicated by now :(

But my first try should work, shouldn't it?

BTW: can you help me with my post at http://forum.hibernate.org/viewtopic.php?t=991143?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 02, 2008 10:00 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Quote:
But my first try should work, shouldn't it?


Would have thought that ...

Have you tried using HQL instead. For static queries it's the better approach. You can even put the query to the mapping file (as a named query).

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 02, 2008 12:21 pm 
Beginner
Beginner

Joined: Tue Dec 04, 2007 4:48 pm
Posts: 21
wolli wrote:
Have you tried using HQL instead. For static queries it's the better approach. You can even put the query to the mapping file (as a named query).


Nope, I haven't. I just wanted to make sure that I can execute such a query with NHibernate :)

I will have to do some thinking on how/where to create the queries - actually I would like to leave any NHibernate related stuff out of my application layer. But I also don't want to have individual repositories, which have methods like 'GetAllProjectsWithLinesMatchingUser' ... don't know what the best approach would be.


Top
 Profile  
 
 Post subject: Re: Projection.Property returns Property not found
PostPosted: Thu May 27, 2010 11:51 pm 
Newbie

Joined: Thu May 27, 2010 11:24 pm
Posts: 1
i need to get data from releted table also . how can i get that.
like I have table Book and Category
i need to get book details with category name.
how can i get it through "Projection.Property"

Book.MyCategory.Name

if i given like this it shows error

Add(Projections.Property("MyCategory.Name"))

please help me.


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