我如何创建一个多值组件的NHibernate映射文件是一个唯一的一组列? [英] How do I create a NHibernate Mapping file for a multi-value component that is a Unique set of columns?
问题描述
以下是域对象:
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;组; }
}
不要担心 所以我知道如何从SpaceCoordinate中创建一个组件,但是我想要使它不能存在重复插入同一组坐标。为了做到这一点,我想把组件作为一个整体独一无二。 为了给你一个我想要去的方向感,我会向您展示我对映射类的作用: 这个Fluent NHibernate映射的问题在于它不是统一适用于SpaceCoordinate值的唯一约束。请注意,我并不是试图将每个设置为独特的,这将是太严格了。 那么如何用hbm或流利的映射文件来映射这个呢? 任何帮助将不胜感激,谢谢阅读!! m4bwav与hbm映射正确;但是,在你写问题的时候,流利的NHibernate没有这方面的支持。我刚刚提交了一个修复程序,它向组件映射添加了一个 所以你需要更新你的Fluent NHibernate的副本。如果由于某种原因无法执行此操作,可以使用 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: and here is the component I'm having trouble with: Don't worry about 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: 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 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
这篇关于我如何创建一个多值组件的NHibernate映射文件是一个唯一的一组列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! SpaceObject AtLocation <
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
Unique
方法。
<$ (X => x.Coordinates,m =>
{
m.Map(x => xx);
m.Map(x => xy);
m.Map(x => xz);
})。
SetAttribute
方法来破解它。
<$ p (x => xx);
> Component< SpaceCoordinate>(x => x.Coordinates,m =>
{
m.Map m.Map(x => xy);
m.Map(x => xz);
})。SetAttribute(unique,true);
public class SpaceLocation
{
public SpaceCoordinate Coordinates { get; set; }
public SpaceObject AtLocation { get; set; }
}
public struct SpaceCoordinate
{
public int x { get; set; }
public int y { get; set; }
public int z { get; set; }
}
SpaceObject AtLocation
for the purposes of this quesiton.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);
});
}
}
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();
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");