concept delete in category ejb

appears as: delete, deletes, DELETE
EJB 3 in Action, Second Edition

This is an excerpt from Manning's book EJB 3 in Action, Second Edition.

  • DELETE— Deletes a specific resource. Performing this operation multiple times has no effect. Once the resource is deleted, all subsequent operations have no effect.
  • You’ve seen some examples of DELETE and UPDATE statements in JPQL in previous sections, but we avoided any in-depth discussion until now. Let’s assume that Action-Bazaar administrators need functionality to remove instances of entities such as User based on certain conditions. You start with the following code:

    @PersistenceContext em;
    . . .
    // start transaction
    Query query = em.createQuery("DELETE USER u WHERE u.status = :status ");
    query.setParameter("status", 'GOLD');
    int results = query.executeUpdate();
    //end transaction

    In this code, the use of UPDATE and DELETE statements is quite similar to using any other JPQL statements, except for two significant differences. First, you use the executeUpdate method of the Query interface to perform bulk updates and deletes instead of getResultList or getSingleResult. Second, you must invoke executeUpdate within an active transaction.

    Because bulk updates and deletes involve many pitfalls, we recommend that you isolate any bulk operations to a discrete transaction, because they’re directly translated into database operations and may cause inconsistencies between managed entities and the database. Vendors are required only to execute the update or delete operations and aren’t required to modify any changes to the managed entities according to the specification. In other words, the persistence provider won’t remove any associated entities when an entity is removed as a result of a bulk operation.

    JPQL supports three types of statements, as shown in table 11.1. You can use JPQL to perform selects, updates, and deletes in your queries.