Mapping by Code

105 views
Skip to first unread message

Tiago Azevedo

unread,
Sep 30, 2013, 4:07:15 PM9/30/13
to nhu...@googlegroups.com
Someone could tell how to perform the mapping of this class?

public class MyClass
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual Role Parent { get; set; } // or public virtual int ParentId { get; set; } ???
public virtual IList<MyClass> ListClass{ get; set; }
 }

Ricardo Peres

unread,
Oct 1, 2013, 7:48:47 AM10/1/13
to nhu...@googlegroups.com
Sure:

var mapper = new ModelMapper();
mapper.Class<MyClass>(ca =>
{
ca.Id(x => x.Id, map =>
{
map.Column("myclass_id");
map.Generator(Generators.Identity);
});
ca.Property(x => x.Name, a =>
{
//fill in the blanks
});
ca.ManyToOne(c => c.Parent, a =>
{
map.Column("parent_id");
//fill in the blanks
});
ca.Bag(c => c.ListClass, c =>
{
c.Key(x => x.Column("myclass_id"));
c.Inverse(true);
//fill in the blanks
}, c => c.OneToMany());
}

What exactly is your problem?

RP

Tiago Azevedo

unread,
Oct 1, 2013, 8:11:38 AM10/1/13
to nhu...@googlegroups.com
My intention is to build a hierarchical tree structure for controlling user access

Tiago Azevedo

unread,
Oct 1, 2013, 8:48:07 AM10/1/13
to nhu...@googlegroups.com
I managed to create the table in the database with this mapping, however the list in my repository back to the property Roles own class instead of their children.
I made only one Session.Query <role> (). I would have to do some treatment to bring the list correctly?

Ricardo Peres

unread,
Oct 6, 2013, 12:24:17 PM10/6/13
to nhu...@googlegroups.com
Tiago,

I can't understand you, can you explain better?

RP

Tab

unread,
Oct 7, 2013, 9:02:08 AM10/7/13
to nhu...@googlegroups.com
I want to build a hierarchical tree

Like

-Node
         .Node(leaf)
         .Node(leaf)
         -Node
                     .Node(leaf)
                     .Node(leaf)
-Node
         .Node(leaf)
         .Node(leaf)

To have this I thought in the class shown in the first post, theoretically work since I can do the mapping correctly.


2013/10/6 Ricardo Peres <rjp...@gmail.com>

--
You received this message because you are subscribed to a topic in the Google Groups "nhusers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/nhusers/qIJlAN0vuXM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to nhusers+u...@googlegroups.com.
To post to this group, send email to nhu...@googlegroups.com.
Visit this group at http://groups.google.com/group/nhusers.
For more options, visit https://groups.google.com/groups/opt_out.



--
Tiago Azevedo Borges
Goiânia - Goiás

Ricardo Peres

unread,
Oct 7, 2013, 1:31:09 PM10/7/13
to nhu...@googlegroups.com
It does. You will need to mark the Parent property as not required (for the top-level nodes).
What problems are you facing?

RP

Tab

unread,
Oct 8, 2013, 8:07:16 AM10/8/13
to nhu...@googlegroups.com
Got it!

My class:

public class Role : EntityBase<Role, int>
    {
        public virtual string Name { get; set; }
        public virtual bool Checked { get; set; }
        public virtual Role Parent { get; set; }
        public virtual IList<Role> Roles { get; set; }
    }

and my map:

public class RoleMap : ClassMapping<Role>
    {
        public RoleMap()
        {
            Table("ROLE");

            Id(x => x.Id);

            Property(x => x.Name, m =>
            {
                m.Column("name");
                m.NotNullable(true);
            });

            Property(x => x.Checked, m =>
            {
                m.Column("checked");
                m.NotNullable(true);
            });

            ManyToOne(x => x.Parent, m =>
                                         {
                                             m.Column("parent_id");
                                             m.NotNullable(false);
                                         });

            Bag(x => x.Roles, m =>
                                  {
                                      m.Key(k => k.Column("parent_id"));
                                      m.Inverse(true);
                                      m.Cascade(Cascade.DeleteOrphans);
                                      m.Lazy(CollectionLazy.NoLazy);
                                  }, r => r.OneToMany());
        }
    }

Thanks for helping


2013/10/7 Ricardo Peres <rjp...@gmail.com>

Tiago Azevedo

unread,
Oct 8, 2013, 12:47:27 PM10/8/13
to nhu...@googlegroups.com
I celebrated too soon.

The way I did or the way you suggested is not working.


Em segunda-feira, 30 de setembro de 2013 17h07min15s UTC-3, Tiago Azevedo escreveu:

Tiago Azevedo

unread,
Oct 8, 2013, 1:16:35 PM10/8/13
to nhu...@googlegroups.com

I'll try to explain my situation, sorry if the formulation of the phrases get bad, I'm using a translator.


I have a user that has a profile, this profile can be Administrator, Manager, Employee, etc.. Each profile has a list of roles and each role represents an access to a certain page. Each role has a list of other roles. The roles forms a hierarchical tree structure(See image below).

What I need is to set classes and mappings.

Ricardo Peres

unread,
Oct 9, 2013, 5:40:37 AM10/9/13
to nhu...@googlegroups.com
Tiago,

You are not explaining what the problem is. Do you have an exception when building the session factory, or when querying, or doesn't the query bring what you expected? Can you see the generated SQL?

RP

Tab

unread,
Oct 9, 2013, 8:38:52 AM10/9/13
to nhu...@googlegroups.com
The problem is ... I can not get the correct mapping for my class role.

The problem is ... I can not get the correct mapping for my role class. The mapping you suggested in your first reply does not bring what I want correctly, the parent he brings himself and the list is empty.


2013/10/9 Ricardo Peres <rjp...@gmail.com>

--
You received this message because you are subscribed to a topic in the Google Groups "nhusers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/nhusers/qIJlAN0vuXM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to nhusers+u...@googlegroups.com.
To post to this group, send email to nhu...@googlegroups.com.
Visit this group at http://groups.google.com/group/nhusers.
For more options, visit https://groups.google.com/groups/opt_out.

Ricardo Peres

unread,
Oct 10, 2013, 9:34:20 AM10/10/13
to nhu...@googlegroups.com
Tiago:

This class:

public class Role
{
	public Role()
	{
		this.Roles = new List<Role>();
	}
	
public virtual int Id { getset; }
	public virtual string Name { getset
; }
	public virtual bool Checked { getset; }
	
public virtual Role Parent { getset
; }
	
public virtual IList<Role> Roles { getset; } 
}

And this mapping:

public class RoleMap : ClassMapping<Role>
{
	public
 RoleMap()
	{
		this.Table("ROLE");
 
		this.Id(x => x.Id,
		m => m.Generator(Generators.HighLow));
 
		this.Property(x => x.Name,
		m =>
		{
			m.Column("name");
			m.NotNullable(true);
		});
 
		this.Property(x => x.Checked,
		m =>
		{
			m.Column("checked");
			m.NotNullable(true);
		});
 
		this.ManyToOne(x => x.Parent, m =>
		{
			m.Column("parent_id");
			m.NotNullable(false);
		});
 
		this.Bag(x => x.Roles, m =>
		{
			m.Key(k => k.Column("parent_id"));
			m.Inverse(true);
			m.Cascade(Cascade.All | Cascade.DeleteOrphans);
			m.Lazy(CollectionLazy.NoLazy);
		},
		r => r.OneToMany());
	}
}


And these tests:

using (var session = sessionFactory.OpenSession())
using (session.BeginTransaction())
{
	var parent = new Role { Checked = true, Name = "Parent" };
	var child = new Role { Parent = parent, Name = "Child" };
 
	parent.Roles.Add(child);
 
	session.Save(parent);

	session.Flush();
 
	var roles = session.Query<Role>().ToList();
 
	Debug.Assert(roles != null);
 
	Debug.Assert(roles.Count == 2);
 
	Debug.Assert(roles[0].Parent == null);
					
	Debug.Assert(roles[1].Parent == roles[0]);
 
	Debug.Assert(roles[0].Roles.Contains(roles[1]));
}

All work perfectly. What I had to do was set the generator to something (HiLo, in this case).

RP

Tab

unread,
Oct 10, 2013, 9:59:53 AM10/10/13
to nhu...@googlegroups.com
With me the following error occurred while saving the parent

object references an unsaved transient instance - save the transient instance before flushing or set cascade action for the property to something that would make it autosave. Type: Role, Entity: Role


2013/10/10 Ricardo Peres <rjp...@gmail.com>

Ricardo Peres

unread,
Oct 10, 2013, 12:40:25 PM10/10/13
to nhu...@googlegroups.com
That's because on the cascade you don't have Cascade.All | Cascade.DeleteOrphans.

RP

Tab

unread,
Oct 10, 2013, 1:25:48 PM10/10/13
to nhu...@googlegroups.com
Working with Cascade.All ... tnks Ricardo


2013/10/10 Ricardo Peres <rjp...@gmail.com>
Reply all
Reply to author
Forward
0 new messages