Week 10 Flashcards
What is Spring Data?
Spring Data is a part of the Spring Framework that simplifies data access and makes working with databases easier. It provides a consistent model to interact with a wide range of data storage technologies, including relational databases (e.g., MySQL, PostgreSQL) and NoSQL databases (e.g., MongoDB, Cassandra).
What are the main goals of Spring Data?
Simplify data access and persistence.
Provide a consistent programming model for different types of databases.
Minimize boilerplate code in data access layers.
Offer integration with various data stores (relational and NoSQL).
How does Spring Data simplify data access in Java applications?
Spring Data abstracts common persistence operations, like saving, deleting, and querying, through repository interfaces, eliminating the need to write complex SQL queries or boilerplate code. It generates implementation code based on method names and annotations.
What is the role of repositories in Spring Data?
Repositories provide an interface-based abstraction for data access operations. They handle CRUD (Create, Read, Update, Delete) operations and allow querying by automatically generating SQL based on method names.
Can Spring Data work with databases other than relational ones? Name examples.
Yes, Spring Data works with both relational and NoSQL databases. Examples include:
Relational: MySQL, PostgreSQL
NoSQL: MongoDB, Cassandra, Redis, Couchbase
What is JPA, and how does it differ from Hibernate?
JPA (Java Persistence API) is a specification that defines how Java objects should be mapped to database tables. Hibernate is an implementation of JPA, providing additional features beyond the specification. While JPA defines the API, Hibernate offers concrete tools for working with persistence.
How is Hibernate related to JPA?
Hibernate is an implementation of JPA. It adheres to the JPA standard while also offering extended features like custom caching mechanisms and native SQL support.
What role does Spring Data JPA play in the persistence layer?
Spring Data JPA provides the glue between Spring’s repository abstraction and the JPA implementation. It automates many common data access tasks and provides features like query methods and pagination with minimal code.
What benefits does Spring Data JPA provide over using plain JPA?
Reduces boilerplate code with auto-generated repository methods.
Simplifies query creation using method names.
Adds additional features like pagination and sorting.
Provides default implementation of common data access operations.
Can Spring Data JPA work with ORM frameworks other than Hibernate?
Yes, Spring Data JPA can work with other JPA-compliant ORM frameworks, like EclipseLink and OpenJPA, though Hibernate is the most commonly used implementation.
What is CrudRepository in Spring Data?
CrudRepository is a Spring Data interface that provides methods for basic CRUD operations like save(), findById(), findAll(), deleteById(), etc.
What is JpaRepository in Spring Data?
JpaRepository is an interface in Spring Data JPA that extends CrudRepository and provides additional JPA-specific methods, such as batch operations and flushing changes to the database.
How does JpaRepository extend CrudRepository?
JpaRepository extends CrudRepository by adding JPA-specific functionality, such as support for batch operations, pagination, sorting, and finer control over persistence context flushing.
Which repository provides pagination and sorting methods by default?
JpaRepository provides pagination and sorting methods by default.
When would you choose to use CrudRepository over JpaRepository?
You would use CrudRepository when you only need basic CRUD operations and do not require advanced JPA-specific features like pagination and sorting.
What are property expressions in Spring Data?
Property expressions are used to define query methods in Spring Data repositories. The method names contain field names and operations that Spring Data translates into SQL queries.
How does Spring Data generate queries from method names?
Spring Data parses the method names of repository interfaces and converts them into queries by identifying property names and keywords like findBy, countBy, deleteBy, and logical operators (e.g., And, Or).
What are some examples of property expressions used in repository method names?
findByLastName(String lastName)
findByAgeGreaterThan(Integer age)
countByStatus(String status)
How can you use the And and Or keywords in property expressions?
You can chain properties using And and Or keywords to create complex queries. For example:
findByFirstNameAndLastName(String firstName, String lastName)
findByAgeGreaterThanOrAgeLessThan(Integer minAge, Integer maxAge)
Can property expressions handle relationships between entities (e.g., nested properties)?
Yes, Spring Data can handle nested properties in entity relationships. For example, findByAddressCity(String city) can be used if Address is an entity linked to the main entity with a city field.
What is the purpose of the @Entity annotation in JPA?
The @Entity annotation is used to mark a class as a JPA entity, indicating that it should be mapped to a database table.
What does the @Id annotation signify in JPA?
The @Id annotation marks a field as the primary key of the entity.
What is the role of the @GeneratedValue annotation?
The @GeneratedValue annotation is used to indicate that the value of the primary key should be automatically generated by the database.
How does the @Table annotation help in mapping entities?
The @Table annotation is used to specify the table name in the database to which the entity is mapped. It can also define schema and other table-level constraints.
What is the difference between @OneToMany and @ManyToOne annotations?
@OneToMany defines a one-to-many relationship, where one entity is related to many entities of another type.
@ManyToOne defines a many-to-one relationship, where many entities are related to one entity.
What is the @Transactional annotation used for in Spring?
The @Transactional annotation is used to mark a method or class as transactional, meaning that all operations within the transaction are treated as a single unit and will either complete successfully or be rolled back in case of failure.
What does the propagation attribute in @Transactional control?
The propagation attribute controls how transactions are propagated when a method is called within an existing transaction. For example, Propagation.REQUIRED will reuse the existing transaction if present, while Propagation.REQUIRES_NEW will start a new transaction.
What are the different isolation levels supported by @Transactional?
The supported isolation levels include:
READ_UNCOMMITTED
READ_COMMITTED
REPEATABLE_READ
SERIALIZABLE