我试图在两个表之间建立一对多的关系。我有一个名为users的表,其中包含许多user_history条目。我在mysql中定义了这些表,并使用intellij hibrernate支持生成实体。问题是,当我想在数据库中插入一些东西时,我收到以下错误实体映射中的重复列:com.UserHistoryEntity
用户:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.UsersEntity" table="users" schema="" catalog="protein_tracker">
<id name="id">
<column name="id" sql-type="int" not-null="true"/>
</id>
<property name="name">
<column name="name" sql-type="varchar" length="45" not-null="true"/>
</property>
<property name="total">
<column name="total" sql-type="int" not-null="true"/>
</property>
<property name="goal">
<column name="goal" sql-type="int" not-null="true"/>
</property>
<set name="userHistoriesById" inverse="true">
<key>
<column name="id_user" not-null="true"/>
</key>
<one-to-many not-found="ignore" class="com.UserHistoryEntity"/>
</set>
</class>
</hibernate-mapping>
UserHistory:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.UserHistoryEntity" table="user_history" schema="" catalog="protein_tracker">
<id name="id">
<column name="id" sql-type="int" not-null="true"/>
</id>
<property name="entryDate">
<column name="entry_date" sql-type="datetime" not-null="true"/>
</property>
<property name="entry">
<column name="entry" sql-type="varchar" length="45" not-null="true"/>
</property>
<property name="idUser">
<column name="id_user" sql-type="int" not-null="true"/>
</property>
<many-to-one name="usersByIdUser" class="com.UsersEntity">
<column name="id_user" not-null="true"/>
</many-to-one>
</class>
</hibernate-mapping>
如何解决此问题?
答案 0 :(得分:0)
将usersByIdUser的Many-To-One
映射标记为insertable = false,updatable = false。这允许关系仅由您映射的单个属性idUser
管理。