views:

158

answers:

1

Hi,
My bean looks like this:

public class A {  
    ...  
    [HasMany (MapType = typeof(B), Table = "B_table", ColumnKey = "A_object_id",  
              Fetch = FetchEnum.Join,  
              RelationType = RelationType.List, Index = "id",  
              Cascade = ManyRelationCascadeEnum.AllDeleteOrphan)]

    IList<B> BList { get; set; }
    ...
}

and when perform Save on this bean I expect that beans of type B will be automatically saved (and deleted on update) too. NHibernate surely is trying that, but it does so with B_table.A_object_id set to NULL first and then NHibernate updates B_table setting the proper B_table.A_object_id value (that is: A.ID).
This is not what I want, as I have a NOT NULL constraint in the database.
My question is: how to make NHibernate automatically save the child objects with the proper ID set from the start? I know I can create A bean, save it, get it's brand new ID, create B beans, set their A_object_id and then save B beans... but it's a workaround.

A: 

Unidirectional relationships (in which only the parent knows about the child) always result in an update for setting the Id. I'm not sure why and it doesn't make a lot of sense to me either but that's just how NHibernate works.

You need to create a bidirectional relationship where the HasMany would have an Inverse = true and B would have a reference to class A in it (which should be populated when you add B to the A collection.

ShaneC
Actually, I can't do that.The B class cannot have a reference to A, as the mapping is not so simple.The B class in it's B_table.A_object_id may also reference an object from table A2_table. That is, there is another class A2 which HasMany B objects. I have a proper CHECK condition in the database validating such a relation. From the B class perspective I don't need to know where does it belong to.
wysek
Then the only other option that I know of (after quite a bit of research) is to remove the null constraint.
ShaneC
did u solve your matter? i've fighting with something similar to :(
frabiacca