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.  [ 2 posts ] 
Author Message
 Post subject: 'NHibernate.Collection.Bag' cannot be convert to .....
PostPosted: Tue Apr 10, 2007 5:40 am 
Newbie

Joined: Tue Apr 10, 2007 5:25 am
Posts: 1
Hi,
I am using Spring.net and NHibernate with MySql database. I am able Insert, update and delete the records. The problem is with the join querries or with the tables which have referenced in other table. The join query i am using is "IList list = HibernateTemplate.Find("SELECT a FROM Aboutus a WHERE a.Id=?", 1);" It give me below error:
Object of type 'NHibernate.Collection.Bag' cannot be converted to type 'System.Collections.Generic.IList`1[TFAC.TFACAdmin.Domain.Aboutusdetail]

mapping file :

class name="Aboutus" table="aboutus">

<id name="Id" column="Id" type="Int32" unsaved-value="0">
<generator class="native"/>
</id>
<bag name="AboutusdetailList" inverse="true" lazy="true" cascade="none">
<key column="AboutUsId" />
<one-to-many class="Aboutusdetail" />
</bag>
<property column="CenterColumnImage" type="String" name="CenterColumnImage" length="100" />

</class>


The base class for AboutUs is:


#region Private Members

private int id;
private IList<Aboutusdetail> aboutusdetailList;
private string centerColumnImage;
#endregion

#region Constructor
/// <summary>
/// default constructor
/// </summary>
public AboutusBase()
{
this.aboutusdetailList = new List<Aboutusdetail>();
}
#endregion // End of Default ( Empty ) Class Constuctor

#region Public Properties

/// <summary>
/// Gets or sets the id
/// </summary>
/// <value>The id.</value>
public virtual int Id
{
get { return this.id; }
set
{
this.id = value;
}

}

/// <summary>
/// Gets or sets the aboutusdetailList
/// </summary>
/// <value>The aboutusdetailList.</value>
public virtual IList<Aboutusdetail> AboutusdetailList
{
get
{
return this.aboutusdetailList;
}
set
{
this.aboutusdetailList = value;
}
}

/// <summary>
/// Gets or sets the centerColumnImage
/// </summary>
/// <value>The centerColumnImage.</value>
public virtual string CenterColumnImage
{
get { return this.centerColumnImage; }

set
{
if( value != null && value.Length > 100)
throw new ArgumentOutOfRangeException("Invalid value for CenterColumnImage", value, value.ToString());

this.centerColumnImage = value;
}
}


#endregion

The mapping file for aboutusdetail is:

<class name="Aboutusdetail" table="aboutusdetail">

<id name="Id" column="Id" type="Int32" unsaved-value="0">
<generator class="native"/>
</id>
<many-to-one name="Aboutus" column="AboutUsId" class="Aboutus" />
<property column="Description" type="String" name="Description" length="1000" />
<property column="ImageTitle" type="String" name="ImageTitle" length="100" />
<property column="Image" type="String" name="Image" length="100" />
<property column="Line" type="Boolean" name="Line" />
<property column="BoldText" type="String" name="BoldText" length="100" />
<property column="CreatedOn" type="DateTime" name="CreatedOn" />
<property column="UpdatedBy" type="Int32" name="UpdatedBy" />
<property column="UpdatedOn" type="DateTime" name="UpdatedOn" />

</class>

The base class for AboutUsDetail is:

#region Private Members

private int id;
private Aboutus aboutus;
private string description;
private string imageTitle;
private string image;
private bool line;
private string boldText;
private DateTime createdOn;
private int updatedBy;
private DateTime updatedOn;
#endregion

#region Constructor
/// <summary>
/// default constructor
/// </summary>
public AboutusdetailBase()
{
}
#endregion // End of Default ( Empty ) Class Constuctor

#region Public Properties

/// <summary>
/// Gets or sets the id
/// </summary>
/// <value>The id.</value>
public virtual int Id
{
get { return this.id; }
set
{
this.id = value;
}

}

/// <summary>
/// Gets or sets the aboutus
/// </summary>
/// <value>The aboutus.</value>
public virtual Aboutus Aboutus
{
get { return this.aboutus; }
set
{
if( value == null )
throw new ArgumentOutOfRangeException("Null value not allowed for Aboutus", value, "null");

this.aboutus = value;
}

}

/// <summary>
/// Gets or sets the description
/// </summary>
/// <value>The description.</value>
public virtual string Description
{
get { return this.description; }

set
{
if( value != null && value.Length > 1000)
throw new ArgumentOutOfRangeException("Invalid value for Description", value, value.ToString());

this.description = value;
}
}

/// <summary>
/// Gets or sets the imageTitle
/// </summary>
/// <value>The imageTitle.</value>
public virtual string ImageTitle
{
get { return this.imageTitle; }

set
{
if( value != null && value.Length > 100)
throw new ArgumentOutOfRangeException("Invalid value for ImageTitle", value, value.ToString());

this.imageTitle = value;
}
}

/// <summary>
/// Gets or sets the image
/// </summary>
/// <value>The image.</value>
public virtual string Image
{
get { return this.image; }

set
{
if( value != null && value.Length > 100)
throw new ArgumentOutOfRangeException("Invalid value for Image", value, value.ToString());

this.image = value;
}
}

/// <summary>
/// Gets or sets the line
/// </summary>
/// <value>The line.</value>
public virtual bool Line
{
get { return this.line; }
set
{
this.line = value;
}

}

/// <summary>
/// Gets or sets the boldText
/// </summary>
/// <value>The boldText.</value>
public virtual string BoldText
{
get { return this.boldText; }

set
{
if( value != null && value.Length > 100)
throw new ArgumentOutOfRangeException("Invalid value for BoldText", value, value.ToString());

this.boldText = value;
}
}

/// <summary>
/// Gets or sets the createdOn
/// </summary>
/// <value>The createdOn.</value>
public virtual DateTime CreatedOn
{
get { return this.createdOn; }
set
{
this.createdOn = value;
}

}

/// <summary>
/// Gets or sets the updatedBy
/// </summary>
/// <value>The updatedBy.</value>
public virtual int UpdatedBy
{
get { return this.updatedBy; }
set
{
this.updatedBy = value;
}

}

/// <summary>
/// Gets or sets the updatedOn
/// </summary>
/// <value>The updatedOn.</value>
public virtual DateTime UpdatedOn
{
get { return this.updatedOn; }
set
{
this.updatedOn = value;
}

}


#endregion


Can anybody help me to get out of this?
Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 10, 2007 2:57 pm 
Beginner
Beginner

Joined: Tue Jan 02, 2007 5:53 pm
Posts: 42
Location: Bergen, Norway
Hi!

I think your problem is similar to casting a List to an IList<string>. This is not possible because the two types are unrelated, and it does not matter what they contain (and you have no guarantee that the List only contains strings either...).

Anyway, you might solve your problem by not using generics or you could switch to the correct Spring.Data.Nhibernate dll. The one with generic support is called "Spring.Data.Nhibernate12.dll".

If you are using HibernateTemplate with generics support then your find would look like this:
Code:
IList<Aboutusdetail> list = HibernateTemplate.Find<Aboutusdetail>("SELECT a FROM Aboutus a WHERE a.Id=?", 1);


You can also take a look at this thread in the Spring.Net forum:
http://forum.springframework.net/showthread.php?t=686

_________________
Cheers,
Steinar.


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