Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple One-To-One to same entity #458

Open
hrr opened this issue Jul 30, 2020 · 2 comments
Open

Multiple One-To-One to same entity #458

hrr opened this issue Jul 30, 2020 · 2 comments

Comments

@hrr
Copy link

hrr commented Jul 30, 2020

I'm fairly new to NHibernate and recently went over from ODM for this new project.

Not sure if I'm thinking crazy here with this design or this is a limitation in fnh:
image

My ShipmentPlan entity has two nullable columns, PackPlanId and LoadPlanId. They are referencing the same entity so I'm trying to figure out some kind of one-to-one mapping for both columns.

After desperate trial-and-errors, the only setup I have some success with is is the mapping below.

public ShipmentPlanMap()    
{
    References(x => x.PackPlan, "PackPlanId").Nullable().Cascade.SaveUpdate();
    References(x => x.LoadPlan, "LoadPlanId").Nullable().Cascade.SaveUpdate();
}
public OptimizationPlanMap()
{
	HasOne(x => x.ShipmentPlan).Cascade.All();
}

The problem is when I try to save the entity, even though the references looks right before saving, it crashed because ShipmentPlanId is not assigned and not in the insert query.
Entity before saving:
image

Query:

NHibernate xxxx: 2020-07-30 17:42:04.8 [9]: INSERT INTO [OptimizedPlan] (Created, CreatedBy, Updated, UpdatedBy, Name, ProjectId, PlanType, PlanConfigurationId) VALUES ('7/30/2020 9:42:04 AM', 'xxx', NULL, NULL, 'xxx', 1, 0, 3); select SCOPE_IDENTITY()

The success I had with this setup is I can fetch the entities correctly but as you see the saving part is not working.

Is this a issue in the design or mapping?

@namer315
Copy link

try to change
.Cascade.SaveUpdate()
to
.Cascade.Merge()

it should work

@hrr
Copy link
Author

hrr commented Aug 3, 2020

Sorry it didn't work.

This is the state before saving, as you can see the PackPlan is assigned:
image

Here is the log:

NHibernate xxx: 2020-08-03 12:52:27.9 [9]: INSERT INTO [ShipmentPlan] (Created, CreatedBy, Updated, UpdatedBy, Name, xxx, xxx, ShipmentId, PackPlanId, LoadPlanId) VALUES ('8/3/2020 4:52:27 AM', 'xxx', NULL, NULL, 'LoadPack-0', 0, 0, 117, NULL, NULL)

As you see the PackPlanId is saved as NULL.

I have now moved on with another solution:

public ShipmentPlanMap()    
{
    HasMany(x => x.OptimizedPlans).KeyColumn("ShipmentPlanId").Inverse().Cascade.SaveUpdate();
}
public OptimizationPlanMap()
{
    References(x => x.ShipmentPlan, "ShipmentPlanId");
}

This should work as any other one-to-many mapping but would force me to use some extra logic in implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants