Getting Started Flashcards

1
Q

Entity

A

An entity is a Java representation of the database table that has characteristics like persistability, identity, transactionality, and granularity.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the characteristics of an

object that has been turned into an entity?

A
  • Persistability
  • Identity
  • Transactionality
  • Granularity
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Persistability

A

This generally just means that they can be made persistent. An entity is persistable because it can be saved in a persistent store.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Identity

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Transactionality

A

Changes made to the database either succeed or fail atomically, so the
persistent view of an entity should indeed be transactional.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Granularity

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Entity Metadata

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Why use annotations for JPA?

A

They are convenient and it is not necessary to escape to an additional file and a special language (XML) just to specify the metadata

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Configuration by Exception

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Downsides of Configuration by Exception

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Creating an Entity

A
  • 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)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Persistence Context

A
  • 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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

EntityManager

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Persistence unit

A
  • 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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Obtain an EntityManager

A

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();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Persisting an entity

A

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.

17
Q

Finding an entity

A

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.

18
Q

Deleting an entity

A

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.

19
Q

Updating an entity

A

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.

20
Q

Find doesn’t need transaction

A

The find() call is not a mutating operation, so it may be called any time, with or without a transaction.

21
Q

Transactions

A

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();

22
Q

Queries

A
  • we are querying over entities and using a language called Java Persistence Query Language (JPQL).
  • A query is implemented in code as a Query or TypedQuery object.
23
Q

getResultStream()

A

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.

24
Q

Static queries

A

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.

25
Q

Query types

A

A query can be defined either statically or dynamically.

26
Q

Dynamic queries

A

A dynamic query can be issued at runtime by supplying the JP QL query criteria
or a criteria object. They may be a little more expensive to execute because the
persistence provider cannot do any query preparation beforehand

27
Q

Sample of EntityManagerFactory and EntityManager

A

EntityManagerFactory emf =
Persistence.createEntityManagerFactory(“EmployeeService”);
EntityManager em = emf.createEntityManager();
EmployeeService service = new EmployeeService(em);
.
.
.
// close the EM and EMF when done
em.close();
emf.close();

28
Q

persistence unit transaction type

A

The transaction-type attribute indicates that the persistence unit uses resource-level EntityTransaction instead of JTA transactions.

29
Q

persistence unit classes

A

examples.model.Employee

The class element lists the entity that is part of the persistence unit.

the container will automatically scan for entity classes annotated with @Entity as part of the deployment process, but they are needed for portable execution when running in Java SE.

30
Q

Persistence Archive

A

The persistence artifacts are packaged in what we will loosely call a persistence archive.

This is really just a JAR-formatted file that contains the persistence.xml file in the META-INF directory and normally the entity class files.