【发布时间】: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>
有人可以帮帮我吗?
非常感谢您。
【问题讨论】:
-
为什么
locationId是用户和组用户的主键的一部分?我认为这是您问题的根源-您试图将同一列用作多个映射的复合键的一部分。那么为什么要这样做呢?您是否会有多个用户使用相同的userId,但仅根据其位置进行区分? -
尝试谷歌搜索
create hbm.xml from sql error should be mapped with insert="false" update="false"。例如How can I map “insert='false' update='false'” on a composite-id key-property which is also used in a one-to-many FK?.
标签: java xml database hibernate hbm