You can easily make a persistent object transient, removing its persistent state from the database, with the delete) method. After the Session is closed, the object is considered an ordinary transient instance. The transient instance is destroyed by the garage collector if it's no longer referenced by any other object. Both the in-memory object instance and the persistent database row will have been removed.
Do I have to load an object to delete it? Yes, an object has to be loaded into the persistence context; an instance has to be in persistent state to be removed(note that a proxy is good enough). The reason is simple: You may have eHibernate interceptors enabled, and the object must be passed through these interceptors to complete its lifecycle. If you delete rows in the database directly, the interceptor won't run. Having said that, Hibernate offer bulk operations that translate into the direct SQL DELETE statements;
Do I have to load an object to delete it? Yes, an object has to be loaded into the persistence context; an instance has to be in persistent state to be removed(note that a proxy is good enough). The reason is simple: You may have eHibernate interceptors enabled, and the object must be passed through these interceptors to complete its lifecycle. If you delete rows in the database directly, the interceptor won't run. Having said that, Hibernate offer bulk operations that translate into the direct SQL DELETE statements;
Comments
Post a Comment