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.  [ 8 posts ] 
Author Message
 Post subject: NHibernate.PropertyNotFoundException:Could not find a setter
PostPosted: Tue Apr 25, 2006 7:50 am 
Newbie

Joined: Tue Apr 25, 2006 7:30 am
Posts: 3
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hello !

I have used Hibernate in some J2EE development in the past, and now, I hve to try to use Nhibernate in a DOT.NET dev.

So, I have dowloaded NHibernate, and I actually try to use it in Visual Studio 2003 for an aspx/c# development. For that, I follow the instructions from http://www.hibernate.org/362.html , and I am in the step 5. But that doesn't work, I have an error:
" NHibernate.PropertyNotFoundException: Could not find a setter "

Mapping documents:
My XML, located in Mappings , called , County.hbm.xml :

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="ppwebsite.Mappings.County, ppwebsite" table="tblCounty">
<id name="idCounty" column="CountyID" type="System.Int32">
<generator class="assigned"/>
</id>
<property name="nameCounty" column="County" not-null="true" type="String" length="40" />
</class>
</hibernate-mapping>


----------------------

My classe, in the same directory:

using System;

namespace ppwebsite.Mappings
{
public class County
{
private int idCounty;
private string nameCounty;

public County()
{
}

public County(int idCountyNew, string nameCountyNew)
{
this.idCounty=idCountyNew;
this.nameCounty=nameCountyNew;
}

//first try to make a setter
public int Id
{
get { return idCounty; }
set { idCounty = value; }
}

//second try to make a setter
public void setIdCounty(int idCountyNew)
{
this.idCounty=idCountyNew;
}

public int getIdCounty()
{
return this.idCounty;
}

public void setNameCounty(string nameCountyNew)
{
this.nameCounty=nameCountyNew;
}

public string getNameCounty()
{
return this.nameCounty;
}
}
}



Code between sessionFactory.openSession() and session.close():

in my aspx.cs :

......
using NHibernate;
using NHibernate.Cfg;
......


private void Page_Load(object sender, System.EventArgs e)
{
Configuration cfg = new Configuration();
cfg.AddAssembly("ppwebsite");
ISessionFactory factory = cfg.BuildSessionFactory();
ISession session = factory.OpenSession();
ITransaction transaction = session.BeginTransaction();
County c = new County();
c.setIdCounty(70);
c.setNameCounty("countyTest");
session.Save(c);

// commit all of the changes to the DB and close the ISession
transaction.Commit();
session.Close();
}





Full stack trace of any exception that occurs:

Could not find a setter for property 'idCounty' in class 'ppwebsite.Mappings.County'
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: NHibernate.PropertyNotFoundException: Could not find a setter for property 'idCounty' in class 'ppwebsite.Mappings.County'

Source Error:


Line 58: Configuration cfg = new Configuration();
Line 59: cfg.AddAssembly("ppwebsite");
Line 60: ISessionFactory factory = cfg.BuildSessionFactory();
Line 61: ISession session = factory.OpenSession();
Line 62: ITransaction transaction = session.BeginTransaction();


Source File: c:\inetpub\wwwroot\ppwebsite\webform1.aspx.cs Line: 60

Stack Trace:


[PropertyNotFoundException: Could not find a setter for property 'idCounty' in class 'ppwebsite.Mappings.County']
NHibernate.Property.BasicPropertyAccessor.GetSetter(Type type, String propertyName)
NHibernate.Mapping.Property.GetSetter(Type clazz)
NHibernate.Persister.AbstractEntityPersister..ctor(PersistentClass model, ISessionFactoryImplementor factory)
NHibernate.Persister.EntityPersister..ctor(PersistentClass model, ISessionFactoryImplementor factory)
NHibernate.Persister.PersisterFactory.CreateClassPersister(PersistentClass model, ISessionFactoryImplementor factory)
NHibernate.Impl.SessionFactoryImpl..ctor(Configuration cfg, Settings settings)
NHibernate.Cfg.Configuration.BuildSessionFactory()
ppwebsite.WebForm1.Page_Load(Object sender, EventArgs e) in c:\inetpub\wwwroot\ppwebsite\webform1.aspx.cs:60
System.Web.UI.Control.OnLoad(EventArgs e)
System.Web.UI.Control.LoadRecursive()
System.Web.UI.Page.ProcessRequestMain()




--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:1.1.4322.573; ASP.NET Version:1.1.4322.573



Name and version of the database you are using:

I use SQL server, but I can use it correctly without HHibernate, so it shouldn't be the problem


Did I made something wrong ? I mean, I have followed the instructions from "NHibernate Quick Start Guide" http://www.hibernate.org/362.html , so I should at least can run my soft !!!!


Thanks for the people who will answer me.

Rgds,

Stef


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 25, 2006 8:08 am 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
Your C# class is not constructed correctly according to your mapping file.

Your class should look like this to match your mapping file:

Code:
public class County
{
  private int _idCounty;
  private string _nameCounty;

  public County()
  {
  }

  public County(int idCountyNew, string nameCountyNew)
  {
    this.idCounty=_idCountyNew;
    this.nameCounty=_nameCountyNew;
  }

  public int idCounty
  {
    get { return _idCounty; }
    set { _idCounty = value; }
  }

  public int nameCounty
  {
    get { return _nameCounty; }
    set { _nameCounty= value; }
  }

}



Cheers,

Symon.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 25, 2006 9:01 am 
Newbie

Joined: Tue Apr 25, 2006 7:30 am
Posts: 3
Hi Symon

Thank yoyu very much for your help.

I made some little adjusts and now, my example can writte my value. That's a great news. But I don't understand why i had to put some underscores "_" to my vars. This is still a mysterie for the moment.

Anyway, that's a great news, I can continue my learning oh NHibernate :)

So the final "working class" :

using System;

namespace ppwebsite.Mappings
{
public class County
{
private int _idCounty;
private string _nameCounty;

public County()
{
}

public County(int idCountyNew, string nameCountyNew)
{
this._idCounty=idCountyNew;
this._nameCounty=nameCountyNew;
}

public int idCounty
{
get { return _idCounty; }
set { _idCounty = value; }
}

public string nameCounty
{
get { return _nameCounty; }
set { _nameCounty= value; }
}

}
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 25, 2006 12:01 pm 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
Hi Vediovis,

The reason I added underscores is because the mapping property names were referring to your private fields (hence the exception about no setter being found).

For example, you mapping name="idCounty" indicates that NHibernate should look for a property or non-private member of your class with the name "idCounty". Since "idCounty" was a private member was considered not to have a setter.

You had a public property with a getter and a setter called "Id" which referred to the private member "idCounty" but was not mapped by NHibernate. So, to fix this I didn't change your mapping but instead changed your class. I altered the private member so that it didn't match the mapping anymore and then changed the name of the public property "Id" to "idCounty" to match the mapping "idCounty".

Remember, the mapping name should preferably be referring to a property of your class. If you want to have some of your properties read only then you need to use a naming strategy so that NHibernate can determine which private member to use based on the property name you specify in your mapping file.

You may need to read the sections regarding mappings in the documentation again to get the full story on how this works.

Hope that helps!

Symon.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 25, 2006 12:24 pm 
Newbie

Joined: Tue Apr 25, 2006 7:30 am
Posts: 3
of course.... so accustomed to use private for properties than I didn't think at that....

>Hope that helps!

it definitively did :)

thank you very much.

Stef


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 13, 2007 12:04 pm 
Newbie

Joined: Fri Mar 02, 2007 5:21 am
Posts: 3
Hi,

sorry to interrupt but i am having the same problem and i do exacly what you have been writen here.

customer.hbm.xml:
Code:
      <id name="ID" column="ID" type="Int32" unsaved-value="0" >
         <generator class="identity" />         
      </id>

customer class:
Code:
   private int id;

public int ID
        {
            get { return id; }
            set { id = value; }
        }


the exeption i get is:
NHibernate.PropertyNotFoundException: Could not find field 'iD' in class 'Order'.

any idea how to solve it ?

thanks for the help

ori


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 21, 2008 7:29 pm 
Newbie

Joined: Wed May 21, 2008 7:03 pm
Posts: 1
Put the column id into separate tag.

i.e.
Change this
Code:


      <id name="ID" column="ID" type="Int32" unsaved-value="0" >
         <generator class="identity" />         
      </id>



to smth like that:
Code:

<id name="ID" unsaved-value="0">
      <column name="ID"/>
      <generator class="identity"/>
    </id>



Top
 Profile  
 
 Post subject:
PostPosted: Fri May 23, 2008 2:29 pm 
Regular
Regular

Joined: Mon Mar 20, 2006 10:49 pm
Posts: 59
This thread raised a question: My understanding was that NH would only look for properties, not fields, be they public or private, unless access="field" was specified in the property mapping. A previous responder implied otherwise, hence my question.

Also, the problem with not finding property iD in class Order seems to be unrelated to the posted mapping. And putting the column into a separate element makes no semantic difference, AFAIK.

_________________
Mike Abraham


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