连接具有不同连接列名称的两个表

发布于 2024-09-17 18:09:32 字数 479 浏览 3 评论 0原文

我有两个表 A -> B 具有多对一映射/关联。

表 B 的主键是表 A 中的外键。

问题是两个表中的列名不同。假设 B 有主键列“typeNumId”,它是表 A 中的外键“type”。如何连接该列上的两个表?我如何指定映射来指示表必须连接“typeNumId”和“type”,这本质上是相同的。

特别是通过 hibernate 配置( hbm 文件)可以实现这一点吗?

类似的东西

<many-to-one name="Type" class="com.domain.ProcedureType" update="false" insert="false" fetch="join" lazy="false">
    <column name="? <this is in questions? >" not-null="true" />
</many-to-one>

I have two tables A -> B with many-to-one mapping/associations.

Table B's primary key is foreign key in table A.

The problem is column names in both tables are different. let's say B has primary key column "typeNumId" which is foreign key in table A as "type". How can I join both tables on this column? how can I specify mapping to indicate that tables have to join on "typeNumId" and "type" which is essentially same.

especially is this possible through hibernate config ( hbm files) ?

something like

<many-to-one name="Type" class="com.domain.ProcedureType" update="false" insert="false" fetch="join" lazy="false">
    <column name="? <this is in questions? >" not-null="true" />
</many-to-one>

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

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

发布评论

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

评论(3

那些过往 2024-09-24 18:09:32

ON 子句:

select * from A join B on A.type = B.typeNumId

The ON clause:

select * from A join B on A.type = B.typeNumId
爱格式化 2024-09-24 18:09:32

您可以使用many-to-one 元素的column 属性或等效的嵌套column 元素来声明外键的名称。从文档中:

5.1.12。多对一

与另一个人的普通关联
持久化类是使用声明的
多对一元素。关系型
模型是多对一关联;一个
一张表中的外键是
引用主键列
目标表。

<多对一
        名称=“属性名称”(1)
        列=“列名”(2)
        类=“类名”(3)
        级联=“cascade_style”(4)
        fetch="加入|选择" (5)
        更新=“真|假”(6)
        插入=“真|假”(6)
        property-ref="propertyNameFromAssociatedClass" (7)
        访问=“字段|属性|类名”(8)
        唯一=“真|假”(9)
        not-null=“真|假” (10)
        乐观锁=“真|假”(11)
        懒惰=“代理|无代理|假”(12)
        not-found="忽略|异常" (13)
        实体名称=“实体名称” (14)
        Formula="任意 SQL 表达式" (15)
        节点=“元素名称|@属性名称|元素/@属性|。”
        嵌入-xml =“真|假”
        索引=“索引名称”
        unique_key =“unique_key_id”
        外键=“foreign_key_name”
>>
  1. 名称:属性的名称。
  2. column(可选):外键列的名称。这也可以
    由嵌套指定
    元素。
  3. class(可选 - 默认为由以下确定的属性类型)
    反射):名称
    关联类。
  4. cascade(可选):指定哪些操作应该从
    关联的父对象
    对象。
  5. fetch(可选 - 默认为 select):在外连接之间选择
    获取或顺序选择
    正在获取。
  6. 更新、插入(可选 - 默认为 true):指定映射的
    SQL 中应包含列
    UPDATE 和/或 INSERT 语句。
    将两者设置为 false 允许纯
    “派生”关联,其值为
    从另一个属性初始化
    映射到相同的列,或者通过
    触发器或其他应用程序。
  7. property-ref(可选):关联类的属性名称
    被加入到这个外键中。如果
    未指定,主键
    使用关联的类。
  8. 访问(可选 - 默认为属性):Hibernate 使用的策略
    用于访问属性值。
  9. unique(可选):启用 DDL 生成唯一约束
    外键列。通过允许
    这是一个目标
    property-ref,你可以使
    一对一的关联多重性。
  10. not-null(可选):启用可空性的 DDL 生成
    外键约束
    列。
  11. optimistic-lock(可选 - 默认为 true):指定
    对此属性的更新是否有效
    需要收购乐观的
    锁。换句话说,它决定是否
    版本增量应该发生在
    此属性很脏。
  12. lazy(可选 - 默认为代理):默认情况下,单点
    关联是代理的。
    lazy="no-proxy" 指定
    当以下情况时应延迟获取属性
    实例变量是第一个
    已访问。这需要构建时间
    字节码检测。懒惰=“假”
    规定协会将
    总是被急切地取走。
  13. not-found(可选 - 默认为例外):指定外键如何
    引用缺失的行将是
    处理。忽略将处理失踪
    行作为空关联。
  14. entity-name(可选):关联类的实体名称。
  15. 公式(可选):定义值的 SQL 表达式
    计算出的外键。

所以应该这样做:

<many-to-one name="Type" class="com.domain.ProcedureType" update="false" insert="false" fetch="join" lazy="false">
    <column name="type" not-null="true" />
</many-to-one>

参考

You declare the name of the foreign key using the column attribute of the many-to-one element or the equivalnent nested column element. From the documentation:

5.1.12. Many-to-one

An ordinary association to another
persistent class is declared using a
many-to-one element. The relational
model is a many-to-one association; a
foreign key in one table is
referencing the primary key column(s)
of the target table.

<many-to-one
        name="propertyName"                                          (1)
        column="column_name"                                         (2)
        class="ClassName"                                            (3)
        cascade="cascade_style"                                      (4)
        fetch="join|select"                                          (5)
        update="true|false"                                          (6)
        insert="true|false"                                          (6)
        property-ref="propertyNameFromAssociatedClass"               (7)
        access="field|property|ClassName"                            (8)
        unique="true|false"                                          (9)
        not-null="true|false"                                        (10)
        optimistic-lock="true|false"                                 (11)
        lazy="proxy|no-proxy|false"                                  (12)
        not-found="ignore|exception"                                 (13)
        entity-name="EntityName"                                     (14)
        formula="arbitrary SQL expression"                           (15)
        node="element-name|@attribute-name|element/@attribute|."
        embed-xml="true|false"
        index="index_name"
        unique_key="unique_key_id"
        foreign-key="foreign_key_name"
/>
  1. name: the name of the property.
  2. column (optional): the name of the foreign key column. This can also be
    specified by nested
    element(s).
  3. class (optional - defaults to the property type determined by
    reflection): the name of the
    associated class.
  4. cascade (optional): specifies which operations should be cascaded from the
    parent object to the associated
    object.
  5. fetch (optional - defaults to select): chooses between outer-join
    fetching or sequential select
    fetching.
  6. update, insert (optional - defaults to true): specifies that the mapped
    columns should be included in SQL
    UPDATE and/or INSERT statements.
    Setting both to false allows a pure
    "derived" association whose value is
    initialized from another property that
    maps to the same column(s), or by a
    trigger or other application.
  7. property-ref (optional): the name of a property of the associated class
    that is joined to this foreign key. If
    not specified, the primary key of the
    associated class is used.
  8. access (optional - defaults to property): the strategy Hibernate uses
    for accessing the property value.
  9. unique (optional): enables the DDL generation of a unique constraint for
    the foreign-key column. By allowing
    this to be the target of a
    property-ref, you can make the
    association multiplicity one-to-one.
  10. not-null (optional): enables the DDL generation of a nullability
    constraint for the foreign key
    columns.
  11. optimistic-lock (optional - defaults to true): specifies that
    updates to this property do or do not
    require acquisition of the optimistic
    lock. In other words, it determines if
    a version increment should occur when
    this property is dirty.
  12. lazy (optional - defaults to proxy): by default, single point
    associations are proxied.
    lazy="no-proxy" specifies that the
    property should be fetched lazily when
    the instance variable is first
    accessed. This requires build-time
    bytecode instrumentation. lazy="false"
    specifies that the association will
    always be eagerly fetched.
  13. not-found (optional - defaults to exception): specifies how foreign keys
    that reference missing rows will be
    handled. ignore will treat a missing
    row as a null association.
  14. entity-name (optional): the entity name of the associated class.
  15. formula (optional): an SQL expression that defines the value for
    a computed foreign key.

So something like this should do it:

<many-to-one name="Type" class="com.domain.ProcedureType" update="false" insert="false" fetch="join" lazy="false">
    <column name="type" not-null="true" />
</many-to-one>

Reference

一生独一 2024-09-24 18:09:32

像这样的东西吗?

LEFT JOIN B on A.field1 = B.field2

根据您的喜好选择 JOIN 类型

Something like this?

LEFT JOIN B on A.field1 = B.field2

Choose type of JOIN on your taste

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