-->
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.  [ 1 post ] 
Author Message
 Post subject: Not-Null foreign key column being set to null before DELETE
PostPosted: Mon Nov 03, 2008 5:31 am 
Newbie

Joined: Mon Nov 03, 2008 4:36 am
Posts: 1
Hi

Here's the situation: I have two tables, MASTER and DETAIL. DETAIL refererring MASTER by a foreign key column MASTER_ID on the primary key of MASTER, wich is MASTER.MASTER_ID. All FK-Columns in our data model are not nullable.


Hibernate version: 3.2.6

Mapping documents:

Mapping in MASTER:

Code:
@Id
@Column(name = "MASTER_ID", nullable = false, precision = 12, scale = 0)
public Long getMasterId() {
  return this.masterId;
}

@OneToMany(fetch = FetchType.LAZY)
@JoinColumn(name = "MASTER_ID")
@Cascade(CascadeType.ALL)
public List<Detail> getDetails() {
  return details;
}


Maping in class Detail:

Code:
@Column(name = "MASTER_ID", nullable = false, precision = 12, scale = 0)
public Long getMasterId() {
return this.masterId;
}


As you can see the mapping in unidirectional.

Now, on calling session.delete(myDetail), the following SQL is generated:

Code:
DEBUG SQL - update DETAIL set MASTER_ID=null where MASTER_ID=?


SET NULL on a NOT NULL-Column is a very bad thing to do!

If i ignore the exception thrown and continue with saving my MASTER, then the correct DELETE statement is generated.

Code:
DEBUG SQL - delete from DETAIL where DETAIL_ID=? and VERSION=?



Now, i tried to be smart by setting the join column to nullable=false:

Code:
@OneToMany(fetch = FetchType.LAZY)
@JoinColumn(nullable = false, name = "MASTER_ID")
@Cascade(CascadeType.ALL)
public List<Detail> getDetails() {
  return details;
}


Resulting in this at deploy-Time:

Code:
org.hibernate.MappingException: Repeated column in mapping for entity: test.Detail column: MASTER_ID (should be mapped with insert="false" update="false")


Huh ???

I did some research on this error. It might be of importance, that there are several other entities referencing the MASTER, all with a not nullable foreign key from DETAIL2.MASTER_ID or DETAIL3.MASTER_ID on MASTER.MASTER_ID.

AFAIK, Hibernate seems to have a problem with multiple collections sharing the same join column?!

Name and version of the database you are using:

Oracle Server Version : Oracle9i Release 9.2.0.4.0 - Production

I'd be gratefull for any hint. J just want to delete a simple DETAIL...

Bye

Holger


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

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.