SetInverseTrueOnCollectionMapping for a single entity

Posts   
 
    
SteveM
User
Posts: 22
Joined: 23-Mar-2011
# Posted on: 16-Apr-2012 19:32:41   

I'm using NHibernate. In my project settings (Output Settings Values) I have SetInverseTrueOnCollectionMapping selected, which is what I want 'inverse=false' - except for a single entity, where I want 'inverse=false' in my mapping.

I can't find this setting in the entity property pages. Any ideas on how I would achieve this?

I'm using version 3.1.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 17-Apr-2012 08:05:50   

The mentioned setting is for Project level use only, that's why you don't see it in the entity's Code.Geneneration sub-tab.

I don't know much about this NH setting, neither the possible consequences setting it on/off on opposite sides of a relation. this post seems to explain it quite well.

A question: Does it have the same effect if you turn off the "Emit navigator setter" setting in the navigator of the parent side?

If you really need this I think you could do the following:

  1. Create an extension of the NH framework settings. In a notepad paste the following code and save it into <LLBLGen v3.1 installation folder>\Frameworks\SD.Frameworks.NHibernate.Extended.frameworksettings
<?xml version="1.0"?>
<frameworkSettings xmlns="http://sd/llblgen/pro/frameworkSettingsDefinition.xsd">
    <supportedFrameworks>
        <framework name="NHibernate"/>
    </supportedFrameworks>
    <settingDefinitions>
        <settingDefinition name="SetInverseTrueOnCollectionMappingsForNavigator" description="This setting controls whether 'inverse=true' is appended to navigator mappings which represent a set/collection" default="true" category="General" 
                            type="bool" targetElement="512" defaultSource="SetInverseTrueOnCollectionMappings"/>
            </settingDefinitions>   
</frameworkSettings>

What we just did is to create another setting called **SetInverseTrueOnCollectionMappingsForNavigator **which is applied only to 1:n navigators. That setting takes it's default value from the original one, which is placed at Project label, that is SetInverseTrueOnCollectionMappings. So you can place the default for all entities at project level, then you can go to each navigator and change those you want.

  1. Modify the templates so they look now for the new setting.

2.1. Edit <LLBLGen v3.1 installation folder>\Frameworks\NHibernate\Templates\Shared\Shared\entityHbmMapping.lpt , go to line 552 and change it to:

inverse = relationshipInfo.NavigatorInstance.OutputSettingValues.GetRealBoolSettingValue("SetInverseTrueOnCollectionMappingsForNavigator", currentProject)

2.2. For fluent, go to <LLBLGen installation folder>\Frameworks\NHibernate\Templates\Net3.5\C#\entityFluentMapping.lpt, go to line 632 and change it to:

inverse = relationshipInfo.NavigatorInstance.OutputSettingValues.GetRealBoolSettingValue("SetInverseTrueOnCollectionMappingsForNavigator", currentProject)

Save both template files. Now you have two settings the one you always saw, that is at project level, SetInverseTrueOnCollectionMappings. Use it to instruct the Designer to apply this to all your entity navigators. The other one is SetInverseTrueOnCollectionMappingsForNavigator. This setting can be set only to Navigator elements.

To apply it, go to your special entity, then to Code gen info sub-tab, then at the "Element" DropDownList select the 1:n navigator you want to modify. You will see the new setting, set it to true/false, as you wish.

(Edit) If you elaborate more on Why should we put this setting in navigator elements as well, maybe it could be included in future versions.

David Elizondo | LLBLGen Support Team
SteveM
User
Posts: 22
Joined: 23-Mar-2011
# Posted on: 17-Apr-2012 14:58:14   

The setting in question controls which side of the relationship is responsible for persisting changes to the database. I believe the default setting, 'inverse=true' is what is wanted for most cases.

Now that a workaround is posted, I would not recommend adding this as an Entity setting. I just have an unusual situation.

This works as advertised, but there is a small issue with the code you posted. In the .lpt file, the line should be:

inverse = relationshipInfo.NavigatorInstance.OutputSettingValues.GetRealBoolSettingValue("SetInverseTrueOnCollectionMappingsForNavigator", currentProject)

so it's getting a bool value for the '..ForNavigator' setting.

Thanks for the quick response and clear instructions!

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 17-Apr-2012 18:10:27   

You are absolutely right. I didn't post my final test. I just updated my last post so people read it can get it working.

Thanks for the feedback.

David Elizondo | LLBLGen Support Team