Hi folks,
We are using Hibernate 2.1.6. Say we have the following situation:
A tire has a many-to-one relationship with a car. (from tire.hbm.xml)
<many-to-one name="car" class="com.company.component.library.model.main.Car" column="F_CAR_ID" cascade="save-update"/>
A car has a many-to-one relationship with the country in which it is manufactured . (from car.hbm.xml).
<many-to-one name="country" class="com.company.component.library.model.main.Country" column="F_Country"/>
If we want to delete a specific tire from the database by means of a named query:
FROM com.company.component.library.model.main.Tire
WHERE (object_id = :id)
in the following way:
- open session
- start transaction
- delete (statement)
- commit transaction
- close session
we get the following hibernate exception when implicitly flushing the session after a commit of the transaction.
net.sf.hibernate.HibernateException: identifier of an instance of com.company.component.library.model.main.Country
altered from 113
Code:
29 spaces
to 113
Code:
no spaces
at net.sf.hibernate.impl.SessionImpl.checkId(SessionImpl.java:2642)
at net.sf.hibernate.impl.SessionImpl.flushEntity(SessionImpl.java:2465)
at net.sf.hibernate.impl.SessionImpl.flushEntities(SessionImpl.java:2458)
at net.sf.hibernate.impl.SessionImpl.flushEverything(SessionImpl.java:2260)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2239)
at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
In the constructor of the country we do a trim() of the id (String).
Why does hibernate gets the Country information in a delete and why is it performing the check? Why is the String id not trimmed when the id is requested from the database ? How can we avoid this problem without removing the trim in the constructor of country.
Thanks in advance,
Ron
Code: