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
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
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
Persistence Context
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
Persistence unit
• 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();
Persisting an entity
Persisting an entity is the operation of taking a transient entity, or one that does not yet have any persistent representation in the database, and storing its state so that it can be retrieved later.
If the entity manager encounters a problem doing this, it will throw an unchecked PersistenceException.
Finding an entity
Employee emp = em.find(Employee.class, 158);
We are passing in the class of the entity that is being sought and the ID or primary key that identifies the particular entity.
Passing in the class as a parameter also allows the find method to be parameterized and to return an object of same type that was passed in, saving the caller an extra cast.
In the event that the object was not found, then the find() call simply returns
null.
Deleting an entity
In order to remove an entity, the entity itself must be managed, meaning that it is present in the persistence context.
Employee emp = em.find(Employee.class, id);
if (emp != null) {
em.remove(emp);
}
It will return successfully whether the employee exists or not.
Updating an entity
Employee emp = em.find(Employee.class, 158);
emp.setSalary(emp.getSalary() + 1000);
In this case, we are not calling into the entity manager to modify the object, but directly calling the object itself. We indicate success by returning the updated employee.
Find doesn’t need transaction
The find() call is not a mutating operation, so it may be called any time, with or without a transaction.
Transactions
the key is the environment in which the code is being executed. When executing in Java SE, we either need to begin and to commit the transaction in the operational methods, or we need to begin and to commit the transaction before and after calling an operational method.
em.getTransaction().begin();
createEmployee(158, “John Doe”, 45000);
em.getTransaction().commit();
Queries
getResultStream()
that JPA version 2.2 introduced for the JPA interfaces Query and TypedQuery a
new method called getResultStream(), which will return a Java 8 stream of the query
result. This method, by default, will delegate to getResultList().stream(). This
method provides a better way to move through the query result set.
Static queries
A static query is typically defined in either annotation or XML metadata, and it must include the query criteria as well as a user-assigned name. This kind of query is also called a named query, and it is later looked up by its name at the time it is executed.