Java Reference
In-Depth Information
//...
}
That's it! By using the @Entity annotation, JPA now knows this is an entity you wish it
to manage during database interactions. As the code snippet demonstrates, all nonabstract
entities must have either a public or a protected no-argument constructor. The constructor
is used by JPA to create a new instance of the entity—you never manually create a new
instance yourself when getting data through JPA.
A powerful feature of JPA is that because entities are POJOs, they support a full range of
OO features like inheritance and polymorphism. You can have an entity extend to either an-
other entity or even to a nonentity class. For example, figure 9.4 demonstrates good design
to extend both the Seller and Bidder domain object classes from a common User
class (the User class may or may not be annotated with @Entity ). In the following list-
ing, you'll declare the parent User class as an entity.
Figure 9.4. Inheritance support with entities. Bidder and Seller entities extend the User entity class.
Listing 9.2. User entity
@Entity
public abstract class User {
// ...
String userId;
String username;
String email;
 
 
Search WWH ::




Custom Search