Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

org.hibernate.MappingException: Repeated column in mapping for entity

Tags:

java

hibernate

I am using Hibernate 3.2.5. I am getting the above exception while using many-to-one mapping. The training table is having a many to one relation with Department table, i.e. One Depatement is capable of taking more than one training.

The exception is asking me to add insert="false" update="false" in my hbm file. If I add this bit in hbm file, then the code works fine.

Here is the hbm file:

<?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">
<hibernate-mapping>

  <class name="com.infy.model.Training" table="training">
  <id name="Id" type="integer" column="ID">
      <generator class="assigned"></generator>
  </id>      
  <property name="trainerName">
      <column name="TRAINER_NAME"></column>
  </property>
  <property name="deptId">
      <column name="DEPT_ID"></column>
  </property>
  <property name="trainingSubject">
      <column name="TRAINING_SUBJECT"></column>
  </property>
  <many-to-one name="departmentDetails" column="DEPT_ID"></many-to-one>
  </class>
</hibernate-mapping>

If I change this line to:

<many-to-one name="departmentDetails" column="DEPT_ID" insert="false" update="false"></many-to-one>

Then the code works. I want to know what is the exact reason for adding this.

Regards,

like image 602
user182944 Avatar asked Nov 10 '12 14:11

user182944


3 Answers

You have mapped the DEPT_ID column twice, here:

  <property name="deptId">
      <column name="DEPT_ID"></column>
  </property>

And here:

  <many-to-one name="departmentDetails" column="DEPT_ID"></many-to-one>

When executing a select statement, Hibernate will be fine populating two properties of your object from the same column, however when doing an insert or an update it cannot decide which property to persist in the database.

Why do you need two properties mapped to the same column in the first place? If you need access to the deptId, you can probably remove the deptId property and instead do

training.getDepartmentDetails().getId()
like image 70
Guillaume Avatar answered Oct 22 '22 01:10

Guillaume


The error message for this scenario is quite clear (you haven't put it here, but I've seen it a few times). The problem is that you've mapped the column DEPT_ID to two different fields in your class.

First, you've mapped it to the property deptId and then to departmentDetails. As you found out, hibernate allows to do this only if one of the mappings is configured to be insert="false" update="false".

The reason is quite simple. If you would change deptId to another id, hibernate would need to change the class that is mapped in departmentDetails, which is quite complicated.

if you need to get the deptId, you can add a getDeptId method on Training that returns departmentDetails.getId(). And don't provide a setDeptId.

like image 27
Augusto Avatar answered Oct 21 '22 23:10

Augusto


If you are using the same column name twice in your mapping file. might be you get mapping Exception

Initial SessionFactory creation failed.org.hibernate.MappingException:

Also if u mark insert=flase and update=false .

if u try to update or insert in records in table or another legacy system try to update these column value. it wouldn't update or insert that filed.

Please check the below link .it will help to find your solutions.

http://www.techienjoy.com/hibernate-insert-update-control.php

Thanks Sandeep G.

like image 41
sandeep gupta Avatar answered Oct 21 '22 23:10

sandeep gupta