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: Could not resolve property
PostPosted: Tue Sep 02, 2008 11:44 am 
Beginner
Beginner

Joined: Fri Apr 27, 2007 11:50 am
Posts: 23
Hi Guys,

I've hit across a problem and keep getting the error

Code:
could not resolve property: User.Email of: Core.Domain.NewsletterSubscription


Whilst i know this usualy revolves around an incorrect mapping, the weird thing is a different query that queries agains "User.Id" works.

The code for the 2 functions are:

Code:
        public NewsletterSubscription GetByEmail(string email)
        {
            ICriteria criteria = NHibernateSession.CreateCriteria(typeof(NewsletterSubscription));
            criteria.Add(Expression.Eq("User.Email", email));
            return criteria.UniqueResult<NewsletterSubscription>();
        }

        public NewsletterSubscription GetByUser(User user)
        {
            ICriteria criteria = NHibernateSession.CreateCriteria(typeof(NewsletterSubscription));
            criteria.Add(Expression.Eq("User.Id", user.Id));
            return criteria.UniqueResult<NewsletterSubscription>();
        }


With the mapping file for the NewsletterSubscription class being:

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="Core.Domain.NewsletterSubscription, Core" table="`NewsletterSubscription`" lazy="false">

    <id name="Id" column="`NewsletterSubscriptionId`" unsaved-value="00000000-0000-0000-0000-000000000000">
      <generator class="guid" />
    </id>
    
    <property name="Email" access="nosetter.camelcase-underscore" />
   <property name="IsVerified" />
   <property name="IsActive" />
    <property name="CreatedDate" />
   <property name="ModifiedDate" />
   
   <many-to-one name="User" class="Core.Domain.User, Core" column="`UserId`" not-null="false" />

  </class>
</hibernate-mapping>


Anybody got any ideas why one would work, but the other throws the said error?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 03, 2008 3:08 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Probably the "Id" property is handled differently. Without creating the association criteria (CreateCriteria, CreateAlias) User.Id will internally be translated to column "userid" from table NewsletterSubscription.
When you use User.Email you definitely need the join because that information is only available on table User.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 03, 2008 4:28 am 
Beginner
Beginner

Joined: Fri Apr 27, 2007 11:50 am
Posts: 23
Hey wolli,

Thanks for the pointers.

Just one other question. The NewsletterSubscription entity has a property "Email" of it's own, which it's getter is:

Code:
get { return (User != null)? User.Email : _email; }


Should this work within the context of the NHibernate query? or will it just check against the field in the database?

Many thanks

Matt


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 03, 2008 4:33 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
You can only use "mapped" properties/fields in a query/criteria. In your case you have to use:

Code:
criteria.CreateCriteria("User", "u")
       .Add(Expression.Eq("u.Email", email));

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 03, 2008 4:51 am 
Beginner
Beginner

Joined: Fri Apr 27, 2007 11:50 am
Posts: 23
Hey wolli,

Thanks for your help, i've ended up with the following as i need to check both fields:


Code:
ICriteria criteria = NHibernateSession.CreateCriteria(typeof(NewsletterSubscription));
            criteria.CreateAlias("User", "u", JoinType.LeftOuterJoin);
            criteria.Add(
                Expression.Or(
                    Expression.Eq("Email", email),
                    Expression.Eq("u.Email", email)
                )
            );
            return criteria.UniqueResult<NewsletterSubscription>();


Many thanks


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.