NHibernate mapping for an object for several different parent objects (e.g. Addres & # 8594; Firm, Addres & # 8594; Client)

Can someone help me with the best way to match the following situation with the current nHibernate? The Address class is used both in the client and in the company. How can I store it most efficiently in SQL? And what should the comparison look like? I thought of several options, but I'm not experienced enough with nHibernate for these situations:

  • use 1 address entity and 1 table and use the denominator column to distinguish between the address for the client and the address for the company β†’ how to implement this in nHibernate?

  • use 1 address entity and 2 tables (ClientAddresses and CompanyAddresses) β†’ but I can only define 1 table when matching the Address class

  • use 2 addresses and 2 tables -> not very elegant

I just stumbled upon this problem when I started implementing a company class and realized that it also needed several addresses. So far, I had the Address and Client class, and there was one, many things between them. In the database, the address had an additional column CustomerId. But introducing the company class, I was stuck ...

Any help would be greatly appreciated.

I am currently working in the area of ​​sharparch 1.5, which uses automation, and my mapping files look like this:

public class AddressMap : IAutoMappingOverride<Address> { public void Override(AutoMapping<Address> mapping) { mapping.Table("addresses"); mapping.Id(x => x.Id, "AddressGuid") .UnsavedValue(Guid.Empty) .GeneratedBy.GuidComb(); mapping.References(x => x.Client, "ClientGuid"); } } 

Below is another code illustrating the problem:

Address

 public class Address { public virtual string StreetLine1 { get; set; } public virtual string StreetLine2 { get; set; } public virtual string PostalCode { get; set; } public virtual string City { get; set; } public virtual string Country { get; set; } } 

which has the following table:

tablename = addresses
fields = AddressGuid, StreetLine1, StreetLine2, PostalCode, City, Country

Client

 public class Client { public IList<Address> Addresses {get;set;} } 

Company

 public class Company { public IList<Address> Addresses {get;set;} } 
+4
source share
2 answers

It looks like you can implement # 1 with a nHibernate <any> mapping. Please note that in this case you cannot specify foreign key restrictions.

<any> example

Free nHibernate syntax

+1
source

You could model relationships as many-to-many: many companies to many addresses and many clients to many addresses.

In both comparisons of your company and client:

 mapping.HasManyToMany(x => x.Addresses); 

This will create two additional tables: one mapping between companies and addresses, another mapping between clients and addresses.

In theory, this can lead to a sharing situation (some companies and clients sharing the same address bar) that you probably don’t want, but as long as your application logic does not allow this to happen, you will be fine. and you don’t have to do anything complicated with nhibernate.

0
source

Source: https://habr.com/ru/post/1308686/


All Articles