NHibernate trickery: what happened to my associations?
I usually post things of moderate difficulty, let us see if you can not only figure this one out, but where that bug exists.
Given the following class & mapping:
public class User { public virtual string Username { get; set; } public virtual ICollection<string> AllowedPaths { get; set; } public User() { AllowedPaths = new HashSet<string>(); } }
<class name="User" table="Users"> <id name="Username"> <generator class="assigned"/> </id> <set name="AllowedPaths" table="UsersAllowPaths"> <key column="`User`" not-null="true"/> <element column="Path" type="System.String" not-null="true"/> </set> </class>
I have the following code:
public class Program { public static void Main() { Configuration configuration = new Configuration().Configure("nhibernate.config"); ISessionFactory sessionFactory = configuration.BuildSessionFactory(); new SchemaExport(configuration).Create(true, true); using (ISession s = sessionFactory.OpenSession()) using (ITransaction tx = s.BeginTransaction()) { s.Save(new User { Username = "ayende", AllowedPaths = {"/users", "/meetings"} }); tx.Commit(); } using (ISession s = sessionFactory.OpenSession()) using (ITransaction tx = s.BeginTransaction()) { var user = s.Get<User>("Ayende"); Console.WriteLine("Found user {0}", user.Username); Console.WriteLine("Allowed paths:"); foreach (string allowedPath in user.AllowedPaths) { Console.WriteLine("\t{0}", allowedPath); } tx.Commit(); } } }
First, and without running the code!
- What is the expected output?
- Am I doing something wrong here?
Then, run the code, and answer them again.
You can download the project here: http://github.com/ayende/NHibernate-Challanges, sub folder TrickyBug

Comments
Comment preview