1

I have a class structure which is akin to a PurchaseOrder (parent) and PurchaseOrderLine (child) pattern, where the order lines will only be saved to the DB by saving the parent PurchaseOrder and will only ever be accessed via the parent too.

The DB has PurchaseOrderLine.PurchaseOrder set to not permit null values.

It seems from searching through the web that it is not possible to have a uni-directional association from PurchaseOrder via an IList property without having to have a property on the line pointing back when the child has a NOT NULL constraint for its PurchaseOrder column.

Is this really the case? It seems like one of the most basic things one would want to do with an ORM, I find it difficult to accept that a product as mature as NHibernate cannot handle such a basic scenario.

Peter Morris
  • 20,174
  • 9
  • 81
  • 146

3 Answers3

0

This can now be achieved in NH3 using the Not.KeyNullable()

        this.HasMany(x => x.Actions)
            .Access.BackingField()
            .KeyColumn("[Application]")
            .Not.KeyNullable()
            .Cascade.AllDeleteOrphan();
Peter Morris
  • 20,174
  • 9
  • 81
  • 146
0

No it's not the case. Please see the example provided in the answer to this question: When to use inverse=false on NHibernate / Hibernate OneToMany relationships?

Community
  • 1
  • 1
Phil Degenhardt
  • 7,215
  • 3
  • 35
  • 46
  • The answer you linked says that a uni-directional association must have Child.ParentID column set to allow NULL, I am attempting a uni-directional association where the parent has a list of children and the Child.ParentID column does NOT allow null values. – Peter Morris Oct 29 '11 at 08:37
  • Yes that's right. The column needs to allow null values but you do not need "to have a property on the line pointing back". – Phil Degenhardt Oct 29 '11 at 11:44
0

Well, it may be the case that you can't have unidirectional one-to-many relationship defined only on one side, but I'll argue with your statement that this is "one of the most basic things one would want to do with an ORM".

One of the most basic things would be to have unidirectional one-to-many defined only on many side - as it is natural for RDBM tables. And ORMs (despite the common misconception) are not intended (or able) to fully abstract domain model from underlying data source. Even if in some cases they can, the database side suffers from select N+1 problems or very ineffective queries.

Defining one-to-many at one side makes an impression that i.e. counting the collection is cheap. It is the case with plain object graphs, but not with NHibernate entities, as reading collection causes (at least one) call to the database. Eager fetching from one side is also not able to properly use database join mechanism in the way it's intended to be used (opposite to eager fetch from many side).

Even if I don't agree with a lot of arguments, I think it is useful to read some of the articles saying that "ORM is an anti-pattern", like this one. They helped me to leverage the way I think about ORMs and make me think about ORMs as a compromise between two not matching paradigms, but not the way to hide one behind another.

NOtherDev
  • 9,542
  • 2
  • 36
  • 47
  • It's more common to have a purchase order know about its lines than it is for a purchase order line to know about its parent. With the correct mapping the whole structure can be retrieved as a single select with a join. The DB would obviously need to be structured the other way around, but that's a different issue. – Peter Morris Oct 30 '11 at 17:08
  • The structure you propose can be retrieved as single select, but quite odd one for the database. Anyway, just make the relationship bidirectional, set `inverse` properly, make property on *one* side private and you'll have the best compromise you can have in this case. – NOtherDev Oct 30 '11 at 17:31
  • The whole point is that I do not want a bi-directional relationship, I want a uni-directional relationship from parent to child. I don't want to have to litter my model with unwanted properties to satisfy my ORM. I am really surprised NH cannot do this. – Peter Morris Oct 31 '11 at 08:39