Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception while deleting a Entity

 
krishna bulusu
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
E Lievaart
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 329
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
krishna bulusu
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 329
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 329
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic