concept entity in category ejb

appears as: entities, n entity, entities, entity, entity, An entity
EJB 3 in Action, Second Edition

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

Figure 1.3. Domain-driven design typically has four or more layers. The presentation layer is responsible for the user interface and for interaction with the application/service layer. The application/service layer is typically very light and only allows communication between the presentation layer and the domain. The domain layer is the complex expression of your application data model consisting of entities, value objects, aggregates, factories, and repositories. The infrastructure layer gets to the database and other similar technology.

Entities are the Java objects that are persisted into the database. While session beans are the “verbs” of a system, entities are the “nouns.” Common examples include an Employee entity, a User entity, and an Item entity. Entities are the OO representations of the application data stored in the database. Entities survive container crashes and shutdown. The ORM metadata specifies how the object is mapped to the database. You’ll see an example of this in the next chapter. JPA entities support a full range of relational and OO capabilities, including relationships between entities, inheritance, and polymorphism.

Chapter 9 introduced the basics of JPA entities. You saw how to implement the domain objects with JPA, define relationships between objects, and handle inheritance. In this chapter we’re going to delve into EntityManager and show how to manage entities. We’ll discuss what an EntityManager is and how to inject them into your EJB classes. You’ll learn about the lifecycle of entities and how to use EntityManager to persist, find, merge, and remove entities—the EntityManager equivalent of the basic database CRUD (create, read, update, delete) operations. You’ll also learn about the different scopes entities go through during their lifecycle.

A detached entity is an entity that’s no longer managed by the EntityManager and there’s no guarantee that the state of the entity is in synch with the database. Detachment and merge operations become handy when you want to pass an entity across application tiers. For example, you can detach an entity and pass it to the web tier, then update it and send it back to the EJB tier, where you can merge the detached entity to the persistence context.

The usual way entities become detached is a little subtler. Essentially, an attached entity becomes detached as soon as it goes out of the EntityManager context’s scope. Think of this as the expiration of the invisible link between an entity and the EntityManager at the end of a logical unit of work or a session. An EntityManager session could be limited to a single method call or span an arbitrary length of time. (Reminds you of session beans, doesn’t it? As you’ll soon see, this isn’t entirely an accident.) For an EntityManager whose session is limited to a method call, all entities attached to it become detached as soon as a method returns, even if the entity objects are used outside the method. If this isn’t crystal clear right now, it will be once we talk about the EntityManager’s persistence context in the next section.

Entity instances also become detached through cloning or serialization. This is because the EntityManager quite literally keeps track of entities through Java object references. Because cloned or serialized instances don’t have the same object references as the original managed entity, the EntityManager has no way of knowing they exist. This scenario occurs most often in situations where entities are sent across the network for session bean remote method calls.

In addition, if you call the clear method of EntityManager, it forces all entities in the persistence context to be detached. Calling the EntityManager’s remove method will also detach an entity. This makes perfect sense because this method removes the data associated with the entity from the database. As far as the EntityManager is concerned, the entity no longer exists, so there’s no need to continue managing it. For your Bid entity, this would be an apt demise:

manager.remove(bid);

A good way to remember the entity lifecycle is through a convenient analogy. Think of an entity as an aircraft and the EntityManager as the air traffic controller. When an entity is outside the range of air traffic control, it’s “detached” or “new.” But when it does come into range (managed), the traffic controller manages the aircraft’s movement (state synchronized with database). Eventually, a grounded aircraft is guided into takeoff and goes out of airport range again (detached), at which point the pilot is free to follow their own flight plan (modifying a detached entity without the state being managed).