<p>Warning - I am very new to NHibernate. I know this question seems simple - and I'm sure there's a simple answer, but I've been spinning my wheels for some time on this one. I am dealing with a legacy db which really can't be altered structurally. I have a details table which lists payment plans that have been accepted by a customer. Each payment plan has an ID which links back to a reference table to get the plan's terms, conditions, etc. In my object model, I have an AcceptedPlan class, and a Plan class. Originally, I used a many-to-one relationship from the detail table back to the ref table to model this relationship in NHibernate. I also created a one-to-many relationship going in the opposite direction from the Plan class over to the AcceptedPlan class. This was fine while I was simply reading data. I could go to my Plan object, which was a property of my AcceptedPlan class to read the plan's details. My problem arose when I had to start inserting new rows to the details table. From my reading, it seems the only way to create a new child object is to add it to the parent object and then save the session. But I don't want to have to create a new parent Plan object every time I want to create a new detail record. This seems like unnecessary overhead. Does anyone know if I am going about this in the wrong way?</p>

[{'answer_id': 10993, 'author': 'DavidWhitney', 'author_id': 1297, 'author_profile': 'https://Stackoverflow.com/users/1297', 'pm_score': 0, 'selected': False, 'text': "<p>The approach I'd take to model this is as follows:</p>\n\n<p>Customer object contains an ICollection &lt;PaymentPlan&gt; PaymentPlans which represent the plans that customer has accepted.</p>\n\n<p>The PaymentPlan to the Customer would be mapped using a bag which uses the details table to establish which customer id's mapped to which PaymentPlans.  Using cascade all-delete-orphan, if the customer was deleted, both the entries from details and the PaymentPlans that customer owned would be deleted.</p>\n\n<p>The PaymentPlan object contains a PlanTerms object which represented the terms of the payment plan.</p>\n\n<p>The PlanTerms would be mapped to a PaymentPlan using a many-to-one mapping cascading save-update which would just insert a reference to the relevant PlanTerms object in to the PaymentPlan.</p>\n\n<p>Using this model, you could create PlanTerms independantly and then when you add a new PaymentPlan to a customer, you'd create a new PaymentPlan object passing in the relevant PlanTerms object and then add it to the collection on the relevant Customer.  Finally you'd save the Customer and let nhibernate cascade the save operation.</p>\n\n<p>You'd end up with a Customer object, a PaymentPlan object and a PlanTerms object with the Customer (customer table) owning instances of PaymentPlans (the details table) which all adhear to specific PlanTerms (the plan table).</p>\n\n<p>I've got some more concrete examples of the mapping syntax if required but it's probably best to work it through with your own model and I don't have enough information on the database tables to provide any specific examples.</p>\n"}, {'answer_id': 10996, 'author': 'lomaxx', 'author_id': 493, 'author_profile': 'https://Stackoverflow.com/users/493', 'pm_score': 0, 'selected': False, 'text': "<p>I don't know if this is possibly because my NHibernate experience is limited, but could you create a BaseDetail class which has just the properties for the Details as they map directly to the Detail table.</p>\n\n<p>Then create a second class that inherits from the BaseDetail class that has the additional Parent Plan object so you can create a BaseDetail class when you want to just create a Detail row and assign the PlanId to it, but if you need to populate a full Detail record with the Parent plan object you can use the inherited Detail class.</p>\n\n<p>I don't know if that makes a whole lot of sense, but let me know and I'll clarify further.</p>\n"}, {'answer_id': 11149, 'author': 'Jon Limjap', 'author_id': 372, 'author_profile': 'https://Stackoverflow.com/users/372', 'pm_score': 0, 'selected': False, 'text': "<p>I think the problem you have here is that your AcceptedOffer object contains a Plan object, and then your Plan object appears to contain an AcceptedOffers collection that contains AcceptedOffer objects. Same thing with Customers. The fact that the objects are a child of each other is what causes your problem, I think.</p>\n\n<p>Likewise, what makes your AcceptedOffer complex is it has a two responsibilities: it indicates offers included in a plan, it indicates acceptance by a customer. That violates the Single Responsibility Principle.</p>\n\n<p>You may have to differentiate between an Offer that is under a Plan, and an Offer that is accepted by customers. So here's what I'm going to do:</p>\n\n<ol>\n<li>Create a separate Offer object which does not have a state, e.g., it does not have a customer and it does not have a status -- it only has an OfferId and the Plan it belongs to as its attributes. </li>\n<li>Modify your Plan object to have an Offers collection (it does not have to have accepted offer in its context). </li>\n<li>Finally, modify your AcceptedOffer object so that it contains an Offer, the Customer, and a Status. Customer remains the same.</li>\n</ol>\n\n<p>I think this will sufficiently untangle your NHibernate mappings and object saving problems. :)</p>\n"}, {'answer_id': 11164, 'author': 'DavidWhitney', 'author_id': 1297, 'author_profile': 'https://Stackoverflow.com/users/1297', 'pm_score': 3, 'selected': True, 'text': '<p>I\'d steer away from having child object containing their logical parent, it can get very messy and very recursive pretty quickly when you do that.  I\'d take a look at how you\'re intending to use the domain model before you do that sort of thing.  You can easily still have the ID references in the tables and just leave them unmapped.</p>\n\n<p>Here are two example mappings that might nudge you in the right direction, I\'ve had to adlib table names etc but it could possibly help.  I\'d probably also suggest mapping the StatusId to an enumeration.</p>\n\n<p>Pay attention to the way the bag effectivly maps the details table into a collection.</p>\n\n<pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;\n&lt;hibernate-mapping default-cascade="save-update" xmlns="urn:nhibernate-mapping-2.2"&gt;\n    &lt;class lazy="false" name="Namespace.Customer, Namespace" table="Customer"&gt;\n        &lt;id name="Id" type="Int32" unsaved-value="0"&gt;\n            &lt;column name="CustomerAccountId" length="4" sql-type="int" not-null="true" unique="true" index="CustomerPK"/&gt;\n            &lt;generator class="native" /&gt;\n        &lt;/id&gt;\n\n        &lt;bag name="AcceptedOffers" inverse="false" lazy="false" cascade="all-delete-orphan" table="details"&gt;\n          &lt;key column="CustomerAccountId" foreign-key="AcceptedOfferFK"/&gt;\n          &lt;many-to-many\n            class="Namespace.AcceptedOffer, Namespace"\n            column="AcceptedOfferFK"\n            foreign-key="AcceptedOfferID"\n            lazy="false"\n           /&gt;\n        &lt;/bag&gt;\n\n  &lt;/class&gt;\n&lt;/hibernate-mapping&gt;\n\n\n&lt;?xml version="1.0" encoding="utf-8" ?&gt;\n&lt;hibernate-mapping default-cascade="save-update" xmlns="urn:nhibernate-mapping-2.2"&gt;\n    &lt;class lazy="false" name="Namespace.AcceptedOffer, Namespace" table="AcceptedOffer"&gt;\n        &lt;id name="Id" type="Int32" unsaved-value="0"&gt;\n            &lt;column name="AcceptedOfferId" length="4" sql-type="int" not-null="true" unique="true" index="AcceptedOfferPK"/&gt;\n            &lt;generator class="native" /&gt;\n        &lt;/id&gt;\n\n        &lt;many-to-one \n          name="Plan"\n          class="Namespace.Plan, Namespace"\n          lazy="false"\n          cascade="save-update"\n        &gt;\n        &lt;column name="PlanFK" length="4" sql-type="int" not-null="false"/&gt;\n        &lt;/many-to-one&gt;\n\n        &lt;property name="StatusId" type="Int32"&gt;\n            &lt;column name="StatusId" length="4" sql-type="int" not-null="true"/&gt;\n        &lt;/property&gt;\n\n  &lt;/class&gt;\n&lt;/hibernate-mapping&gt;\n</code></pre>\n'}, {'answer_id': 11166, 'author': 'Jon Limjap', 'author_id': 372, 'author_profile': 'https://Stackoverflow.com/users/372', 'pm_score': 0, 'selected': False, 'text': '<p>A tip that may (or may not) be helpful in NHibernate: you can map your objects against Views as though the View was a table. Just specify the view name as a table name; as long as all NOT NULL fields are included in the view and the mapping it will work fine.</p>\n'}, {'answer_id': 11176, 'author': 'DavidWhitney', 'author_id': 1297, 'author_profile': 'https://Stackoverflow.com/users/1297', 'pm_score': 1, 'selected': False, 'text': '<p>Didn\'t see your database diagram whilst I was writing.</p>\n\n<pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;\n&lt;hibernate-mapping default-cascade="save-update" xmlns="urn:nhibernate-mapping-2.2"&gt;\n    &lt;class lazy="false" name="Namespace.Customer, Namespace" table="Customer"&gt;\n        &lt;id name="Id" type="Int32" unsaved-value="0"&gt;\n            &lt;column name="customer_id" length="4" sql-type="int" not-null="true" unique="true" index="CustomerPK"/&gt;\n            &lt;generator class="native" /&gt;\n        &lt;/id&gt;\n\n        &lt;bag name="AcceptedOffers" inverse="false" lazy="false" cascade="all-delete-orphan"&gt;\n            &lt;key column="accepted_offer_id"/&gt;\n            &lt;one-to-many class="Namespace.AcceptedOffer, Namespace"/&gt;\n        &lt;/bag&gt;\n\n  &lt;/class&gt;\n&lt;/hibernate-mapping&gt;\n\n\n&lt;?xml version="1.0" encoding="utf-8" ?&gt;\n&lt;hibernate-mapping default-cascade="save-update" xmlns="urn:nhibernate-mapping-2.2"&gt;\n    &lt;class lazy="false" name="Namespace.AcceptedOffer, Namespace" table="Accepted_Offer"&gt;\n        &lt;id name="Id" type="Int32" unsaved-value="0"&gt;\n            &lt;column name="accepted_offer_id" length="4" sql-type="int" not-null="true" unique="true" /&gt;\n            &lt;generator class="native" /&gt;\n        &lt;/id&gt;\n\n        &lt;many-to-one name="Plan" class="Namespace.Plan, Namespace" lazy="false" cascade="save-update"&gt;\n            &lt;column name="plan_id" length="4" sql-type="int" not-null="false"/&gt;\n        &lt;/many-to-one&gt;\n\n  &lt;/class&gt;\n&lt;/hibernate-mapping&gt;\n</code></pre>\n\n<p>Should probably do the trick (I\'ve only done example mappings for the collections, you\'ll have to add other properties).</p>\n'}]
['https://Stackoverflow.com/questions/10915', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1284/']