-->
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.  [ 3 posts ] 
Author Message
 Post subject: Problem creating bidirection many-to-one association
PostPosted: Mon May 17, 2010 2:28 pm 
Newbie

Joined: Mon May 17, 2010 11:43 am
Posts: 2
I'm currently trying to setup a bidirectional many-to-one association and keep getting a constraint violation when I run my regression tests. Any thoughts on what I have done wrong would be appreciated. I'm using Hibernate (3.5.1-Final) with MySQL (5.1.35-community). I have isolated my problem to a test case with an entity A that references a single entity B while entity B references a collection of entity A's. The mappings for these are as follows

Code:
<hibernate-mapping package="ssss.domain">
  <class name="A" table="A" >
    <id name="id" column="A_ID" type="long">
        <generator class="native"/>
    </id>
    <many-to-one name="b" column="B_ID" class="B" not-null="true"/>
  </class>
</hibernate-mapping>


Code:
<hibernate-mapping package="ssss.domain">
  <class name="B" table="B" >
    <id name="id" column="B_ID" type="long">
        <generator class="native"/>
    </id>
    <bag name="a" inverse="true">
       <key column="A_ID" not-null="false"/>
       <one-to-many class="A"/>
    </bag>
  </class>
</hibernate-mapping>


I'm running test in a Spring framework with transactions automatically inserted around each test. My test code is given below, the constraint violation happens when the save on the bean is called.

Code:
@Test
public void testBValue() {
  final A bean = createInitialisedA();
  final B value = createInitialisedB();
  save(value);
  bean.setB(value);
  save(bean);
}


The constraint violation is
Code:
[WARN,JDBCExceptionReporter,main] SQL Error: 1452, SQLState: 23000
[ERROR,JDBCExceptionReporter,main] Cannot add or update a child row: a foreign key constraint fails (`ssss`.`a`, CONSTRAINT `FK41212E88BF` FOREIGN KEY (`A_ID`) REFERENCES `b` (`B_ID`))


I've tried looking at the schema that results and the constraint FK41212E88BF in the ddl, below, looks wrong, but I don't know what part of my mapping is causing it.

Code:
create table A (A_ID bigint not null auto_increment, B_ID bigint not null, primary key (A_ID))
create table B (B_ID bigint not null auto_increment, primary key (B_ID))
alter table A add index FK41212EFD1E (B_ID), add constraint FK41212EFD1E foreign key (B_ID) references B (B_ID)
alter table A add index FK41212E88BF (A_ID), add constraint FK41212E88BF foreign key (A_ID) references B (B_ID)


Top
 Profile  
 
 Post subject: Re: Problem creating bidirection many-to-one association
PostPosted: Mon May 17, 2010 4:56 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Code:
alter table A add index FK41212E88BF (A_ID), add constraint FK41212E88BF foreign key (A_ID) references B (B_ID)


is not correct and I think it is caused by

Code:
<key column="A_ID" not-null="false"/>


in the <bag> element. The column should be B_ID.


Top
 Profile  
 
 Post subject: Re: Problem creating bidirection many-to-one association
PostPosted: Tue May 18, 2010 2:35 am 
Newbie

Joined: Mon May 17, 2010 11:43 am
Posts: 2
Thanks. I'd been looking at this for too long and needed a fresh pair of eyes to spot the silly mistake.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.