1

I have 2 classes and they're mapped as 1-to-1

class B mapping:

<one-to-one class="ClassA" constrained="true" name="a" property-ref="bId"/>

class A mapping:

<property insert="false" name="bId" type="integer" update="false">
      <column length="200" name="BID" not-null="false"/>
 </property>
 <many-to-one class="ClassB"  name="b">
       <column name="BID" not-null="false"/>
 </many-to-one>

when i call:

A a = (A) session.load(A.class, 1);    
session.delete(a.getB());

B record gets deleted, but BID in A stays intact. Why!? Any help is appreciated.

5
  • Actually, the mapping is n-to-1 (many-to-one) not 1-to-1 (one-to-one) for class A. Commented Mar 14, 2012 at 13:51
  • constrained="true" does nothing? Commented Mar 14, 2012 at 13:52
  • so i don't get Repeated column in mapping for entity Commented Mar 14, 2012 at 13:54
  • Do you have bId in A or reverse? Which is the parent in this relationship A or B? Commented Mar 14, 2012 at 14:17
  • I posted exact mappings. I don't know who's a parent :( Commented Mar 14, 2012 at 14:20

1 Answer 1

1

Yes, that's correct. Hibernate does not modify the object a. Hibernate can't do it. In the call session.delete(a.getB()) you give an instance of class B. This instance does not contain any reference to the object a.

You have to change a manually and update it.

An other possibility:

You move the foreign key from A to B (in one-to-one relations it doesn't matter which table has the foreign key). When you delete an instance of B then, you don't need to update A. [Or you use cascade=all,delete-orphan, then you can set the reference in a to null and update a, which will force hibernate to delete the instance of B. But I never tried this possibility.]

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for response. But it seams i can't change it manually cuz i have insert=false update=false in this property. And i cant move foreign key. Please help me!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.