4

How can fix this thing

Repeated column in mapping for entity: com.abc.domain.PersonConnect column: PERSON_ID (should be mapped with insert="false" update="false")

this is snippet from my hbm file

<class name="com.abc.domain.PersonConnect" table="PERSON_CONNECT">    
    <composite-id>
        <key-many-to-one name="Parent" class="com.abc.domain.Person" column="PARENT_PERSON_ID"/>
        <key-many-to-one name="Child" class="com.abc.domain.Person" column="CHILD_PERSON_ID"/>
    </composite-id>

    <many-to-one class="com.abc.domain.Person" fetch="select" name="parent" lazy="false" > 
        <column length="20" name="PERSON_ID" not-null="true"/> 
    </many-to-one> 
    <many-to-one class="com.abc.domain.Person" fetch="select" name="child" lazy="false" > 
        <column length="20" name="PERSON_ID" not-null="true"/> 
    </many-to-one>    
</class>

and the table goes like this

Person_Connect

  • PK - PARENT_PERSON_ID
  • PK - CHILD_PERSON_ID

Person

  • PK - PERSON_ID
  • FNAME
  • LNAME
Pradeep Kumar
  • 79
  • 1
  • 10

2 Answers2

2

Your mapping is wrong, this is the correct mapping. On the many-to-one side the column name is the column in the same table which is a foreign referring the primary key of Person.

<class name="com.abc.domain.PersonConnect" table="PERSON_CONNECT">

 <composite-id>
    <key-many-to-one name="Parent" class="com.abc.domain.Person" column="PARENT_PERSON_ID"/>
    <key-many-to-one name="Child" class="com.abc.domain.Person" column=" CHILD_PERSON_ID"/>
     </composite-id>

</class>
gkamal
  • 20,777
  • 4
  • 60
  • 57
  • ouch !!! i see that I confused two different thing and tried to put everything at same place. Let me try that and I will confirm if it worked. Thanks again. – Pradeep Kumar Sep 28 '11 at 03:20
  • Okay that takes the problem out of composite-id but when when I am trying to define Many-to-one foreign key I am back in same situation. – Pradeep Kumar Sep 28 '11 at 03:37
  • same fix - many-to-one always refer to the foreign key of the other table - whether it is key or not doesn't matter. – gkamal Sep 28 '11 at 10:08
1

Well, for one, it seems unlikely that both "Parent" and "Child" should be mapped to the same column. That's probably a problem. Otherwise, do what the error says, and add insert="false" update="false" to one of the column mappings. A column can only "belong" to a single property. Otherwise you can get into unresolvable situations where one property says the value should be x and the other says it should be y.

Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199
  • I dont see a reason why its un-likely. A person has two parents and we need to map it their ID and has two kids and need to map it to their ID aswell. – Pradeep Kumar Sep 28 '11 at 02:44
  • @Pradeep: Yes, but that mapping says that the Parent and the Child are the same person. You're only convincing me that instead of a single PERSON_ID column, you should be mapping to two separate PARENT_ID and CHILD_ID columns. – Ryan Stewart Sep 28 '11 at 02:50
  • I should have added table at first. Please elaborate more how can a parent be a child in situation like this. – Pradeep Kumar Sep 28 '11 at 03:04
  • @Pradeep: You just proved my point. In the PersonConnect class, in a many-to-one (either part of a composite id or not), you should be giving the column name of the column in the Person_Connect table. There is no PERSON_ID in that table. The column names you map to need to be PARENT_PERSON_ID and CHILD_PERSON_ID. Get rid of your fixation on PERSON_ID. It shouldn't be mentioned in the PersonConnect mapping at all. – Ryan Stewart Sep 28 '11 at 04:21
  • okay got that, but how will it understand that where is it mapping to in Person – Pradeep Kumar Sep 28 '11 at 04:36
  • As [per the reference guide](http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/mapping.html#mapping-declaration-manytoone), relationships default to using the primary key of the target entity. Hibernate knows you're mapping to a Person, and it knows what Person's primary key is. There's no need to give it any other information. – Ryan Stewart Sep 28 '11 at 11:37