Database, JPA Queries, Cardinality Mappings Flashcards
What does a One-to-Many (1:N) cardinality mapping signify in database relationships?
In a One-to-Many (1:N) cardinality mapping, a single instance of one entity is associated with multiple instances of another entity. However, each instance of the latter entity is related to only one instance of the former entity.
What does a Many-to-One (N:1) cardinality mapping represent in database relationships?
In a Many-to-One (N:1) cardinality mapping, multiple instances of one entity are related to a single instance of another entity.
Describe the One-to-One (1:1) cardinality mapping in database relationships.
One-to-One (1:1) cardinality mapping represents a direct relationship where one instance of an entity is associated with exactly one instance of another entity. Both entities have a unique connection, often reflecting specific attributes or dependencies between them.
Explain Many-to-Many (N:N) cardinality mapping in database relationships.
Many-to-Many (N:N) cardinality mapping signifies a complex relationship where multiple instances of one entity can be linked to multiple instances of another entity. To manage this, a junction table or intermediary entity is introduced to facilitate the connections between these entities.
What defines the Zero or One-to-Many (0/1:N) cardinality mapping in database relationships?
Zero or One-to-Many (0/1:N) cardinality mapping represents instances where one entity is associated with multiple instances of another entity. However, the initial entity may have either zero or one relationship with the second entity. This indicates an optional connection.
How is a Many-to-Zero or One (N:0/1) cardinality mapping characterized in database relationships?
Many-to-Zero or One (N:0/1) cardinality mapping represents a scenario where multiple instances of an entity have an optional association with another entity. The second entity can be linked to zero or one instance of the initial entity.
What does JPA stand for?
JPA stands for Java Persistence API. It’s a Java specification for managing relational data in applications using object-relational mapping (ORM) techniques.
Explain JPQL in JPA.
JPQL (Java Persistence Query Language) is a query language used with JPA to perform database operations. It’s similar to SQL but operates on Java objects rather than database tables. JPQL allows querying entities based on their properties and relationships.
How do you execute JPQL queries in JPA?
In JPA, JPQL queries can be executed using the EntityManager interface’s createQuery() method. This method accepts JPQL queries as strings and returns a Query object that can be used to execute the queries.
What are the benefits of using JPA queries?
JPA queries provide a standardized way to interact with databases, making applications database-independent.
They allow developers to write queries using an object-oriented approach, abstracting the underlying database structure.
JPA queries support various query types, including SELECT, UPDATE, DELETE, and INSERT operations.
Differentiate between JPQL and SQL.
JPQL is object-oriented and operates on entities and their attributes, focusing on Java objects. SQL, on the other hand, is a database-oriented language that deals with tables, rows, and columns directly in the database.
What does the Spring Physical Strategy refer to in Spring Data JPA?
The Spring Physical Strategy in Spring Data JPA deals with how Java entities are mapped to the underlying database structure, including tables, columns, constraints, and relationships.
What are the common components of the Spring Physical Strategy in Spring Data JPA?
Table Creation Strategy: Defines how tables are created in the database based on entity definitions.
Column Mapping: Maps Java entity attributes to database columns.
Relationship Mapping: Establishes relationships between entities and maps them to the database structure.
How does Spring Data JPA handle table creation based on entity classes?
Spring Data JPA provides strategies like CREATE, CREATE_IF_NOT_EXIST, and VALIDATE to manage table creation during application startup or runtime.
What annotations are used in Spring Data JPA for mapping columns and relationships?
Column Mapping: @Column annotation for specifying column details.
Relationship Mapping: @OneToOne, @OneToMany, @ManyToOne, @ManyToMany for defining relationships and corresponding foreign keys.
What is “Spring Open in View”?
Spring Open in View is a pattern that allows Hibernate Sessions (database connections) to remain open for the entire duration of processing an HTTP request. It’s often used in conjunction with web frameworks to automatically open and close sessions during request handling.
How does Spring Open in View work in Spring Framework?
Spring Open in View leverages interceptors or filters to open a Hibernate Session at the beginning of a request and close it after the view has been rendered, ensuring the session remains open for the entire request cycle.
What are the advantages and disadvantages of Spring Open in View?
Advantages: Simplifies transaction management by allowing lazy-loading in the view layer, making it easier to work with ORM frameworks.
Disadvantages: Can cause performance issues due to longer session duration, leading to potential database connection leaks or inefficient resource usage.
Which configuration options are related to Spring Open in View?
OpenEntityManagerInViewFilter for JPA-based applications.
OpenSessionInViewFilter for Hibernate-based applications.
What are inheritance strategies in JPA?
In JPA, inheritance strategies define how entities with inheritance hierarchies are mapped to the relational database.
Name the different inheritance strategies in JPA.
There are three inheritance strategies in JPA: SINGLE_TABLE, TABLE_PER_CLASS, and JOINED.
Describe the SINGLE_TABLE inheritance strategy in JPA.
In the SINGLE_TABLE strategy, all entities in an inheritance hierarchy are mapped to a single database table, using discriminator columns to differentiate between entity types.
What does the TABLE_PER_CLASS inheritance strategy do in JPA?
The TABLE_PER_CLASS strategy maps each concrete entity class to its table, duplicating common fields across tables but avoiding null values.
Explain the JOINED inheritance strategy in JPA.
With the JOINED strategy, each entity class in the hierarchy corresponds to a separate table in the database, linked through foreign key relationships.