Got classical Hibernate problem:
Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: entry.Authority column: username (should be mapped with insert="false" update="false")
users.hbm.xml:
<class name="entry.User" table="users">
<property name="username" column="username"/>
<set name="authorities" table="authorities" lazy="false" cascade="all">
<key column="username" not-null="true"/>
<one-to-many class="entry.Authority"/>
</set>
authorities.hbm.xml:
<class name="entry.Authority" table="authorities">
<id name="id"/>
<property name="username" column="username"/>
</class>
Tried classical solution:
<property name="username" column="username" insert="false" update="false"/>
Got:
java.sql.BatchUpdateException: Field 'username' doesn't have a default value
Why I can’t specify username twice in .xml?
As I understand this declaration <property name="username" column="username"/> relates to User and <key column="username" not-null="true"/> relates to Authorities
.So why they clash?
How can it be fixed with minimum invasion?
EDITED:
<class name="entry.User" table="users">
<id name="id">
<generator class="increment" />
</id>
<property name="username" column="username"/>
<set name="authorities" table="authorities" lazy="false" cascade="all" inverse="true">
<key column="username" not-null="true"/>
<one-to-many class="entry.Authority"/>
</set>
...
and
<class name="entry.Authority" table="authorities">
<id name="id"/>
<property name="username" column="username"/>
<property name="authority" column="authority"/>
<many-to-one name="user" class="entry.User">
<column name="username"/>
</many-to-one>
</class>
With the key definition in users.hbm.xml
<key column="username" not-null="true"/>you already handle the column username in Authorities. The line<property name="username" column="username"/>in authorities.hbm.xml is superfluous, even if used in many-to-one. Every time you load/update or insert the instances of Authorities via Users, the field is handled automatically by Hibernate. If you define the field also in authorities.hbm.xml then it is set twice – that’s why you got your error message.If you by some special reasons really want to have the username also in authorities.hbm.xml, then you have to specify insert/update = false (as you already did) and a default value to avoid your error message (even if the default never is used). For example
But I would simply recommend this:
Added after your comment:
Now I see a problem in your new mapping: the
<key>element has to reference to the key of the parent table, and that is not the case. The key in the Users table is id, but you use a normal property Username as the foreign key in Authority. There are two possibilities to resolve this:1) You make Username the key of Users (drop column id and define the key in Users as
(I’m not sure if in this case in new instances of Authority hibernate will set the member username automatically or if you have to do it manually.)
new: Here is the complete mapping (not tested):
2) You make id the foreign key in Authority:
In users.hbm.xml
, and in authority.hbm.xml you replace the column username with
You also have to modify the database table then.