Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7089951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:59:06+00:00 2026-05-28T07:59:06+00:00

I use nhibernate 3.2 mapping by code and I’ve a strange behaviour. If I

  • 0

I use nhibernate 3.2 mapping by code and I’ve a strange behaviour.

If I simplify my project, I have 2 tables :

[Serializable]
public class Intervention
{
    public Intervention()
    {
        ReponsePointVerification = new List<ReponsePointVerification>();
    }

    #region Public Properties
    public virtual int Id
    {
        get;
        set;
    }
    public virtual IList<ReponsePointVerification> ReponsePointVerification
    {
        get;
        set;
    }
}

public class InterventionMap : ClassMapping<Intervention>
{
    public InterventionMap()
    {
        Id<int>(x => x.Id, map =>
        {
            map.Generator(NHibernate.Mapping.ByCode.Generators.Identity);
        });
        Bag(x => x.ReponsePointVerification, map =>
        {
            map.Key( k => k.Column( "IdIntervention" ) );
        }); 
    }
}

and

[Serializable]
public class ReponsePointVerification
{
    #region Public Properties
    public virtual int Id
    {
        get;
        set;
    }
    public virtual Intervention Intervention
    {
        get;
        set;
    }
}

public class ReponsePointVerificationMap : ClassMapping<ReponsePointVerification>
{
    public ReponsePointVerificationMap()
    {
        Id<int>(x => x.Id, map =>
        {
            map.Generator(NHibernate.Mapping.ByCode.Generators.Identity);
        });
        ManyToOne<Intervention>(x => x.Intervention, map => map.Column("IdIntervention"));
    }
}

which represents two tables Intervention and ReponsePointVerification which has a foreign key (IdIntervention) which is linked to Intervention.

When I call :

        return (from interv in Session.Query<Intervention>()
                .Fetch(rep => rep.ReponsePointVerification)
                select interv).ToList();

or even when I don’t fetch the information. I has this error :

Could not cast the value in field id0_ of type Int32 to the Type SerializableType.  Please check to make sure that the mapping is correct and that your DataProvider supports this Data Type.

The sql query looks to be ok.

I found that post http://groups.google.com/group/nhusers/browse_thread/thread/ef137c3e5b66acdc

And I modify the InterventionMap class according to that post it will work :

public class InterventionMap : ClassMapping<Intervention>
{
    public InterventionMap()
    {
        Id<int>(x => x.Id, map =>
        {
            map.Generator(NHibernate.Mapping.ByCode.Generators.Identity);
        });
        Bag(x => x.ReponsePointVerification, map =>
        {
            map.Key( k => k.Column( "IdIntervention" ) );
        }, rm => rm.OneToMany()); 
    }
}

The only difference is the add of “, rm => rm.OneToMany()”

Trust me I’m very happy it worked but I really don’t understand that line can someone explain me the meaning ?

Regards

Edit

I made a WriteAllXmlMapping and the 2 codes are :

version without rm => rm.OneToMany() (which doesn’t work)

<bag name="ReponsePointVerification">
  <key column="IdIntervention" />
  <element type="Hyma.BusinessObjets.ReponsePointVerification" />
</bag>

version with rm => rm.OneToMany() (which work)

<bag name="ReponsePointVerification">
  <key column="IdIntervention" />
  <one-to-many class="ReponsePointVerification" />
</bag>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T07:59:07+00:00Added an answer on May 28, 2026 at 7:59 am

    OneToMany tells NH that this is a one-to-many relationship to another mapped entity. if left blank NH takes ElementCollection as default which is a one-to-many relationship to some ElementType without an id.

    for example when you have these classes

    class Parent
    {
        public ICollection<Child> Childs { get; set; }  <- one-to-many collection
        public ICollection<string> Names { get; set; }  <- element collection
    }
    
    class Child
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
    

    then NH would create these 3 tables

    ParentTable
    id
    
    ChildTable
    id | parent_id | name
    
    NamesTable
    parent_id | name
    

    or vice versa, when you have the 3 Tables, it would be reasonable to map it to the classes

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have started to use NHibernate 3.0 and PostgreSQL for a small project, so
I'm using mapping by code in NHibernate. I got a class with several properties.
I am new to nHibernate. I understand how to use mapping using Fluent nHibernate.
I'm trying to use NHibernate for data access, and I have 2 simple entities
For our test fixtures we use NHibernate to generate a database schema. We have
Just started developing a project using NHibernate and Fluent NHibernate for the mapping and
I am changing my application to use Fluent NHibernate. I have created my Fluent
I used NH 3.2 mapping by code and I tryied Nhibernate Mapping Generator http://nmg.codeplex.com/
Background: I'm getting a mapping failure when trying to use nHibernate. The application consists
I have to model this relationship in NHibernate (simplified the code a bit to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.