首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >compiste主键中的外键(列重复错误)

compiste主键中的外键(列重复错误)
EN

Stack Overflow用户
提问于 2015-02-07 00:51:11
回答 1查看 316关注 0票数 0

问题是我们的模式有许多组合主键,因此,我们有很多引用这些组合ids的外键。

在上面你可以看到一个数据库模式的例子,在这个例子中我们有很多问题。还有一个hbm.xml文件:

代码语言:javascript
复制
<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文件的多对一关系中,则无法将配置文件实例插入到数据库中(无效):

代码语言:javascript
复制
<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>

有人能帮帮我吗?

非常提前感谢您。

EN

回答 1

Stack Overflow用户

发布于 2015-02-07 21:57:52

因为您说在现有的模式中有许多包含数据的组合主键,并且您想要向其中添加hibernate。

只是想一想:我认为删除现有的组合主键,使它们成为唯一索引,并创建新的非组合主键,并使用序列更新所有数据(取决于您正在使用的DMBS ),这并不是什么大事。

当然,这取决于您希望如何集成hibernate。可能存在保留旧的组合主键的原因。但也许你应该考虑一下。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28370736

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档