问题
I have a hibernate mapping file ParcelServicePresetIds.hbm.xml:
<hibernate-mapping>
<class name="de.delife.sql.ParcelServicePresetIds" table="ParcelServicePresetIDs" schema="dbo" catalog="xxx">
<id name="id" type="int">
<column name="ID" />
<generator class="assigned" />
</id>
<many-to-one name="itemsBase" class="de.sql.ItemsBase" fetch="select" property-ref="itemId">
<column name="ItemID" />
</many-to-one>
<property name="itemId" column="ItemID" type="java.lang.Integer"/>
<property name="intValue" type="java.lang.Integer">
<column name="intValue" />
</property>
</class>
</hibernate-mapping>
When I run my program I get an error:
Initial SessionFactory creation failed.org.hibernate.MappingException: Repeated column in mapping for entity: de.sql.ParcelServicePresetIds column: ItemID (should be mapped with insert="false" update="false")
Why?
I've another entity Others.hbm.xml:
<many-to-one name="itemsBase" class="de.sql.ItemsBase" fetch="select" property-ref="itemId">
<column name="ItemID" unique="true" />
</many-to-one>
<property name="itemId" column="ItemID" type="java.lang.Integer"/>
In this entity it's working!!! I do not understand it! Can someone explain the difference?
回答1:
You are declaring ItemID
columns twice: one time with <property />
and second time with <many-to-one />
tag.
Remove one (probably <property/>
declaration)
回答2:
You probably don't need to use a propertyRef here. It could simply be:
<many-to-one name="itemsBase" class="de.sql.ItemsBase" fetch="select" column="ItemID"/>
Also, you don't require the property declaration for itemId
.
To answer the second part of the question:
- Whenever you give propertyRef, you need to follow it up with a property definition.
- If it is a 'managed column', i.e. a foreign-key-reference for example; you ought to qualify the property definition with
insert="false" update="false"
- However, in most cases; you donot need a propertyRef. You can do with a simple column definition in the many-to-one section.
来源:https://stackoverflow.com/questions/19632605/repeated-column-in-mapping-for-entity-where-each-column-is-only-mapped-once