Flashcards
Spring Data JDBC & JPA
Cards in this deck (12)
- 1What is the difference between Spring JDBC and Spring Data JDBC?Spring JDBC, a sub-module under the Spring framework project, provides Spring abstractions over the plain JDBC DataSource that we can use with the Spring Framework.
Spring JDBC nicely hooks support for Transaction Management, Exception Handling, DB connection management, Connection pool configuration, avoiding boilerplate code using JdbcTemplate etc.
Spring Data JDBC, a sub-module under the Spring Data project, makes it easy to implement JDBC-based repositories.
Spring Data JDBC is an Object Relational Mapper (ORM) based on the Repository abstraction.
Spring Data JDBC nicely hooks support for CrudRepository, @Query, PagingAndSortingRepository, @Value in persistence constructors etc. - 2What do you mean by database dialect?A database dialect is a configuration setting for platform-independent software (JPA, Hibernate, etc.) which allows such software to translate its generic SQL statements into vendor-specific DDL, DML.
- 3Why does Spring Data JDBC need database dialect?One of the core features of Spring Data JDBC is to generate automatic queries using CrudRepository for vendor-specific databases.
To generate the queries Spring Data JDBC internally uses database dialects. - 4Spring Data JDBC includes direct support for which database dialects?In terms of databases, Spring Data JDBC requires a dialect to abstract common SQL functionality over vendor-specific flavors.
Spring Data JDBC includes direct support for the following databases:
DB2
H2
HSQLDB
MariaDB
Microsoft SQL Server
MySQL
Oracle
Postgres - 5Why do we use PagingAndSortingRepository in Spring?Spring Data JDBC sub-module under the Spring Data project provides PagingAndSortingRepository which is an extension of CrudRepository to provide additional methods to retrieve entities using the pagination and sorting abstraction.
- 6The CrudRepository interface does not have an update method then how do we update the record in the database?CrudRepository has only save but it acts as an update as well.
When we save an entity with an empty id it will do a save.
When you do save on the entity wan its existing id it will do an update.
This means after we used findById and changed something in returned object, we can call save on that object and it will do an update because after findById you get an object with a populated id that exists in our database.
save in CrudRepository can accept a single entity or Iterable of our entity type. - 7Is it mandatory to call the save method to update an object inside a transactional method for CrudRepository?Calling the save method to update an object inside a transactional method is not mandatory.
When we use the findById method to retrieve an entity within a transactional method, the returned entity is managed by the persistence provider.
Hence, any change to that entity will be automatically persisted in the database, regardless of whether we are invoking the save method inside a transactional method. - 8What is the use of @Query annotation in Spring Data?Spring Data provides multiple ways to define a query that we can execute.
One of these popular methods is using @Query annotation.
We can annotate the Spring Data repository method with the @Query annotation where its value attribute contains the JPQL or SQL to be executed.
It's a convenient approach to place a query definition just above the method inside the repository rather than inside our domain model as named queries. - 9Explain the query builder mechanism in the Spring Data repository?The query builder mechanism built into the Spring Data repository infrastructure is useful for building constraining queries over entities of the repository.
The query method names are divided into subject and predicate.
The first part (find…By, exists…By) defines the subject of the query, and the second part forms the predicate.
Few Queries subject keywords - find…By, get…By, count…By, …Distinct…, delete…By,Top…
Few Queries predicate keywords - Containing, After, Before, Between, EndingWith, StartsWith, LessThan, GreaterThan
Few Query predicate modifier keywords - IgnoreCase, OrderBy… - 10How you will implement the SQL LIKE query in Spring Data using method names? Give an example.Let’s consider the Login entity comprising the userId attribute.
There can be four different variations of LIKE query formed using method names as follows:
Exact Match SELECT ... LIKE userId
List findByUserIdLike(String userId);
Starting With SELECT ... LIKE userId%
List findByUserIdStartingWith(String userId);
Ending With SELECT ... LIKE %userId
List findByUserIdEndingWith(String userId);
In Between SELECT ... LIKE %userId%
List findByUserIdContaining(String userId); - 11What is Spring Data JPA?Spring Data JPA, a sub-module under the Spring Data project, makes it easy to implement JPA based repositories.
Spring Data JPA provides repository support for the Java Persistence API (JPA).
It eases the development of applications that need to access JPA data sources.
Spring Data JPA nicely hooks support for JpaRepository, Hibernate, OpenJPA, EclipseLink, Querydsl, @Modifying - 12What is the use of @Modifying in Spring Data JPA?Adding @Modifying annotation indicates the query is not for a SELECT query.
@Modifying annotation lets you execute DML inserts, updates, and deletes)and DDL using JPA @Query annotations.
@Modifying annotation is only relevant in combination with the @Query annotation, derived query methods or custom methods do not require this Annotation.
Information
- Description
- Spring Data JDBC & JPA INterview questions
- Created by
- Jerry Reeves Jr
- Views
- 3
- Rating