-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Hibernate updates the PK of the child in a one-to-many list
PostPosted: Fri Aug 25, 2017 9:02 am 
Newbie

Joined: Fri Aug 25, 2017 8:57 am
Posts: 3
I am upgrading the spring and hibernate jars to 4 and i am facing issues with one to many mappings.

This is my parent class Evn mapping config xml:


This is my AddressRole class config xml,

Code:
<class name="Evn" table="EVN" select-before-update="true">
   <id name="id" type="java.lang.Long">
      <column name="ID" />
      <generator class="sequence">
         <param name="sequence">EVN_ID_SEQ</param>
      </generator>
   </id>
   <list name="addressRoles" inverse="true" fetch="subselect" lazy="false"
      cascade="all">
      <key>
         <column name="fk_evn_Id" not-null="true" />
      </key>
      <index column="ROLE" type="int"></index>
      <one-to-many class="AddressRole" />
   </list>
   <list name="histories" inverse="true" cascade="all" fetch="subselect"
      lazy="true">
      <key>
         <column name="fk_evn_Id" not-null="true" />
      </key>
      <index column="ID" type="java.lang.Long"></index>
      <one-to-many class="History" />
   </list>

This is my History class config xml,

Code:
<id name="id" type="java.lang.Long">
   <column name="ID" />
   <generator class="sequence">
      <param name="sequence">HISTORY_ID_SEQ</param>
   </generator>
</id>
<many-to-one name="evn" class="Evn" cascade="none">
   <column name="fk_evn_id" not-null="true" />
</many-to-one>


Using the below block of code i am trying to insert insert the data into DB,

Code:
Session session = getHibernateTemplate().getSessionFactory()
      .getCurrentSession();
try {
   LOG.info("transientInstance: " + transientInstance.toString());
   // transaction = session.getTransaction();
   long id = (Long) session.save(transientInstance);
   session.flush();


Here the all the child classes in the parent class are getting inserted successfully into DB. Then hibernate is trying to execute the update commands for one to many association but it is failing due to SQLIntegrityConstraintViolationException.

Below are the SQL query is being executed by Hibernate for one to many relationship,

Code:
2017-08-24 18:53:39,745 DEBUG [https-openssl-nio-8743-exec-5::CHY4001] spi.SqlStatementLogger -

update
    EVN.ADDRESSROLE
set
    ROLE=?
where
    id=?

2017-08-24 18:53:39,745 DEBUG [https-openssl-nio-8743-exec-5::CHY4001] spi.SqlStatementLogger -

update
    EVN.HISTORY
set
    ID=?
where
    ID=?


I am getting SQLIntegrityConstraintViolationException on the EVN.HISTORY table. It is trying to update the primary key value as 0 that is why i am getting SQL exception. But could not figure out how is that happening.

Same mapping is working without any issue on hibernate 3.5, but it is giving issue in hibernate 4.3.10.

Can anyone please help on this issue?


Top
 Profile  
 
 Post subject: Re: Hibernate one to many mapping issue
PostPosted: Fri Aug 25, 2017 9:47 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
The problem is most likely caused by this line:

Code:
<index column="ID" type="java.lang.Long"></index>


Hibernate tries to maintain the list order using the ID column which is wrong since this should be the PK.

You need to change it to something else:

Code:
<index column="order_ID" type="java.lang.Long"></index>


Top
 Profile  
 
 Post subject: Re: Hibernate one to many mapping issue
PostPosted: Fri Aug 25, 2017 10:17 am 
Newbie

Joined: Fri Aug 25, 2017 8:57 am
Posts: 3
vlad wrote:
The problem is most likely caused by this line:

Code:
<index column="ID" type="java.lang.Long"></index>


Hibernate tries to maintain the list order using the ID column which is wrong since this should be the PK.

You need to change it to something else:

Code:
<index column="order_ID" type="java.lang.Long"></index>


This field
Code:
<index column="ID" type="java.lang.Long"></index>
is PK in the table History. The value for this field will be generated from sequence. I tried to change other field in the table but the value is getting updated as 0.


Top
 Profile  
 
 Post subject: Re: Hibernate updates the PK of the child in a one-to-many list
PostPosted: Fri Aug 25, 2017 10:47 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
The index of the list should never be the entity identifier. The entity identifier needs to be generate from a sequence while the index always takes a value from 0 to N - 1, where N is the size of the list.

So, you need to add a new field, as I suggested you.


Top
 Profile  
 
 Post subject: Re: Hibernate updates the PK of the child in a one-to-many list
PostPosted: Fri Aug 25, 2017 2:33 pm 
Newbie

Joined: Fri Aug 25, 2017 8:57 am
Posts: 3
vlad wrote:
The index of the list should never be the entity identifier. The entity identifier needs to be generate from a sequence while the index always takes a value from 0 to N - 1, where N is the size of the list.

So, you need to add a new field, as I suggested you.


Thanks vlad, I will add the new field as suggested by you and try to run the application. But the same configuration is working fine in hibernate 3.5. So this changes are newly implemented in hibernate 4.
In case If it is available since hibernate 3, am i missing any configuration changes in hibernate 4.


Top
 Profile  
 
 Post subject: Re: Hibernate updates the PK of the child in a one-to-many list
PostPosted: Fri Aug 25, 2017 4:03 pm 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Both Hibernate 3 and 4 are no longer supported. Even if you find an issue, it will not be fixed. So, if you really want to upgrade, it's better to upgrade to 5.x.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.