Getting Started Flashcards
Entity
An entity is a Java representation of the database table that has characteristics like persistability, identity, transactionality, and granularity.
What are the characteristics of an
object that has been turned into an entity?
- Persistability
- Identity
- Transactionality
- Granularity
Persistability
This generally just means that they can be made persistent. An entity is persistable because it can be saved in a persistent store.
Identity
Persistent identity, or an identifier, is the key that uniquely identifies an entity instance and distinguishes it from all the other instances of the same entity type.
The entity identifier, then, is equivalent to the primary key in the database table that stores the entity state.
Transactionality
Changes made to the database either succeed or fail atomically, so the
persistent view of an entity should indeed be transactional.
Granularity
Entities are not primitives, primitive wrappers, or built-in objects with single-dimensional state. Ideally, entities should be designed and defined as fairly
lightweight objects of a size comparable to that of the average Java object
Entity Metadata
- every JPA entity has some associated metadata that describes it.
- This metadata may exist as part of the saved class file or it may be stored external to the class, but it is not persisted in the database.
- It enables the persistence layer to recognize, interpret, and properly manage the entity from the time it is loaded through to its runtime invocation.
- Entity metadata may be specified in two ways: annotations or XML
Why use annotations for JPA?
They are convenient and it is not necessary to escape to an additional file and a special language (XML) just to specify the metadata
Configuration by Exception
the persistence engine defines defaults that apply to the majority of applications and that users need to supply values only when they want to override the default value.
Downsides of Configuration by Exception
When defaults are embedded into the API and do not have to be specified, then they are not visible or obvious to users. This can make it possible for users to be unaware of the complexity of developing persistence applications, making it harder to debug or to change the behavior when it becomes necessary
Creating an Entity
- Must be annotated with the javax.persistence.Entity annotation
- Must have a public or protected no-argument constructor (it may have other constructors)
- Must not be declared final (therefore, no methods or persistent instance variables can be declared final)
- Must implement the Serializable interface (in case the entity instance is passed by value as a detached object via a session bean’s remote business interface)
- May extend both entity and non-entity classes, and non-entity classes may extend entity classes
- Persistent instance variables must be declared private, protected, or package-private (they can be accessed directly only by the entity class methods)
Persistence Context
- The set of managed entity instances within an entity manager at any given time is called its persistence context.
- Only one Java instance with the same persistent identity may exist in a persistence context at any time.
For example, if an Employee with a persistent identity (or ID) of 158 exists in the persistence context, then no other Employee object with its ID set to 158 may exist within that same persistence context.
EntityManager
- Entity managers are configured to be able to persist or manage specific types of objects, read and write to a given database, and be implemented by a particular persistence provider (or provider for short).
- It is the provider that supplies the backing implementation engine for the entire Java Persistence API, from the EntityManager through to implementation of the query classes and SQL generation.
- All entity managers come from factories of type javax.persistence.EntityManagerFactory.
Persistence unit
- The configuration for an entity manager is templated from the entity manager factory that created it, but it is defined separately as a persistence unit.
- There is, therefore, a one-to-one correspondence between a persistence unit and its concrete EntityManagerFactory instance.
• The configuration that describes the persistence unit is defined in an XML file called
persistence.xml.
• A single persistence.xml file can contain one or more named persistence unit configurations, but each persistence unit is separate and distinct from the others, and they can be logically thought of as being in separate persistence.xml files.
Obtain an EntityManager
The static createEntityManagerFactory() method in the Persistence class returns the EntityManagerFactory for the specified persistence unit name.
EntityManagerFactory emf =
Persistence.createEntityManagerFactory(“EmployeeService”);
name argument identifies the given persistence unit configuration that determines such things as the connection parameters that entity managers generated from this factory will use when connecting to the database.
Now that we have a factory, we can easily obtain an entity manager from it.
EntityManager em = emf.createEntityManager();