• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

one-to-one: foreign key: save Problem

Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have one-to-one relationship between class A and class B

<class name="A" table="TEMPA" >
<id name="aaId" type="string" column="AID" length="10">
<generator class="assigned"></generator>

<one-to-one name="b" class="B" constrained="true" outer-join="false" cascade="save-update" />

<class name="B" table="TEMPB" lazy="true">
<id name="baId" type="string" column="BID" length="10">
<generator class="foreign">
<param name="property">a</param>

<one-to-one name="a" class="A" />

Problem 1: When i try to save A, it fires :
insert into HBLEARN1.TEMPB (BID) values ('1')
and throws error :
The insert or update value of the FOREIGN KEY "TEMPB.CC1246876555824" is not equal to any value of the
parent key of the parent table. SQLSTATE=23503

Why it is first trying to save 'B' instead of 'A' first ?

Problem 2: With this mapping, if i set <property name="hibernate.hbm2ddl.auto">create</property> in hibernate.cfg.xml,
It generates TEMPA whose aaId is the foreign key referencing TEMPB. This is also incorrect..
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you are trying to save instance of A, than instance of B attached to it is transient? If instance of B is transient it will try to save instance of B because you have cascade behavior set to save-update.

If possible use annotations to configure your mapping.

Won't you be my neighbor? - Fred Rogers. tiny ad:
the value of filler advertising in 2020
    Bookmark Topic Watch Topic
  • New Topic