javax.persistence.Entitymanager: remove() method

前端 未结 4 1880
梦毁少年i
梦毁少年i 2021-01-31 19:00

Does remove(Object entity) method of EntityManager work only on those objects got from find() method?

I have following code snippet:

4条回答
  •  时光说笑
    2021-01-31 19:43

    Quoting from ObjectDB's manual on deleting JPA entity objects:

    In order to delete an object from the database it has to first be retrieved (no matter which way) and then in an active transaction, it can be deleted using the remove method.

    An IllegalArgumentException is thrown by remove if the argument is not a an instance of an entity class or if it is a detached entity.

    When creating object with new operator, it becomes a detached entity, you need to persist it if you want to remove it.

    When retrieving entity, you are retrieving persistent entity.

提交回复
热议问题