我使用hibernate对一些mySQL表进行反向工程,这些表与外键链接到ID
字段。现在我得到org.hibernate.DuplicateMappingException: Duplicate collection role mapping com.hibernate.user.User.empHours.
我已经解决了这个问题,然后摆弄我的代码,但我不得不重做逆向工程,这次似乎无法摆脱错误。如果有人能够解释如何解决问题以及发生了什么,那么我可以理解将来如何解决它,我们将不胜感激。
用户信息.hbm.xml:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.hibernate.user.User" table="user" catalog="secondsql">
<id name="id" type="java.lang.String">
<column name="ID" length="10" />
<generator class="assigned" />
</id>
<property name="username" type="java.lang.String">
<column name="Username" length="20" unique="true" />
</property>
<property name="password" type="java.lang.String">
<column name="Password" length="20" />
</property>
<property name="firstName" type="java.lang.String">
<column name="FirstName" length="20" />
</property>
<property name="lastName" type="java.lang.String">
<column name="LastName" length="20" />
</property>
<property name="resume" type="java.lang.String">
<column name="Resume" length="100" />
</property>
<property name="admin" type="java.lang.Boolean">
<column name="admin" not-null="true" />
</property>
<property name="colorScheme" type="java.lang.String">
<column name="colorScheme" length="10" not-null="true" />
</property>
<set name="empHours" inverse="true" table="emp_hours">
<key>
<column name="ID" length="10" />
</key>
<one-to-many class="com.hibernate.hours.EmpHours" />
</set>
</class>
</hibernate-mapping>
员工时间.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.hibernate.hours.EmpHours" table="emp_hours" catalog="secondsql">
<id name="timesheetId" type="java.lang.Long">
<column name="Timesheet_ID" />
<generator class="identity" />
</id>
<many-to-one name="user" class="com.hibernate.user.User" fetch="select">
<column name="ID" length="10" />
</many-to-one>
<property name="weekOf" type="java.util.Date">
<column name="WeekOf" length="10" />
</property>
<property name="workType" type="java.lang.String">
<column name="Work_Type" length="20" />
</property>
<property name="monday" type="java.lang.Integer">
<column name="Monday" />
</property>
<property name="tuesday" type="java.lang.Integer">
<column name="Tuesday" />
</property>
<property name="wednesday" type="java.lang.Integer">
<column name="Wednesday" />
</property>
<property name="thursday" type="java.lang.Integer">
<column name="Thursday" />
</property>
<property name="friday" type="java.lang.Integer">
<column name="Friday" />
</property>
<property name="saturday" type="java.lang.Integer">
<column name="Saturday" />
</property>
<property name="sunday" type="java.lang.Integer">
<column name="Sunday" />
</property>
<property name="weekTotal" type="java.lang.Integer">
<column name="Week_Total" />
</property>
</class>
</hibernate-mapping>
我发现这个错误很有意思的是,empHours位于com.hibernate.hours
,用户信息位于com.hibernate.user
答案 0 :(得分:0)
如果有人偶然发现这个问题,我只是使用注释映射而不是.hbm.xml
解决了这个问题,它使用起来要简单得多。