Search code examples
javasqlhibernatehibernate-mappinghibernate-entitymanager

'Repeated column in mapping' when mapping a class to an existing list of objects


I'm adding ResourcePermission to the object Report. Each Query object can have in a one-to-one relationship. A Query extends Resource and has ResourcePermission's in a one-to-many (one Query to many Permissions).

I need to add the same property to the Report object associated with the Query because it can have different permissions. When I add the list and the map the one Query to many Permission relationship I get

Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: com.bio.ResourcePermission column: resource_id (should be mapped with insert="false" update="false")

Which I am not understanding why, the Report does not extend Query or Resource and therefore it isn't mapped twice. Can a table just not be the many for multiple one-to-many relationships?

<class name="com.bio.Report" table="REPORT">
    <id name="id" type="long" column="id">
        <generator class="foreign">
            <param name="property">query</param>
        </generator>
    </id>
    <property name="name" column="name"/>

    <!--Trying to add this list mapping breaks it-->
    <bag name="permissions" table="RESOURCE_PERMISSION">
        <key column="resource_id" not-null="true"/>
        <one-to-many class="com.bio.ResourcePermission"/>
    </bag>

    <!-- This query extends Resource-->
    <one-to-one name="query" class="com.bio.Query" />
</class>

This is the original item that had ResourcePermissions

    <class name="com.bio.Resource" table="RESOURCE">
    <id name="id" type="long" column="id">
        <generator class="native">
            <param name="sequence">SEQ_RESOURCE_AUTO</param>
        </generator>
    </id>

    <bag name="permissions" table="RESOURCE_PERMISSION" lazy="true" batch-size="50" cascade="all-delete-orphan">
        <key column="resource_id" not-null="true"/>
        <one-to-many class="com.bio.ResourcePermission"/>
    </bag>
</class>

The Permission mapping

    <class name="com.bio.ResourcePermission" table="RESOURCE_PERMISSION">
    <id name="id" type="long" column="id">
        <generator class="native">
            <param name="sequence">SEQ_RES_PERM_AUTO</param>
        </generator>
    </id>

    <property name="canEdit" column="edit"/>
    <property name="canView" column="can_view"/>
    <property name="canRun" column="run"/>
    <property name="everyone" column="everyone"/>
</class>

Solution

  • I had to set inverse="true" on the Report mapping since the ReportPermission will be responsible for the relationship.

    <bag name="permissions" table="RESOURCE_PERMISSION" inverse="true"> <key column="resource_id" not-null="true"/> <one-to-many class="com.bio.ResourcePermission"/> </bag>