问题是我们的模式有许多组合主键,因此,我们有很多引用这些组合ids的外键。
在上面你可以看到一个数据库模式的例子,在这个例子中我们有很多问题。还有一个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>有人能帮帮我吗?
非常提前感谢您。
发布于 2015-02-07 21:57:52
因为您说在现有的模式中有许多包含数据的组合主键,并且您想要向其中添加hibernate。
只是想一想:我认为删除现有的组合主键,使它们成为唯一索引,并创建新的非组合主键,并使用序列更新所有数据(取决于您正在使用的DMBS ),这并不是什么大事。
当然,这取决于您希望如何集成hibernate。可能存在保留旧的组合主键的原因。但也许你应该考虑一下。
https://stackoverflow.com/questions/28370736
复制相似问题