简体   繁体   English

如何在NHibernate hbm xml(或在fluent-nhibernate类映射中)映射也是主键的组件?

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

I'm trying to figure out how to map a component as a primary key in nhibernate and if possible in fluent nhibernate as well. 我试图弄清楚如何将组件映射为nhibernate中的主键,如果可能的话,也可以在流畅的nhibernate中映射。

The component in question is a unique set of 3d coordinates, here's the object: 有问题的组件是一组独特的3d坐标,这里是对象:

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

SpaceCoordinate is a struct defined as follows: SpaceCoordinate是一个定义如下的结构:

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

In fluent nhibernate to make SpaceCoordinate a componet I would create a mapping class like this: 在流畅的nhibernate中使SpaceCoordinate成为一个组件我会创建一个这样的映射类:

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 what I would like to know is how to make the SpaceCoordinate component as a whole the primary key with it's unique constraint. 但我想知道的是如何使SpaceCoordinate组件作为一个整体成为主键,并具有独特的约束。 How would I map this in Nhibernate xml, or in a fluent nhibernate classmap? 我如何在Nhibernate xml或流畅的nhibernate类映射中映射它?

I believe that unless you're running on NHibernate trunk, you can't do this. 我相信,除非你在NHibernate主干上运行,否则你不能这样做。 The unique attribute on component wasn't added until after 2.0 was released; 直到2.0发布之后才添加componentunique属性; so unless there's way around this, I don't think it's possible. 所以,除非有解决方法,我认为这是不可能的。

Are you able to map the fields as a composite-id instead? 您是否可以将字段映射为复合ID

it should be possible now using 现在应该可以使用

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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM