-->
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.  [ 7 posts ] 
Author Message
 Post subject: Error while deletion in one-tomany mapping
PostPosted: Sun Mar 01, 2009 11:28 pm 
Newbie

Joined: Mon Feb 23, 2009 1:26 am
Posts: 13
Hi,
I have two tables A,B.
A --> B one-to-many
B has a FK contraint from Table A.
B also has its own primary key.
When I delete the record in A i want to delete all the dependent records in table B.
I want to set only the primary key of A and get the above delete functionaly.
but doing this throws below exception.Where the Id is the primary key in A and a FK in B.

Thank you in advance.



- SQL Error: 1048, SQLState: 23000
- Duplicate key or integrity constraint violation, message from server: "Column 'Id' cannot be null"
- Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:253)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:169)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at com.agdelta.trade.marketdata.dao.VolSurfaceDAO.deleteVolSurface(VolSurfaceDAO.java:106)
at com.agdelta.trade.marketdata.volSurfaceTest.testVolSurface(volSurfaceTest.java:71)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: java.sql.BatchUpdateException: Duplicate key or integrity constraint violation, message from server: "Column 'Id' cannot be null"
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1461)
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeBatch(NewProxyPreparedStatement.java:1723)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 02, 2009 1:43 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
You mean you want to set the Primary key of A to null and that should delete the related record in B? Why do u want to do that?

You can never set null to primary key field. PK is always unique and no-null. Why are you tryng to set PK as null? If we kno tht may be we can solve it the correct way.

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 02, 2009 2:29 am 
Newbie

Joined: Mon Feb 23, 2009 1:26 am
Posts: 13
No.I want to just set the primary key of A and want to delete all records in A as wel as B.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 02, 2009 2:38 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
I am really confused nw. Lets take an eg.

Table A (id <PK>, col1_A)
-----------------------------
1, ABC
2, XYZ

Table B (id <PK>, col2_B, A_ID <FK>)
---------------------------------------------
10, AAA, 1
11, BBB, 1
12, CCC, 2

Now you do:
A a = session.get(A.class, 1);

Then you do:
a.id = null;
session.update(a);
tx.commit();

You want this to result in:
delete from B where A_ID=1;
delete from A where ID=1;

Is this your requirement?

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 02, 2009 2:51 am 
Newbie

Joined: Mon Feb 23, 2009 1:26 am
Posts: 13
Thx for the reply.Howver i want to achieve the following.

a.id = 1;
session.delete(a);
tx.commit();

result in:
delete from B where A_ID=1;
delete from A where ID=1;

do i need to do A a = session.get(A.class, 1); before deleting


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 02, 2009 3:18 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
Yes you can do this by putting cascade="delete" in the one-to-many mapping.

But you will have to load the parent class before deleting otherwise the cascade wont work.

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 02, 2009 3:23 am 
Newbie

Joined: Mon Feb 23, 2009 1:26 am
Posts: 13
ok.thank you.
I will try that and will let you know if any issues.
Regards
Akshay


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