-->
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.  [ 11 posts ] 
Author Message
 Post subject: Cascading problem?
PostPosted: Fri Sep 10, 2010 5:06 am 
Newbie

Joined: Fri Sep 10, 2010 5:00 am
Posts: 7
Hey guys, I'm using hibernate on my project, and I've got a parent class & child class in a onetoone relationship, with the parent having a cascade=ALL to the child. When I'm persisting the parent, I see that the db is queried for the child (to see if it already exists, which it doesn't, the table is empty), and right after hibernate tries to insert the parent before the child, thus giving a FK constraint error. Do you know anything about this?

Code:
@Entity
@Table(name = "LANOMAL")
public class LAnomal extends Throwable implements java.io.Serializable {

[...]

   @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
   @PrimaryKeyJoinColumn
   public LStacktrace getStacktraceString() {
      return stacktraceString;
   }

}

@Entity
@Table(name = "LSTACKTRACE")
public class LStacktrace implements java.io.Serializable {

[...]

   @OneToOne(fetch = FetchType.LAZY, mappedBy = LAnomal.PROP_LSTACKTRACE)
   public LAnomal getException() {
      return this.exception;
   }

}


Quote:
INFO: Hibernate: select lanomal0_.DTCREAT as DTCREAT29_1_, lanomal0_.ADAMID as ADAMID29_1_, [...] from LANOMAL lanomal0_ left outer join LSTACKTRACE lstacktrac1_ on lanomal0_.DTCREAT=lstacktrac1_.DTCREAT where lanomal0_.DTCREAT=?
INFO: Hibernate: select lstacktrac0_.DTCREAT as DTCREAT49_0_, lstacktrac0_.CKEEP as CKEEP49_0_, [...] from LSTACKTRACE lstacktrac0_ where lstacktrac0_.DTCREAT=?
INFO: Hibernate: insert into LANOMAL (ADAMID, ANOMAL, CKEEP, DARCH, NOMCLASSE, CORELID, SLEUTEL, MSGID, METHODE, DTWIJZ, module, PACKAGE, RECRC, CTRAIT, USERID, DTCREAT) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

WARNING: 14578 [SimpleAsyncTaskExecutor-33] WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: -530, SQLState: 23503

WARNING: 14578 [SimpleAsyncTaskExecutor-33] ERROR org.hibernate.util.JDBCExceptionReporter - DB2 SQL error: SQLCODE: -530, SQLSTATE: 23503, SQLERRMC: EVAADM.LANOMAL.FKLANOMAL


Any help would be greatly appreciated!


Top
 Profile  
 
 Post subject: Re: Cascading problem?
PostPosted: Mon Sep 13, 2010 4:57 am 
Newbie

Joined: Fri Sep 10, 2010 5:00 am
Posts: 7
*bump*

Could anyone take a look at this please? It would greatly help me along.

Thanks in advance!


Top
 Profile  
 
 Post subject: Re: Cascading problem?
PostPosted: Tue Sep 14, 2010 6:15 am 
Newbie

Joined: Fri Sep 10, 2010 5:00 am
Posts: 7
Anyone who knows this issue?


Top
 Profile  
 
 Post subject: Re: Cascading problem?
PostPosted: Tue Sep 14, 2010 7:11 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
and right after hibernate tries to insert the parent before the child, thus giving a FK constraint error.


I have not looked into the details but the parent *should* be inserted before the child, so either you should change the foreign key definition in the database or you should switch the parent/child relationship around on your mappings.


Top
 Profile  
 
 Post subject: Re: Cascading problem?
PostPosted: Tue Sep 14, 2010 7:22 am 
Newbie

Joined: Fri Sep 10, 2010 5:00 am
Posts: 7
nordborg wrote:
Quote:
and right after hibernate tries to insert the parent before the child, thus giving a FK constraint error.


I have not looked into the details but the parent *should* be inserted before the child, so either you should change the foreign key definition in the database or you should switch the parent/child relationship around on your mappings.


But how can you insert the parent before the child if it has a reference to the child? The child won't exist yet in the database, and the reference to the child in the parent will be null.

Is the child the one who has a reference to the parent then?


Top
 Profile  
 
 Post subject: Re: Cascading problem?
PostPosted: Tue Sep 14, 2010 7:51 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
Is the child the one who has a reference to the parent then?


Yes! Just as in the real world a child can't exist without a parent. In a one-to-one relation it is not so obvious which one is the parent and which one is the child. Usually both hold references to each other. I am not so familiar with annotations but when using hbm.xml mapping files you simply put constrained="true" on the "child" side to indicate that there is a foreign key constraint and that the other side should be inserted first. I think the mappedBy attribute has a similar function.


Top
 Profile  
 
 Post subject: Re: Cascading problem?
PostPosted: Tue Sep 14, 2010 8:02 am 
Newbie

Joined: Fri Sep 10, 2010 5:00 am
Posts: 7
nordborg wrote:
Quote:
Is the child the one who has a reference to the parent then?


Yes! Just as in the real world a child can't exist without a parent. In a one-to-one relation it is not so obvious which one is the parent and which one is the child. Usually both hold references to each other. I am not so familiar with annotations but when using hbm.xml mapping files you simply put constrained="true" on the "child" side to indicate that there is a foreign key constraint and that the other side should be inserted first. I think the mappedBy attribute has a similar function.


Ah okey!

If you look at my code snippet I pasted in my original post, then I assume I need to put constrained=true on the getStackTraceString() method of the Anomal class?


Top
 Profile  
 
 Post subject: Re: Cascading problem?
PostPosted: Tue Sep 14, 2010 8:26 am 
Regular
Regular

Joined: Fri Aug 06, 2010 1:49 am
Posts: 102
Location: shynate26@gmail.com
HI Krosan,

This can be resolved in simple object oriented concept.

Say u are creating one parent & One child.
now problem is that child reference does not have any value. It can resolve using,

parent.setChildReference(Child);
now, child.setParentReference(this);

class Parent{

Child childObj;

public void setChildObj(Child child){
this.childObj=child;
this.childObj.setParentObj(this);
}
}

_________________

Cheers!
Shynate
mailto:shynate26@gmail.com
www.CSSCORP.com


Top
 Profile  
 
 Post subject: Re: Cascading problem?
PostPosted: Tue Sep 14, 2010 8:35 am 
Newbie

Joined: Fri Sep 10, 2010 5:00 am
Posts: 7
Hey, thanks for the reply.

I think I've got what you propose already implemented. I didn't paste the entire classes in my initial post, so I'll link you a pastebin with both classes:

LAnomal.java: http://pastebin.com/yW3CkVkL
LStacktrace.java: http://pastebin.com/g7iv9pGr


Top
 Profile  
 
 Post subject: Re: Cascading problem?
PostPosted: Tue Sep 14, 2010 8:50 am 
Regular
Regular

Joined: Fri Aug 06, 2010 1:49 am
Posts: 102
Location: shynate26@gmail.com
Could you post your service layer snippet.

_________________

Cheers!
Shynate
mailto:shynate26@gmail.com
www.CSSCORP.com


Top
 Profile  
 
 Post subject: Re: Cascading problem?
PostPosted: Tue Sep 14, 2010 9:03 am 
Newbie

Joined: Fri Sep 10, 2010 5:00 am
Posts: 7
shynate26 wrote:
Could you post your service layer snippet.


Here you go: http://pastebin.com/XPsSu0r8


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