Podcast
Questions and Answers
What is the purpose of the @NamedQuery annotation in Figure 9.5?
What is the purpose of the @NamedQuery annotation in Figure 9.5?
What is the role of the EntityManager in Figure 9.6?
What is the role of the EntityManager in Figure 9.6?
What is the purpose of the persistence unit in Figure 9.6?
What is the purpose of the persistence unit in Figure 9.6?
What is the difference between the 'pre' and 'post' events in the entity life cycle?
What is the difference between the 'pre' and 'post' events in the entity life cycle?
Signup and view all the answers
What is the purpose of the JDBC driver in the EntityManager?
What is the purpose of the JDBC driver in the EntityManager?
Signup and view all the answers
What is the role of the persistence.xml file in Figure 9.6?
What is the role of the persistence.xml file in Figure 9.6?
Signup and view all the answers
What are the four categories of operations made to entities, as mentioned in the entity life cycle?
What are the four categories of operations made to entities, as mentioned in the entity life cycle?
Signup and view all the answers
What is the purpose of the named query in Figure 9.5?
What is the purpose of the named query in Figure 9.5?
Signup and view all the answers
What is the difference between the EntityManager and the JDBC driver?
What is the difference between the EntityManager and the JDBC driver?
Signup and view all the answers
What is the purpose of the entity life cycle?
What is the purpose of the entity life cycle?
Signup and view all the answers
Study Notes
Querying Entities
- JPA allows mapping entities to databases and querying them using different criteria.
- JPA's power lies in offering an object-oriented way to query entities and their relationships without using underlying database foreign keys or columns.
- The central piece of the API is the javax.persistence.EntityManager, which manages entities, reads from and writes to a database, and allows simple CRUD operations and complex queries using JPQL.
Obtaining an EntityManager
- An EntityManager can be obtained using an EntityManagerFactory, which is created using Persistence.createEntityManagerFactory("chapter04PU").
- The EntityManager interface can be used to manipulate entities, hiding JDBC calls to the database and INSERT or SELECT SQL statements.
Querying Entities using JPQL
- JPQL queries manipulate objects and attributes, not tables and columns.
- JPQL statements can be executed with dynamic queries (created dynamically at runtime) or static queries (defined statically at compile time).
- Static queries, also known as named queries, are defined using annotations (@NamedQuery) or XML metadata.
Entity Life Cycle and Callbacks
- Entities may include Bean Validation constraints and be automatically validated through JPA.
- JPA runtime will throw a ConstraintViolation exception if the entity does not meet the validation constraints.
JPA Specification Overview
- JPA is an abstraction above JDBC that makes it possible to be independent of SQL.
- The main components of JPA are:
- ORM, which maps objects to data stored in a relational database.
- An entity manager API to perform database-related operations.
- JPQL, which allows retrieving data with an object-oriented query language.
- Transactions and locking mechanisms provided by the Java Transaction API (JTA).
- Callbacks and listeners to hook business logic into the life cycle of a persistent object.
What's New in JPA 2.2?
- Schema generation
- Converters
- CDI support
- Support for stored procedures
- Bulk update and delete criteria queries
- Downcasting
- Stream query results
Persistence Unit
- The persistence unit indicates to the entity manager the type of database to use and the connection parameters.
- The persistence unit is defined in the persistence.xml file, which must be accessible in the class path.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Description
This quiz is about using Java Entity Manager to create and persist entities, as well as defining named queries. It involves understanding Object Relational Mapping and Java classes.