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 <[email protected]> > Tiago: > > This class: > > public class Role > { > public Role() > { > this.Roles = new List<Role>(); > } > > public virtual int Id { get; set; } > 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 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 > > > On Wednesday, October 9, 2013 1:38:52 PM UTC+1, Tiago Azevedo wrote: > >> 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 <[email protected]> >> >>> 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 >>> >>> >>> On Tuesday, October 8, 2013 6:16:35 PM UTC+1, Tiago Azevedo wrote: >>>> >>>> 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). >>>> >>>> >>>> <https://lh4.googleusercontent.com/-QD-a5q-qixE/UlQ8wsC1K1I/AAAAAAAAAVc/IzBh1g9mD_I/s1600/TriStateTreeView.png> >>>> What I need is to set classes and mappings. >>>> >>> -- >>> 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<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 [email protected]. >>> >>> Visit this group at >>> http://groups.google.com/**group/nhusers<http://groups.google.com/group/nhusers> >>> . >>> For more options, visit >>> https://groups.google.com/**groups/opt_out<https://groups.google.com/groups/opt_out> >>> . >>> >> >> >> >> -- >> Tiago Azevedo Borges >> Goiânia - Goiás >> > -- > 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 > [email protected]. > To post to this group, send email to [email protected]. > 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 -- You received this message because you are subscribed to the Google Groups "nhusers" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/nhusers. For more options, visit https://groups.google.com/groups/opt_out.
