Java Reference
In-Depth Information
was necessary because of entity beans. The fact that EJB 3 detached entities are nothing
but POJOs makes the DTO anti-pattern less of a necessity of life. Instead of having to cre-
ate separate DTOs from domain data just to pass back and forth between the business and
presentation layers, you may simply pass detached entities. This is exactly the model fol-
lowed in this chapter.
But if your entities contain behavior, you might be better off using the DTO pattern anyway,
to safeguard business logic from inappropriate usage outside a transactional context. In any
case, if you decide to use detached entities as a substitute for DTOs, you should make sure
they're marked .
10.2.4. Deleting entities
The deleteItem method in the ItemManagerBean in listing 10.1 deletes an Item
from the database. An important detail to notice about the deleteItem method (repeated
next) is that the Item to be deleted was first attached to the EntityManager using the
merge method:
public void deleteItem(Item item) {
This is because the remove method can only delete currently attached entities and the
Item entity being passed to the deleteItem method isn't managed. If a detached entity
is passed to the remove method, it throws an IllegalArgumentException . Before
the deleteItem method returns, the Item record will be deleted from the database us-
ing a DELETE statement like this:
Just as with the persist and merge methods, the DELETE statement isn't necessarily
issued immediately but is guaranteed to be issued at some point. Meanwhile, the
EntityManager marks the entity as removed so that no further changes to it are syn-
chronized (as we noted in the previous section).
Search WWH ::

Custom Search