1

我使用 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

4

1 回答 1

0

如果有人偶然发现这一点,我只使用注释映射解决了这个问题,而不是.hbm.xml使用起来更简单。

于 2012-08-14T13:41:46.273 回答