-->
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.  [ 1 post ] 
Author Message
 Post subject: ManyToMany relationship issue
PostPosted: Mon Jun 25, 2012 12:56 pm 
Newbie

Joined: Mon Jun 25, 2012 12:49 pm
Posts: 1
Dear All,

I have a problem when saving a object to the database. I'm using C# and Sql Server.
My domain objects are:

Code:
public class UserSpecialization : PersonSpecialization, IUserSpecialization
{
    public static readonly string SpecializationNameValue = "User";

    public UserSpecialization()
    {
        Roles = new Collection<IRole>();
    }

    public virtual IEnumerable<IRole> Roles
    {
        get;
        set;
    }
}

public abstract class PersonSpecialization : DomainObjectOfInt32, IPersonSpecialization
{
    public virtual IPerson Person { get; set; }
    public virtual string SpecializationName { get; protected set; }
}

public class Person : DomainObjectOfInt32, IPerson, IAuditable
{
    public Person()
    {
        Specializations = new List<IPersonSpecialization>();
    }

    public virtual IList<IPersonSpecialization> Specializations { get; protected set; }
}

public abstract class Role : DomainObjectOfInt32, IRole, IAuditable
{
    public Role(string roleName)
        : this()
    {
        Name = roleName;
    }
    protected Role()
    {
        AssignedUsers = new Collection<IUserSpecialization>();
    }

    public virtual ICollection<IUserSpecialization> AssignedUsers
    {
        get;
        protected set; }
}


My mapping is as follow:

Code:
public class PersonMap : ClassMap<Person>
{
    public PersonMap()
    {
        Id(x => x.Id).GeneratedBy.Identity();
        HasMany<PersonSpecialization>(c => c.Specializations)
            .Cascade.AllDeleteOrphan();
    }
}

public class RoleMap : ClassMap<Role>
{
    public RoleMap()
    {
        Id(x => x.Id).GeneratedBy.Identity();
        HasManyToMany<UserSpecialization>(x => x.AssignedUsers).Table("UserRole");
        DiscriminateSubClassesOnColumn("Type");
    }
}

public class PersonSpecializationMap : ClassMap<PersonSpecialization>
{
    public PersonSpecializationMap()
    {
        Table("PersonSpecialization");

        Id(c => c.Id).GeneratedBy.Identity();

        DiscriminateSubClassesOnColumn<string>("SpecializationName");

        Map(x => x.SpecializationName).ReadOnly();

        References<Person>(c => c.Person);
    }
}

public class UserSpecializationMap : SubclassMap<UserSpecialization>
{
   public UserSpecializationMap()
    {
        Table("User");

        DiscriminatorValue(UserSpecialization.SpecializationNameValue);

        Join("[User]", joined =>
        {
            joined.Map(c => c.UserName, "Username");

            joined.HasManyToMany<Role>(x => x.Roles)
                .Table("UserRole");
        });
    }
}



Now when i select a Person, i get also all his roles in Roles collection. Also when saving a Role, all linked Persons are nicely saved. But when i save a Person, with roles linked to it, i'm getting this exception:

Nhibernate.PropertyAccessException:Exception occurred getter of DO.DomainObjectOfInt32.Id. InnerException:Object does not match target type.

How can i solve this issue?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.