我如何创建一个多值组件的NHibernate映射文件是一个唯一的一组列? [英] How do I create a NHibernate Mapping file for a multi-value component that is a Unique set of columns?

查看:144
本文介绍了我如何创建一个多值组件的NHibernate映射文件是一个唯一的一组列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用NHibernate和Fluent NHibernate来创建一个域对象的映射文件(尽管我不在乎一个答案是否使用连贯的NHibernate或者xml hbm语法)。我在计算如何指定表示域对象中的组件的一组列是唯一的时遇到问题。



以下是域对象:

  public class SpaceLocation 
{
public SpaceCoordinate Coordinates {get;组; }
public SpaceObject AtLocation {get;组; }
}

以下是我遇到的问题:

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

不要担心 SpaceObject AtLocation <

所以我知道如何从SpaceCoordinate中创建一个组件,但是我想要使它不能存在重复插入同一组坐标。为了做到这一点,我想把组件作为一个整体独一无二。

为了给你一个我想要去的方向感,我会向您展示我对映射类的作用:

  public class SpaceLocationMap:ClassMapWithGenerator< SpaceLocation> 
{
public SpaceLocationMap()
{

Component< SpaceCoordinate>(x => x.Coordinates,m =>
{
m.Map(x => xx);
m.Map(x => xy);
m.Map(x => xz);
});






$ b

这个Fluent NHibernate映射的问题在于它不是统一适用于SpaceCoordinate值的唯一约束。请注意,我并不是试图将每个设置为独特的,这将是太严格了。

那么如何用hbm或流利的映射文件来映射这个呢?



任何帮助将不胜感激,谢谢阅读!!

解决方案

m4bwav与hbm映射正确;但是,在你写问题的时候,流利的NHibernate没有这方面的支持。我刚刚提交了一个修复程序,它向组件映射添加了一个 Unique 方法。

 <$ (X => x.Coordinates,m => 
{
m.Map(x => xx);
m.Map(x => xy);
m.Map(x => xz);
})。

所以你需要更新你的Fluent NHibernate的副本。如果由于某种原因无法执行此操作,可以使用 SetAttribute 方法来破解它。



<$ p (x => xx);
> Component< SpaceCoordinate>(x => x.Coordinates,m =>
{
m.Map m.Map(x => xy);
m.Map(x => xz);
})。SetAttribute(unique,true);


I'm using NHibernate and Fluent NHibernate to create a mapping file for a domain object (though I don't care if an answer uses fluent NHibernate or xml hbm syntax). And I'm having trouble with figuring out how I specify that a set of columns representing a component within the domain object is unique.

Here's the domain object:

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

and here is the component I'm having trouble with:

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

Don't worry about SpaceObject AtLocation for the purposes of this quesiton.

So I know how to make a component out of SpaceCoordinate, but I want to make it so that there cannot be duplicate inserts of the same set of Coordinates. In order to do this, I want to make the component as a whole unique.

To give you a sense of the direction I'm trying to go, I'll show you what I have for the mapping class:

public class SpaceLocationMap : ClassMapWithGenerator<SpaceLocation>
    {
        public SpaceLocationMap()
        {

            Component<SpaceCoordinate>(x => x.Coordinates, m => 
            {
                m.Map(x => x.x);
                m.Map(x => x.y);
                m.Map(x => x.z);
            });
        }
    }

The problem with this Fluent NHibernate mapping is that it does not apply the unique constraint to the SpaceCoordinate's values collectively. Mind you I'm not trying to set each as unique individually, that would be too restrictive.

So how would you map with this with a hbm or fluent map file?

Any help would be greatly appreciated, Thanks for reading!!

解决方案

m4bwav was correct with the hbm mapping; however, Fluent NHibernate didn't have support for this at the time you wrote your question. I've just committed a fix that adds a Unique method to the component mapping.

Component<SpaceCoordinate>(x => x.Coordinates, m => 
{
  m.Map(x => x.x);
  m.Map(x => x.y);
  m.Map(x => x.z);
}).Unique();

So you'll need to update your copy of Fluent NHibernate. If you're unable to do this for some reason, you could use the SetAttribute method to hack it.

Component<SpaceCoordinate>(x => x.Coordinates, m => 
{
  m.Map(x => x.x);
  m.Map(x => x.y);
  m.Map(x => x.z);
}).SetAttribute("unique", "true");

这篇关于我如何创建一个多值组件的NHibernate映射文件是一个唯一的一组列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆