To be more concrete. What is the best way to check if an Post is assigned to an Blog in and preInsert event?
-Steve Steve Wagner schrieb: > Hi, at first my example mapping: > > <class name="Blog"> > <id name="Id"> > <generator class="guid.comb" /> > </id> > <set name="Posts" cascade="all"> > <key column="Blog_Id" not-null="true" /> > <one-to-many class="Post" /> > </set> > </class> > > <class name="Post"> > <id name="Id"> > <generator class="guid.comb" /> > </id> > </class> > > Ive choose to use an Uni-Directional association, because the model is > cleaner and i don't have to manage the other direction. > > So my problem is that i want to allow this > > blog.Posts.Add(new Post()); > > but when i do > > session.Save(new Post()); > > NHibernate should throw an exception since the Post have no Blog > assigned to it. > > Since database not-null constrains dose not work as described in > documentation: > > Very Important Note: If the <key> column of a <one-to-many> > association is declared NOT NULL, NHibernate may cause constraint > violations when it creates or updates the association. To prevent this > Problem, you must use a bidirectional association with the many valued > end (the set or bag) marked as inverse="true". See the discussion of > bidirectional associations later in this chapter. > > Because it dose the insert first with the Blog_Id null > and after that it it updates the Database with the Blog_Id. > > So my question is, is there any generic way to prevent NHibernate from > inserting Posts without an associated Blog? I think about creating an > event, but i am not sure which is the best strategy to detect this an a > generic way. > > And second, since the database way is not supported, isnt it a good idea > to add something like a soft constraint where nhibernate checks if it > has an association and throw before the transaction is committed. > > Regards, > Steve > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "nhusers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/nhusers?hl=en -~----------~----~----~----~------~----~------~--~---
