views:

160

answers:

1

Hello,

I'm a new NHibernate developer. I'm using attributes and not map files and I have configured the application to create the tables automatically.

I Have two classes , Group and User.

Withing the Group class I have a list of users

public class Group
{
    [NHibernate.Mapping.Attributes.Id(Name = "GroupId")]
    [NHibernate.Mapping.Attributes.Generator(Class = "guid")]
    public virtual Guid GroupId { get; set; }
    // What Attributes do I place here
    public virtual List<User> Users { get; set; }
}

I can't find the right attributes so that there will be two tables that have one to many relation.

Can anyone help?

Thanks, Ronny

+1  A: 

[ManyToMany], [OneToMany] or [ManyToOne] (those linked docs are fairly useless though) depending on how you want it setup. Probably [OneToMany], and then the same on a User.

You could avoid the pain by using the Fluent NHibernate library instead, if you haven't already tried it.

Chris S
Thanks for your replay, I'm tried to use [OneToMany] but the users wasn't saved. Then I tried to add the [List] [Key] [Index] and I got two types of errors."Unexpected row count: 0; expected: 1""object references an unsaved transient instance..."
Ronny
Have you saved the User first? And (for NH2.x) is it inside a transaction?
Chris S
thanks, that was the problem :)Is there a way to tell Hibernate to save the children first automatically?
Ronny
Not that I know of, one of the Cascade options may do it though: http://ayende.com/Blog/archive/2006/12/02/NHibernateCascadesTheDifferentBetweenAllAlldeleteorphansAndSaveupdate.aspx
Chris S