How to save an object with NHibernate, which consists of other complex types, but I only have the identifier of other complex types

I am working on my first NHibernate project, so this may seem like a simple question.

The following is a simplified one from my real scenario to convey a specific question.

Suppose I have a Customer object

public class Customer
{
    prop virtual int ID { get; set; }
    prop virtual string Name { get; set; }
    prop virtual Region Region { get; set; }
}

and my subject of the region (the regions are similar, NorthWest, MidWest, etc. - a pretty specific list, which will be in some kind of drop-down list)

public class Region
{
    prop virtual int ID { get; set; }
    prop virtual string Name { get; set; }
    private readonly IList<Customer> _customers = new List<Customer>();

    public virtual void Add(Customer customer)
    {
        _customers.Add(customer);
    }

    public virtual void Remove(Customer customer)
    {
        _customers.Remove(customer);
    }

    public virtual Customer[] GetCustomers()
    {
        return _customers.ToArray();
    }
}

When I go to Persist Customer Entity, I really want to have only 3 pieces of information (Customer.ID, Customer.Name and Customer.Region.ID) how to do this because NHibernate expects a client object that includes the full Region object (and not only identifier) ​​...

+3
2

Load, Region by ID . , NHibernate .

+6

, , Region . , "" "" ( ), .

0

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


All Articles