0

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 Answer 1

2

[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.

Sign up to request clarification or add additional context in comments.

4 Comments

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..."
Have you saved the User first? And (for NH2.x) is it inside a transaction?
thanks, that was the problem :) Is there a way to tell Hibernate to save the children first automatically?
Not that I know of, one of the Cascade options may do it though: ayende.com/Blog/archive/2006/12/02/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.