Lecture 8 - Session Beans and Jakarta Persistence (Intro to EJB) Flashcards
What is a Jakarta Enterprise Bean (EJB) and what services does it provide?
A bean that can be used for business logic:
- Concurrency
- Transaction management
- Persistence
- Security etc.
What are the 3 types of EJBs?
Session beans (business behaviour, 1 client at a time)
@Stateless, @Stateful, @Singleton
Message-driven beans (async messages, methods are normally synchronous)
@MessageDriven
Entity beans (persist in DB, multiple clients at a time)
@Entity
Why use EJB over CDI bean?
Transactions + security controls for methods
Automatic concurrency
Instance pooling -> stateless beans can be shared
Passivation -> Stateful beans can be temp stored in memory when not needed
Difference between stateless and stateful session beans?
Stateless beans are short-lived and not persistent (independent method calls).
Stateful session beans hold client information between method calls.
NOTE: singleton beans are useful for application-wide shared resources. Must be thread-safe/concurrent
What are entity beans?
EJB bean that has persistent state
can be used by multiple clients at a time
Replaced by jakarta persistence (don’t depend on EJB container, less boilerplate)
What are EJB session beans?
Interact with single client at a time.
Stateful session beans should be serializable
Relatively short lived (represents chunk of application logic)
What is Jakarta Persistence?
Java classes with annotations
EntityManager manages beans that are attached:
- create
- find
- update
- delete
changes to attached beans tracked and saved to DB
detached beans are regular java objects that can be sent across the network/application as data transfer objects
What is Object-to-relational persistence?
mapping object fields to a relational database using EJB 3 standard - Object-Relational Mapping (ORM)
Fields annotated with @Id represent PK
(Normally have @Column annotation as well)
Fields annotated with @Column(name=”someName”) maps field to a column in DB
An entity bean is defined for each table (T or F)
T
(Multiple rows are created and mapped to multiple instances)
(CRUD operations from Entity manager manipulate these instances)
Annotations for Jakarta persistence (Entity bean, ID - primary key, Table, Column )
@jakarta.persistence.*
What is persistence.xml?
Lives in META-INF
Specifies name of persistence unit (configure how application connects to DB)
Specifies reference to data source (inside <persistence-unit>)
May specify vendor-specific options</persistence-unit>