How do you map a component that is also a primary key in NHibernate hbm xml (or in a map of the fluent-nhibernate class)?

I am trying to understand how to map a component as a primary key in nhibernate and, if possible, in free nhibernate.

This component is a unique set of three-dimensional coordinates, here is the object:

public class SpaceLocation
{
    public virtual SpaceCoordinate Coordinates { get; set; }
    public virtual SpaceObject AtLocation { get; set; }
}

SpaceCoordinate is a structure defined as follows:

public struct SpaceCoordinate
{
    public int x { get; set; }
    public int y { get; set; }
    public int z { get; set; }
}

In free nhibernate, to make SpaceCoordinate a command, I would create a display class as follows:

public class SpaceLocationMap : ClassMapWithGenerator<SpaceLocation>
{
    public SpaceLocationMap()
    {
        References(x => x.AtLocation);
        Component<SpaceCoordinate>(x => x.Coordinates, m =>
        {
            m.Map(x => x.x);
            m.Map(x => x.y);
            m.Map(x => x.z);
        }).Unique();
    }
}

But I would like to know how to make the SpaceCoordinate component as a whole a primary key with its unique restriction. How do I map this in Nhibernate xml or in the free nhibernate class map?

+3
2

, NHibernate, . unique component , 2.0; , , , .

composite-id?

+2

public class SpaceLocationMap : ClassMap<SpaceLocation>
{
    public SpaceLocationMap()
    {
        CompositeId(x => x.Coordinates)
            .KeyProperty(x => x.x)
            .KeyProperty(x => x.y)
            .KeyProperty(x => x.z);

        References(x => x.AtLocation);
    }
}
+2

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


All Articles