-->
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.  [ 6 posts ] 
Author Message
 Post subject: Getting error while implementing one ot many using collectio
PostPosted: Wed Aug 15, 2007 1:45 pm 
Newbie

Joined: Wed Aug 15, 2007 1:22 pm
Posts: 5
I am new to nhibernate and using it firsttime with .net2.0
I am trying to do a insert .
Any help or suggestion to do it different way is a welcome.

I have two tables Employee and Emp_Relations
Employee (EmpNum(PK),Age,FirstName,LAstname)
Emp_Relations(R_ID(PK indentity),EmpNum,R_Fname)

I have two classes


public class Employee
{
#region Private
private int _empnum;
private int _age;
private Name _name ;

private IList<EmpDependent> _dependents = new List<EmpDependent>();

#endregion

#region Constructor
public Employee()
{
_name = new Name();
}
#endregion

#region Public properties

public virtual int EmpNum
{
get{return _empnum;}
set{_empnum=value;}
}
public virtual int Age
{
get{return _age;}
set{_age=value;}
}
public virtual string EmpName
{
get{return _name.CompleteName;}

}
public virtual Name Name
{
get{return _name;}
set { _name = value; }
}

public virtual void AddDependent(EmpDependent d)
{
_dependents.Add(d);
}
private IList<EmpDependent> Dependents
{
get { return _dependents; }
set { _dependents = value; }
}




#endregion


}

Second class
public class EmpDependent
{
#region Private
private int _id;
private int _empnum;
private string _fname;

#endregion
#region Constructor
public EmpDependent(string fname)
{

_fname = fname;
}
public EmpDependent()
{

}
#endregion

public virtual int ID
{
get { return _id; }
set { _id = value; }

}
public virtual string Fname
{
get{return _fname;}
set{_fname = value;}
}
public virtual int EmpNum
{
get { return _empnum; }
set { _empnum = value; }
}
}



Hibernate version:1.2.0.4000

Mapping documents:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name ="DomainModel.Employee,DomainModel" table="Employee">
<id name ="EmpNum" type ="int">
<generator class="native" />
</id>

<property name="Age" column ="age" type="int" />
<component name ="Name" class="DomainModel.Name,DomainModel">
<property name ="FirstName" column ="FirstName" type ="string" length ="50" />
<property name ="LastName" column ="LastName" type ="string" length ="50" />

</component>

<set name="Dependents" inverse="true" lazy="true">
<key column="EmpNum"/>
<one-to-many class="DomainModel.EmpDependent,DomainModel"/>
</set>
</class>


<class name="DomainModel.EmpDependent,DomainModel" table ="Emp_Relations" >
<id name="ID" type ="int" column ="R_ID">
<generator class="native"/>
</id>
<property name="Fname" type ="string" column ="R_Fname" />
<property name="EmpNum" type ="int" column ="EmpNum" />
<many-to-one name="EmpNum" class="DomainModel.Employee,DomainModel" column="EmpNum" not-null="true"/>
</class>


</hibernate-mapping>



Full stack trace of any exception that occurs:
Test method UnitTest.EmployeeRepositoryTest.AddEmployee threw exception: NHibernate.ADOException: Could not save object ---> System.InvalidCastException: Unable to cast object of type 'System.Collections.Generic.List`1[DomainModel.EmpDependent]' to type 'Iesi.Collections.Generic.ISet`1[DomainModel.EmpDependent]'..

Name and version of the database you are using:
SQlServer 2005


Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 15, 2007 5:33 pm 
Newbie

Joined: Sun Aug 12, 2007 2:34 pm
Posts: 8
Location: Geneva, IL USA
Try using a bag instead of the set to map your collection. If you use set, your collection needs to be of type ISet.

LH


Top
 Profile  
 
 Post subject: Bag gives casting error
PostPosted: Thu Aug 16, 2007 9:51 am 
Newbie

Joined: Wed Aug 15, 2007 1:22 pm
Posts: 5
If I use bag then I get following error

Test method UnitTest.EmployeeRepositoryTest.AddEmployee threw exception: NHibernate.MappingException: Invalid mapping information specified for type DomainModel.Employee, check your mapping file for property type mismatches ---> System.InvalidCastException: Unable to cast object of type 'NHibernate.Collection.Generic.PersistentGenericBag`1[DomainModel.EmpDependent]' to type 'System.Collections.Generic.List`1[DomainModel.EmpDependent]'..

It gives me casting error.

I did some changes to my code.Earlier I was trying to make a bidirectional but I changed mapping and inserting simple one to many the way it is mentioned in help document. I am using list now in the mapping file and defined an index column too.
Here is
Changed Employee class
public class Employee
{
#region Private
private int _empnum;
private int _age;
private Name _name ;

private List<EmpDependent> _dependents = new List<EmpDependent>();

#endregion

#region Constructor
public Employee()
{
_name = new Name();
}
#endregion

#region Public properties

public virtual int EmpNum
{
get{return _empnum;}
set{_empnum=value;}
}
public virtual int Age
{
get{return _age;}
set{_age=value;}
}
public virtual string EmpName
{
get{return _name.CompleteName;}

}
public virtual Name Name
{
get{return _name;}
set { _name = value; }
}

// public virtual void AddDependent(EmpDependent d)
// {
// _dependents.Add(d);
// }
public virtual List<EmpDependent> Dependents
{
get { return _dependents; }
set { _dependents = value; }
}




#endregion


}
Changed EmployeeDependent class

public class EmpDependent
{
#region Private
private int _id;
//private int _empnum;
private string _fname;
private Employee _employee;

#endregion
#region Constructor
public EmpDependent(string fname)
{

_fname = fname;
}
public EmpDependent()
{

}
#endregion

public virtual int ID
{
get { return _id; }
set { _id = value; }

}
public virtual string Fname
{
get{return _fname;}
set{_fname = value;}
}
//public virtual Employee Employee
//{
// get { return _employee; }
// set { _employee = value; }
//}

}
mapping file look like
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name ="DomainModel.Employee,DomainModel" table="Employee">
<id name ="EmpNum" type ="int">
<generator class="native" />
</id>

<property name="Age" column ="age" type="int" />
<component name ="Name" class="DomainModel.Name,DomainModel">
<property name ="FirstName" column ="FirstName" type ="string" length ="50" />
<property name ="LastName" column ="LastName" type ="string" length ="50" />

</component>

<list name="Dependents" inverse="true" lazy="true">
<key column="EmpNum"/>
<index column="EmpNum" />
<one-to-many class="DomainModel.EmpDependent,DomainModel"/>
</list>
</class>


<class name="DomainModel.EmpDependent,DomainModel" table ="Emp_Relations" >
<id name="ID" type ="int" column ="R_ID">
<generator class="native"/>
</id>
<property name="Fname" type ="string" column ="R_Fname" />

<!--
<many-to-one name="Employee" class="DomainModel.Employee,DomainModel" column="EmpNum" not-null="true"/>
-->
</class>
</hibernate-mapping>

Now error coming is
Test method UnitTest.EmployeeRepositoryTest.AddEmployee threw exception: NHibernate.MappingException: Repeated column in mapping for collection: DomainModel.Employee.Dependents column: EmpNum.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 16, 2007 10:15 am 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
Use IList<T> instead of List<T>. It's a requirement of NHibernate due to lazy loading and all that fun stuff.

_________________
Karl Chu


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 16, 2007 11:11 am 
Newbie

Joined: Wed Aug 15, 2007 1:22 pm
Posts: 5
If I use IList<t>

then I cannot use Ilist<T> = new IList<t>

So if I use IList<t> _dependents

and use

public virtual IList<EmpDependent> Dependents
{
get { return _dependents; }
set { _dependents = value; }
}

So when I execute

session.save(emp)

it gives me null reference error.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 16, 2007 1:39 pm 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
On construction (or in your getter, upon determining that the IList is null) initialize your IList to a List. NH will change it to the collection class it requires for persistent bags when it needs to.


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