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.
Which inheritance strategy provides normalized tables in JPA?
The JOINED strategy provides normalized tables as it separates entity attributes into individual tables based on the entity hierarchy.
What factors should be considered when choosing an inheritance strategy in JPA?
Considerations include performance, ease of querying, maintenance, and the nature of the inheritance hierarchy when choosing an appropriate strategy.
What is a lazy fetch type in JPA?
Lazy fetching delays the loading of associated data until it’s explicitly requested, enhancing performance by loading data only when needed.
Explain the purpose of the @ManyToOne annotation in JPA.
@ManyToOne establishes a many-to-one relationship between entities, indicating that multiple instances of one entity can be associated with a single instance of another entity.
What does the @Entity annotation signify in JPA?
The @Entity annotation marks a Java class as a persistent entity, indicating that it is mapped to a database table.
How do you denote a primary key field in JPA?
In JPA, you mark a field as a primary key using the @Id annotation.
What is the purpose of the @DiscriminatorColumn annotation in JPA?
@DiscriminatorColumn in JPA specifies the database column that holds the discriminator value, aiding in distinguishing between different subclasses when using inheritance strategies like SINGLE_TABLE.
What does “Spring Boot ambiguity service” refer to, particularly concerning dependency injection?
In Spring Boot, “ambiguity service” denotes a scenario where multiple beans match a specific dependency or qualifier, causing ambiguity in selecting the correct bean during dependency injection, often requiring disambiguation strategies like using @Qualifier or refining bean definitions.
What are the multiple options to consume large query results?
Paging, Offset-based scrolling, Keyset-based scrolling.
How can you use named parameters in Spring Data JPA?
By using the @Param annotation to give a method parameter a concrete name and bind the name in the query.
What does the #{#entityName} expression do in Spring Data JPA?
It dynamically inserts the entity name of the domain type associated with the repository.
How can you activate Hibernate comments in Spring Boot?
By setting spring.jpa.properties.hibernate.use_sql_comments=true in the application.properties file.
What does the @EntityGraph annotation do in Spring Data JPA?
It configures the fetch plan of a query by referencing a named entity graph on an entity.
What is the purpose of scrolling in query results?
Scrolling allows a more fine-grained approach to iterate through larger result sets in chunks.
How does Keyset-Filtering differ from Offset-based scrolling?
Keyset-Filtering leverages built-in capabilities of the database to reduce computation and I/O requirements by using a stable sorting order and passing keys into the query to resume scrolling.
What is the use of the @Modifying annotation in Spring Data JPA?
It triggers a query annotated to the method as an updating query instead of a selecting one.
How can you add custom comments into JPA operations?
By applying the @Meta annotation to repository operations, allowing custom comments to be inserted into queries.
What does the Window<T> in Spring Data JPA scrolling represent?</T>
It represents a chunk of query results that can be iterated through until the entire query result is consumed.
What are the three main scrolling methods used in Spring Data JPA for handling large query results?
The three scrolling methods in Spring Data JPA for handling large query results are Paging, Offset-based scrolling, and Keyset-based scrolling.