-->
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.  [ 3 posts ] 
Author Message
 Post subject: ManyToMany attribute examples?
PostPosted: Mon Apr 16, 2007 12:39 am 
Newbie

Joined: Mon Apr 16, 2007 12:32 am
Posts: 1
Hello, all!

I'm new to NHibernate, and I have the Java Persistence with Hibernate book, but it certainly has its limitations as a C# reference for a new user. Is there a good source of information and examples of mappings using attributes--particularly the ManyToMany--that anyone can recommend? I've searched, but about all I can find are the XML based mappings... it's hard to find specifics in the noise.

If it matters, I am using 1.2RC2, and the simple mappings are working great for me.

Thanks in advance for any help.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 18, 2007 5:19 pm 
Newbie

Joined: Tue Mar 27, 2007 4:45 pm
Posts: 9
Hi,

perhaps you could post more details. For example the two classes with properties you want to connect


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 18, 2007 7:51 pm 
Regular
Regular

Joined: Tue Dec 02, 2003 6:25 pm
Posts: 61
Location: Dallas, TX
If you check out the source for NH, there is a project called NHibernate.Mapping.Attributes.Test that contains a sample file (Domain.cs) that probably uses every available attribute.

Here's one sample.

It isn't pretty, but this is working great for me in a current project that uses 1.2.0b3.

This is for my Group class, which has a many-to-many relationship to users.
Code:
        private IList<User> _members;

        [
            NHMA.Bag(1,
                Inverse = true,
                Lazy = true,
                Table = "UserGroups",
                Cascade=NHMA.CascadeStyle.SaveUpdate),
            NHMA.Key(2, Column = "[Group]"),
            NHMA.ManyToMany(3,
                Column="[User]",
                ClassType=typeof(User))
        ]
        public virtual IList<User> Members
        {
            get { return _members; }
            set { _members = value; }
        }


And here's what the flip side of the relationship looks like (this is the Groups property of the User class)

Code:
        private ISet<Group> _groups;

        [
            NHMA.Set(1,
                Inverse = false,
                Lazy=true,
                Table="UserGroups"),
            NHMA.Key(2, Column = "[User]"),
            NHMA.ManyToMany(3,
                Column = "[Group]",
                ClassType=typeof(Group)),
            NHMA.Cache(4,
                Usage=NHMA.CacheUsage.ReadWrite)
        ]
        public virtual ISet<Group> Groups
        {
            get { return _groups; }
            set { _groups = value; }
        }


Why did I use an ISet for one side of the relationship and IList for the other? I don't remember. I heard Sets were more efficient to update than Bags, but I could probably use ISet for both properties.


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