【问题标题】:Foreign key in compiste primary key (duplicated column error)复合主键中的外键(重复列错误)
【发布时间】:2015-02-06 16:51:11
【问题描述】:

我们只是将 Hibernate 集成到我们的 Web 应用程序中,该应用程序从今天开始使用对关系数据库的直接查询来开发。 我们生成了 hbm.xml 文件,以便实现所有实体和映射,以便通过 hibernate 访问数据库进行读写。

问题是我们的架构有很多复合主键,因此,我们有很多外键引用这些复合 ID。

您可以在上面看到一个我们遇到很多问题的数据库架构示例。还有hbm.xml文件:

<class name="Profile" table="Profile" optimistic-lock="version">
    <id name="profileId" type "java.lang.Integer">
        <column name="profileId" />
        <generator class="assigned" />
    </id>
    <many-to-one name="location" class="Location" fetch="select" >
        <column name="locationId" length="3" not-null="true" />
    </many-to-one>
    <many-to-one name="users" class="Users" fetch="select"> 
          <column name="locationId" length="3" not-null="true" /> 
          <column name="userId" length="30" not-null="true" />
    </many-to-one>
     <many-to-one name="groupUsers" class="GroupUsers" fetch="select"> 
          <column name="locationId" length="3" not-null="true" /> 
          <column name="groupUserId" length="30" not-null="true" />
    </many-to-one>
</class>

<class name="Users" table="Users" optimistic-lock="version">
    <composite-id name="id" class="UsersId">
        <key-property name="locationId" type="string">
            <column name="locationId" length="3" />
        </key-property>
        <key-property name="userId" type="string">
            <column name="userId" length="30" />
        </key-property>
    </composite-id>

    <set name="profiles" table="Profile" inverse="true" lazy="true" fetch="select">
        <key>
            <column name="locationId" length="3" not-null="true" />
            <column name="userId" length="30" not-null="true" />
        </key>
        <one-to-many class="Profile" />
    </set>
</class>

<class name="GroupUsers" table="GroupUsers" optimistic-lock="version">
    <composite-id name="id" class="GroupUsersId">
        <key-property name="locationId" type="string">
            <column name="locationId" length="3" />
        </key-property>
        <key-property name="groupUserId" type="string">
            <column name="groupUserId" length="30" />
        </key-property>
    </composite-id>

    <set name="profiles" table="Profile" inverse="true" lazy="true" fetch="select">
        <key>
            <column name="locationId" length="3" not-null="true" />
            <column name="groupUserId" length="30" not-null="true" />
        </key>
        <one-to-many class="Profile" />
    </set>
</class>

<class name="Location" table="Location" optimistic-lock="version">
    <id name="locationId" type="string">
        <column name="locationId" length="3" />
        <generator class="assigned" />
    </id>

    <set name="profiles" table="Profile" inverse="true" lazy="true" fetch="select">
        <key>
            <column name="locationId" length="3" not-null="true" />
        </key>
        <one-to-many class="Profile" />
    </set>
</class>

启动 Tomcat 时出现以下错误:“Profile column: locationId (should be mapped with insert="false" update="false")”

但是如果我尝试将这两个属性放在 Profile.hbm.xml 文件的多对一关系中,我无法将配置文件实例插入数据库(无效):

<many-to-one name="location" class="Location" fetch="select" insert="false" update="false">
      <column name="locationId" length="3" not-null="true" />
</many-to-one>
 <many-to-one name="users" class="Users" fetch="select"insert="false" update="false"> 
      <column name="locationId" length="3" not-null="true" /> 
      <column name="userId" length="30" not-null="true" />
</many-to-one>
 <many-to-one name="groupUsers" class="GroupUsers" fetch="select" insert="false" update="false"> 
      <column name="locationId" length="3" not-null="true" /> 
      <column name="groupUserId" length="30" not-null="true" />
</many-to-one>

有人可以帮帮我吗?

非常感谢您。

【问题讨论】:

标签: java xml database hibernate hbm


【解决方案1】:

因为您说您在现有架构中有许多复合主键,其中包含数据,并且您想向其中添加休眠。 听起来您宁可没有复合主键。

只是一个想法: 我认为删除现有的组合主键,改为 uniqe 索引 并创建 新的非组合主键 更新所有数据并使用顺序(取决于您使用的 DMBS)。

这当然取决于你想如何集成hibernate。保留旧的组合主键可能是有原因的。 但也许你应该考虑一下。

【讨论】:

    猜你喜欢
    • 2016-07-04
    • 2011-01-23
    • 2018-04-07
    • 1970-01-01
    • 1970-01-01
    • 2013-06-29
    • 1970-01-01
    • 2022-11-21
    • 1970-01-01
    相关资源
    最近更新 更多