Hi parmendratyagi
Thanks for a quick response.
After reading that part of the book twice, I figured out what I was doing wrong.
I believe I am seriously misled by the exception's description:
Code:
Exception in thread "main" java.lang.IllegalArgumentException: Removing a detached instance com.samples.MyEntity#30
at org.hibernate.ejb.event.EJB3DeleteEventListener.performDetachedEntityDeletionCheck(EJB3DeleteEventListener.java:47)
at org.hibernate.event.def.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:86)
at org.hibernate.event.def.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:52)
at org.hibernate.impl.SessionImpl.fireDelete(SessionImpl.java:766)
at org.hibernate.impl.SessionImpl.delete(SessionImpl.java:744)
at com.samples.MyEntityHome(MyEntityHome.java:82)
at com.samples.MyMain.main(Executor.java:70)
There's nothing wrong with deleting detach objects, what I was really doing before when that exception was thrown was, I was deleting a transient object, but the exception clearly states that I was trying to delete a "detached" object instead of saying I was trying to delete a "transient" object.
I tried deleting a detach objects and it was good. The difference between a transient and detached object wasn't clear to me until I read the book twice and tried experimenting with it.
So, I believe it's not really about whether I'm using a "JPA version" of hibernate or native hibernate.
Quote:
This is correct if you use Hibernate Native for Peristence.
As you know Hibernate also implement (partially) JPA specification throw using
Entity Manager
Though your response made me make experiment using native Hibernate and it turns out that it is OK to delete a transient object, so you made half of the point valid. There's a difference between JPA hibernate and native with regards to handling transient objects, but not with regards to detached objects.
I'm sorry I was just a bit confused, and really, the exception is very misleading, it must have said:
"Removing a transient instance com.samples.MyEntity#30"
instead of:
"Removing a detached instance com.samples.MyEntity#30"
when I was really using a transient instead of detached object.
So there's no way of directly deleting an object without that object going from a persistent state after all, ei? Because in the graph shown in the book, there's no transition from transient to detached state without taking a path to persistent state. The graph was even incomplete, it doesn't show a path from detached state to removed state.
I think we're done here, thanks anyways, please post a reply if you think I'm missing something or you want to add up something that could help me understand more about this matter.