Nhibernate 复制柱

发布于 2024-10-20 01:20:35 字数 2014 浏览 2 评论 0原文

我想映射以下简单实体:

public class AnimalsOwner
{
    public AnimalsOwner()
    {
        Cats = new List<Cat>();
        Dogs = new List<Dog>();
    }

    public virtual int Id { get; set; }

    public virtual IList<Cat> Cats { get; set; }

    public virtual IList<Dog> Dogs { get; set; }
}

public abstract class Animal
{
    public virtual int Id { get; set; }

    public virtual AnimalsOwner AnimalsOwner { get; set; }
}

public class Dog : Animal
{
    public virtual string DogsProperty { get; set; }
}

public class Cat : Animal
{
    public virtual string CatsProperty { get; set; }
}

使用以下映射:

public OwnerMapping()
    {
        this.Table("Owners");
        Id(x => x.Id)
            .GeneratedBy.Native();

        HasMany(x => x.Cats).AsBag().Cascade.SaveUpdate();
        HasMany(x => x.Dogs).AsBag().Cascade.SaveUpdate();
    }
}

public class AnimalMapping : ClassMap<Animal>
{
    public AnimalMapping()
    {
        this.Table("Animals");
        Id(x => x.Id).GeneratedBy.Native();

        References(x => x.AnimalsOwner).Not.Nullable();
    }
}

public class DogMap : SubclassMap<Dog>
{
    public DogMap()
    {
        this.Table("Dogs");
        this.KeyColumn("AnimalId");
        Map(x => x.DogsProperty);
    }
}

public class CatMap : SubclassMap<Cat>
{
    public CatMap()
    {
        this.Table("Cats");
        this.KeyColumn("AnimalId");
        Map(x => x.CatsProperty);
    }
}

使用此类映射一切正常,但生成的架构如下所示:

Db schema

此类代码:

 var owner = new AnimalsOwner();
                    owner.Cats.Add(new Cat()
                    {
                        AnimalsOwner = owner,
                        CatsProperty = "cat"
                    });

在 OwnderId 列的所有三个表(Animals、Cats、Dogs)中进行插入。但是仅将其放在 Animals 表中还不够吗?如果是的话如何实现呢?如果 NHibernate 的行为符合预期,那么他为什么要做这样的重复呢?

I want to mapping following simple entities:

public class AnimalsOwner
{
    public AnimalsOwner()
    {
        Cats = new List<Cat>();
        Dogs = new List<Dog>();
    }

    public virtual int Id { get; set; }

    public virtual IList<Cat> Cats { get; set; }

    public virtual IList<Dog> Dogs { get; set; }
}

public abstract class Animal
{
    public virtual int Id { get; set; }

    public virtual AnimalsOwner AnimalsOwner { get; set; }
}

public class Dog : Animal
{
    public virtual string DogsProperty { get; set; }
}

public class Cat : Animal
{
    public virtual string CatsProperty { get; set; }
}

With the following mappings:

public OwnerMapping()
    {
        this.Table("Owners");
        Id(x => x.Id)
            .GeneratedBy.Native();

        HasMany(x => x.Cats).AsBag().Cascade.SaveUpdate();
        HasMany(x => x.Dogs).AsBag().Cascade.SaveUpdate();
    }
}

public class AnimalMapping : ClassMap<Animal>
{
    public AnimalMapping()
    {
        this.Table("Animals");
        Id(x => x.Id).GeneratedBy.Native();

        References(x => x.AnimalsOwner).Not.Nullable();
    }
}

public class DogMap : SubclassMap<Dog>
{
    public DogMap()
    {
        this.Table("Dogs");
        this.KeyColumn("AnimalId");
        Map(x => x.DogsProperty);
    }
}

public class CatMap : SubclassMap<Cat>
{
    public CatMap()
    {
        this.Table("Cats");
        this.KeyColumn("AnimalId");
        Map(x => x.CatsProperty);
    }
}

With such mappings everything works fine, but generated schema looks like following:

Db schema

And such code:

 var owner = new AnimalsOwner();
                    owner.Cats.Add(new Cat()
                    {
                        AnimalsOwner = owner,
                        CatsProperty = "cat"
                    });

Makes inserts in all three tables (Animals, Cats, Dogs) of column OwnderId. But isn't it enough to have it only in Animals table? If yes then how to achieve it? If NHibernate acts as expected then why is he doing such a duplication?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

韶华倾负 2024-10-27 01:20:35

我将使用每类表策略来映射它,并按类型过滤公共集合。这种数据库结构更容易使用并且查询更高效。

public class AnimalMapping : ClassMap<Animal>
{
    public AnimalMapping()
    {
        Table("Animals");
        Id(x => x.Id).GeneratedBy.Native();    
        References(x => x.AnimalsOwner);
        DiscriminateSubClassesOnColumn("AnimalType");
    }
}

public class DogMap : SubclassMap<Dog>
{
    public DogMap()
    {
        DiscriminatorValue("DOG");
        Map(x => x.DogsProperty);
    }
}

public class CatMap : SubclassMap<Cat>
{
    public CatMap()
    {
        DiscriminatorValue("CAT");
        Map(x => x.CatsProperty);
    }
}

可以通过将 Animals 集合映射为私有成员并按类型过滤来公开 Dogs 和 Cats 集合:

public class AnimalsOwner
{
    private IList<Animal> _animals;

    public AnimalsOwner()
    {
        _animals = new List<Animal>();
    }

    public virtual int Id { get; set; }

    public virtual IEnumerable<Animal> Animals { get { return _animals; } }

    public virtual IEnumerable<Cat> Cats { get { return _animals.OfType<Cat>(); } }

    public virtual IEnumerable<Dog> Dogs { get { return _animals.OfType<Dog>(); } }

    public virtual void AddAnimal(Animal animal)
    {
        if (!_animals.Contains(animal))
        {
            animal.AnimalsOwner = this;
            _animals.Add(animal); 
        }
    }

    public virtual void RemoveAnimal(Animal animal)
    {
        if (_animals.Contains(animal))
        {
            animal.AnimalsOwner = null;
            _animals.Remove(animal);
        |
     }   
}

此类的映射为:

public OwnerMapping()
    {
        this.Table("Owners");
        Id(x => x.Id).GeneratedBy.Native();

        HasMany(x => x.Animals.AsBag.Cascade.AllDeleteOrphan()
            .Inverse().LazyLoad()
            .Access.CamelCaseField(Prefix.Underscore);
    }
}

添加猫的代码为:

var owner = new AnimalsOwner();
owner.AddAnimal(new Cat()
                {
                    CatsProperty = "cat"
                });

I would map this using table-per-class strategy and filter the public collections by type. This database structure is much easier to work with and more efficient to query.

public class AnimalMapping : ClassMap<Animal>
{
    public AnimalMapping()
    {
        Table("Animals");
        Id(x => x.Id).GeneratedBy.Native();    
        References(x => x.AnimalsOwner);
        DiscriminateSubClassesOnColumn("AnimalType");
    }
}

public class DogMap : SubclassMap<Dog>
{
    public DogMap()
    {
        DiscriminatorValue("DOG");
        Map(x => x.DogsProperty);
    }
}

public class CatMap : SubclassMap<Cat>
{
    public CatMap()
    {
        DiscriminatorValue("CAT");
        Map(x => x.CatsProperty);
    }
}

The Dogs and Cats collection can be exposed by mapping the Animals collection as a private member and filtering by type:

public class AnimalsOwner
{
    private IList<Animal> _animals;

    public AnimalsOwner()
    {
        _animals = new List<Animal>();
    }

    public virtual int Id { get; set; }

    public virtual IEnumerable<Animal> Animals { get { return _animals; } }

    public virtual IEnumerable<Cat> Cats { get { return _animals.OfType<Cat>(); } }

    public virtual IEnumerable<Dog> Dogs { get { return _animals.OfType<Dog>(); } }

    public virtual void AddAnimal(Animal animal)
    {
        if (!_animals.Contains(animal))
        {
            animal.AnimalsOwner = this;
            _animals.Add(animal); 
        }
    }

    public virtual void RemoveAnimal(Animal animal)
    {
        if (_animals.Contains(animal))
        {
            animal.AnimalsOwner = null;
            _animals.Remove(animal);
        |
     }   
}

The mapping for this class would be:

public OwnerMapping()
    {
        this.Table("Owners");
        Id(x => x.Id).GeneratedBy.Native();

        HasMany(x => x.Animals.AsBag.Cascade.AllDeleteOrphan()
            .Inverse().LazyLoad()
            .Access.CamelCaseField(Prefix.Underscore);
    }
}

And the code for adding a cat would be:

var owner = new AnimalsOwner();
owner.AddAnimal(new Cat()
                {
                    CatsProperty = "cat"
                });
戏舞 2024-10-27 01:20:35

您会得到重复项,因为您在所有者映射中定义了 HasMany Cats 和 Dogs(以及 Owner 类中的 Cat 和 Dog 集合)。也许您真正想要的是所有者类中的一组动物(而不是单独的狗和猫),并且所有者映射中只有一个 HasMany 动物?

You're getting the duplicates becuse you have HasMany Cats and Dogs defined in the owner mapping (and Cat & Dog collections in the Owner class). Perhaps what you really want is a collection of Animals in the owners class (not individually dogs and cats) and just one HasMany Animals in the Owner mapping?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文