Week 4 - Entity Manager Flashcards
What is ORM?
Object-to-relational mapping, Jakarta persistence maps objects to a relational database.
What are entities?
POJOs that may be managed.
What is an entity manager?
The central service for persistence actions.
What is the difference between a managed and unmanaged entity bean?
A managed entity bean is synchronized with the database.
An unmanaged entity bean is independent from the database.
What are the 2 types of persistence context?
Transaction-scoped and extended.
What is transaction-scoped persistence context?
Rely on container to manage transactions.
Open for single transactions
(default for session bean methods)
@PersistenceContext
What is extended persistence context?
Requires SFSB
Context stays open for multiple transactions which reduces constant merges (extended).
@PersistenceContext(type=EXTENDED)
When is an entity detached/attached in a transaction-scoped persistence context?
Transaction-Scoped Persistence Context (Default)
Entity automatically detaches when the method returns because the persistence context is closed.
When a bean is detached you can use it as a POJO or a DTO (T or F)
T
How are persistence units packaged and why?
The persistence unit is included in an EJB module, which is then bundled into an EAR.
This allows for centralized data access logic and can be shared by multiple web modules.
It is useful for modular applications where persistence logic is separated from the web layer.
What is a persistence unit?
A persistence unit is a set of entity classes managed by JPA and mapped to a single database. It is defined in persistence.xml, which must be placed in the META-INF directory of the JAR or WAR file that contains entity classes.
What attribute is required for each persistence unit?
name
What entity manager method writes entity fields into the database?
entityManager.persist();
What entity manager method will retrieve an entity from the database using its primary key?
entityManager.find();
EJB uses SQL as its query language (T or F)
F - it uses and SQL-like query language based on entities.
Query query = entityManager.createQuery(“from “ +
“Employee e where id=2”);
Employee emp = (Employee)query.getSingleResult();
What does entityManager.flush() do?
Syncs entity beans that are managed.
How do you manually update a detached entity?
EntityManager.merge(theEntity)
EntityManager.delete() will delete an entity from the DB (T or F)
F - EntityManager.remove(entityObject)
What Entity Manager method can be used to tell if an entity is managed or unmanaged?
contains() returns true if its argument is managed
by the persistence context
What entity manager method detaches all managed instances from the persistence context?
clear()