如何在NHibernate中映射两个相互引用的对象?

时间:2021-10-14 18:03:32

I have a class that looks like this:

我有一个看起来像这样的课程:

public class Job
{
    public Company Company {get;set;}
    public Job JobForCompanyA {get;set;}
    public Job JobForCompanyB {get;set;}
}

The jobs for the two companies will reference each other, for example:

这两家公司的工作将互相参考,例如:

var jobA = new Job { Company = Company.CompanyA };
var jobB = new Job { Company = Company.CompabyB };
jobA.JobForCompanyB = jobB;
jobB.JobForCompanyA = jobA;

The problem with this is that if I map these as normal many-to-ones in NHibernate, it can't save them... since each Job object references the other, you would have to save one to get the PK, but you can't because it references the other (unsaved) Job, so you have a chicken and egg problem.

这个问题是如果我在NHibernate中将它们映射为普通的多对一,它就无法保存它们......因为每个Job对象引用另一个,你必须保存一个来获得PK,但是你不能因为它引用了另一个(未保存的)Job,所以你有鸡和蛋的问题。

From a db perspective, I could make a mapping table that maps Jobs for company A to Jobs for company B. Is there a way to map this using NHibernate without me having to change how my entity object looks? I know I could add list properties and hacks to do it like a normal many-to-many relationship, but this has to be a pattern that someone has a better solution for.

从db的角度来看,我可以制作一个映射表,将公司A的Jobs映射到公司B的Jobs。有没有办法使用NHibernate映射它,而不必更改我的实体对象的外观?我知道我可以添加列表属性和黑客,就像正常的多对多关系一样,但这必须是某人有更好的解决方案的模式。

I'm using Fluent NHibernate, so you can give me solutions using Fluent NHibernate or NHibernate XML mappings.

我正在使用Fluent NHibernate,因此您可以使用Fluent NHibernate或NHibernate XML映射为我提供解决方案。

2 个解决方案

#1


This fluent mapping will allow you to reference multiple properties of the same type. Here I'm assuming that in your Job table you have two columns (JobAId and JobBId) that reference another Job (via JobId).

这种流畅的映射将允许您引用相同类型的多个属性。在这里,我假设您的Job表中有两列(JobAId和JobBId)引用另一个Job(通过JobId)。

References(x => x. JobForCompanyA, "JobAId"); References(x => x. JobForCompanyB, "JobBId");

引用(x => x.JobForCompanyA,“JobAId”);引用(x => x.JobForCompanyB,“JobBId”);

#2


I don't see how to do it without creating another entity with none of these references so you can get null values saved. Save two of these dummy entities and grab their ids. Then retrieve each of them using their ids through the Job entity you already have and set them to each other that way. You should be able to save then.

我没有看到如何创建没有这些引用的另一个实体,所以你可以保存空值。保存其中两个虚拟实体并获取它们的ID。然后通过你已经拥有的Job实体使用它们的id检索它们,并以这种方式将它们设置为彼此。你应该能够保存。

It's ugly but that's pretty much how you'd have to do it in SQL anyway. I'd think about coming up with a different pattern if I were you.

这很难看,但这几乎是你在SQL中必须要做的事情。如果我是你,我会考虑提出一个不同的模式。

#1


This fluent mapping will allow you to reference multiple properties of the same type. Here I'm assuming that in your Job table you have two columns (JobAId and JobBId) that reference another Job (via JobId).

这种流畅的映射将允许您引用相同类型的多个属性。在这里,我假设您的Job表中有两列(JobAId和JobBId)引用另一个Job(通过JobId)。

References(x => x. JobForCompanyA, "JobAId"); References(x => x. JobForCompanyB, "JobBId");

引用(x => x.JobForCompanyA,“JobAId”);引用(x => x.JobForCompanyB,“JobBId”);

#2


I don't see how to do it without creating another entity with none of these references so you can get null values saved. Save two of these dummy entities and grab their ids. Then retrieve each of them using their ids through the Job entity you already have and set them to each other that way. You should be able to save then.

我没有看到如何创建没有这些引用的另一个实体,所以你可以保存空值。保存其中两个虚拟实体并获取它们的ID。然后通过你已经拥有的Job实体使用它们的id检索它们,并以这种方式将它们设置为彼此。你应该能够保存。

It's ugly but that's pretty much how you'd have to do it in SQL anyway. I'd think about coming up with a different pattern if I were you.

这很难看,但这几乎是你在SQL中必须要做的事情。如果我是你,我会考虑提出一个不同的模式。