aspose file tools*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Exception while deleting a Entity Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Exception while deleting a Entity" Watch "Exception while deleting a Entity" New topic
Author

Exception while deleting a Entity

krishna bulusu
Ranch Hand

Joined: Aug 28, 2006
Posts: 185
Hi Folks,
I have the following Bean code:

My client code is as follows:

When excecuting the above client, I am getting the following exception:
javax.persistence.EntityNotFoundException: deleted entity passed to persist: [bala.persistent.Employee#<null>]
(same the case with sateless session bean in the above case)

If I make the following change for PersistenceContext it works fine:
@PersistenceContext(type=PersistenceContextType.EXTENDED)

Could anyone explain why this behaviour?


Thanks&Regards, Krishna.
SCJP1.4, SCWCD1.4, SCBCD 5.0
E Lievaart
Greenhorn

Joined: Jul 06, 2008
Posts: 28
Hmm, that's strange since you're calling merge on it first.
It should be all right.

The entity does exist in the database right?
Does deleting the entity violate a FK constraint somewhere?

What happens if you use:
entity.remove(entity.getReference(emp.getPK()));

instead of:
entity.remove(entity.merge(emp));
Sergio Tridente
Ranch Hand

Joined: Mar 22, 2007
Posts: 329

Make sure the entity actually exists in the database. I have tried the code you posted (with some modifications) and everything worked fine.

This is my implementation of CopmpanyBean (I just added the insertEmployee() business method):


And here's my client's code:


As you can see, I have simply added some code to make sure the employee exists.
Can you tell us in what circumstances you are running the example?


SCJP 1.4 (88%) - SCJP 5.0 Upgrade (93%) - SCWCD 1.4 (97%) - SCBCD 5.0 (98%)
krishna bulusu
Ranch Hand

Joined: Aug 28, 2006
Posts: 185
If you observe closely my client code. I am printing the company name after the retrieval of the company entity from the database. It works simply fine if I am changing the persistent context type to extended.
I am posting the Employee Bean:
krishna bulusu
Ranch Hand

Joined: Aug 28, 2006
Posts: 185
More over, If the entity doesn't existing in the database, I think the system throws Entity doesn't existing exception upon deleting the entity rather than the exception I posted earlier.
krishna bulusu
Ranch Hand

Joined: Aug 28, 2006
Posts: 185
Hi E Lievaart,
It is working fine If I am using the following method instead of merge:

And another thing is that, I have a manager entity which has one-to-many relation with the employee.
followig is the code:

If I am trying to delete the manager useing the following code, it simply working fine but the same code is not working for the employee entity.

can you throw some light on it?
E Lievaart
Greenhorn

Joined: Jul 06, 2008
Posts: 28
Hey Krishna,

My best guess here is that you get the exception due to a FK constraint.
In JPA it is the programmers job to ensure the references are valid.
Try setting the references to null after you merge, before you remove.
So set Employee.manager to null and remove the Employee from Manager.employees.
Sergio Tridente
Ranch Hand

Joined: Mar 22, 2007
Posts: 329

Hi Krishna,

I agree with E Lievaart. You must be performing the actions in the wrong order.

I have tried again with both entities and everything is working fine. Could you please post your complete bean's and client's code?
krishna bulusu
Ranch Hand

Joined: Aug 28, 2006
Posts: 185
If it is FK constraint problem, then system would throws the same exception.
I habe already posted the entities code.
I am posting the Bean and the client code.
I am using JBoss 4.2.2 server.

Client code:


Beans code:

Try it your self and let me know the result.
Sergio Tridente
Ranch Hand

Joined: Mar 22, 2007
Posts: 329

Hi Krishna,

I finally got an exception. Let me explain what I did.

At the beginning my client was doing the following:
1) Insert manager
2) Insert employee
3) Delete employee
4) Delete manager

Everething in the same client execution: I did not get any errors.

At a given time and after trying several combinations, I got the following exception: javax.persistence.EntityExistsException (Cannot persist detached object). This is what I did:
1) Insert the manager
2) Insert the client
3) Delete the client
At this point my client execution finished, and in the next execution I tried to:
4) Insert the client ---> EntityExistsException

it works fine after changing the bean in the following way:


I also needed to implement the equals() method in the Employee class (to make List.remove() work properly);

Try the changes I am proposing and tell me if it works OK.
[ July 22, 2008: Message edited by: Sergio Tridente ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception while deleting a Entity