使用外键将一对多集合映射到外键

发布于 2024-07-12 22:19:52 字数 1821 浏览 13 评论 0原文

我正在使用 nhibernate 映射遗留数据库,并且在映射关系时遇到一些问题。

对于像这样的表来说,这两个类看起来

public class Questionnaire
{
    public int Id {get; set;}
    public string FormCode {get; set;}
    public IList<Question> Questions {get; set;}
}

public class Question
{
    public int Id{get; set;}
    public Questionnaire Questionnaire {get;set;}
    public string QuestionText{get;set;}
}

像这样

Questionnaire Table  
Id int  
FormCode varchar(100)  

Question Table  
Id int  
FormCode varchar(100)  
QuestionText varchar(max)  

两个表之间的关系是表单代码列。

我当前的映射是这样的

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="QDesign.Core.Models" assembly="QDesign.Core">
<class name="Questionnaire" table="_questionnaire_list">
    <id column="Id" name="Id">
            <generator class="identity"/>
    </id>
        <property name="FormCode" column="FormCode"/>
        <bag name="Questions" >
            <key foreign-key="FormCode" property-ref="FormCode" />
            <one-to-many class="Question" />            
        </bag>
</class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="QDesign.Core.Models" assembly="QDesign.Core">
<class name="Question" table="_questionnaire_items">
    <id column="ID" name="Id" unsaved-value="-1">
            <generator class="identity" />
        </id>
        <property name="QuestionText" column="QuestionText" />
</class>
</hibernate-mapping>

当我运行映射时,我得到一个标识符类型不匹配,假设它试图将表单代码放入问题的 ID 中。 不幸的是,我无法更改表的结构,并且我不知道如何映射它,任何帮助将不胜感激。

I'm mapping a legacy database with nhibernate and having some problems with mapping a relation.

The two classes look like this

public class Questionnaire
{
    public int Id {get; set;}
    public string FormCode {get; set;}
    public IList<Question> Questions {get; set;}
}

public class Question
{
    public int Id{get; set;}
    public Questionnaire Questionnaire {get;set;}
    public string QuestionText{get;set;}
}

to tables that are like this

Questionnaire Table  
Id int  
FormCode varchar(100)  

Question Table  
Id int  
FormCode varchar(100)  
QuestionText varchar(max)  

The relationship between the two tables being the formcode column.

My current mapping is like this

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="QDesign.Core.Models" assembly="QDesign.Core">
<class name="Questionnaire" table="_questionnaire_list">
    <id column="Id" name="Id">
            <generator class="identity"/>
    </id>
        <property name="FormCode" column="FormCode"/>
        <bag name="Questions" >
            <key foreign-key="FormCode" property-ref="FormCode" />
            <one-to-many class="Question" />            
        </bag>
</class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="QDesign.Core.Models" assembly="QDesign.Core">
<class name="Question" table="_questionnaire_items">
    <id column="ID" name="Id" unsaved-value="-1">
            <generator class="identity" />
        </id>
        <property name="QuestionText" column="QuestionText" />
</class>
</hibernate-mapping>

When I run the mapping I get a Identifier type mismatch assuming it is trying to put the formcode into the Id of the question. Unfortunately I am unable to change the structure of the table and I am at a loss for how to map this and any help would be greatly appreciated.

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

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

发布评论

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

评论(2

冷默言语 2024-07-19 22:19:52

你们的关系是一种没有中间实体的 m:n 关系。 以这种方式定义 m:n 关系是一个典型的错误,因为它切断了 pk 边,从而导致存在两个具有相同属性/字段的表并且它们恰好在语义上表示相同事物的情况。 然而,因为它们在双方都是非 pk 值,所以可能存在冗余和不准确。 您可以通过两个字段将两个表连接在一起,但从语义上讲,这没有任何意义:对于将实体 X 关联到 Y 的实体模型,FK 方将关系的 PK 方作为 FK 字段获取,而对于 m:n 关系,您需要两个源自中间实体的 m:1 关系。 就是这样,没有例外。

因此,尽管您想按照您建议的方式进行映射,但这是无法完成的,因为 o/r 映射器无法保证正确性,因为建议的关系根本不正确。

Your relationship is an m:n relationship without an intermediate entity. It's a classic mistake to define m:n relationships this way, because it cuts out a pk side, which leads to the situation where there are two tables with the same attribute/field and they happen to semantically represent the same thing. However, because they're non-pk values in both sides, there's redundancy and also inaccuracy possible. You can join the two tables together over the two fields but semantically it means nothing: for an entity model to relate entity X to Y, the FK side gets the PK side of the relationship as FK fields, and for m:n relationships, you need two m:1 relationships originating from the intermediate entity. That's it, there's no exception.

So although you want to map this the way you propose, it can't be done as the o/r mapper can't guarantee correctness because the suggested relationship is simply not correct.

别把无礼当个性 2024-07-19 22:19:52

这就是 ORM 的“问题”。 我说“问题”是因为 Hibernate 在技术上是正确的:外键应该是主键。 但是,正如您所看到的,情况并非总是如此。

ID真的有什么用途吗? 如果不是,阻力最小的方法是将 FormCode 设为主键。 这是一个选择吗?

如果没有,我真的不知道除了查询问题而不是将它们视为子实体之外还能做什么。

Well this is the "problem" with ORMs. I say "problem" because Hibernate is technically correct: foreign keys should be primary keys. But, as you're witnessing, that's not always the case.

Is ID actually used for anything? If not, the path of least resistance is to make FormCode the primary key. Is that an option?

If not, I'm really not sure what to do other than query the questions rather than treating them as child entities.

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