Spring Data JDBC and JPA
Learn about a few interview questions regarding Spring data JDBC and JPA.
We'll cover the following...
- How does Spring provide DAO support?
- Describe Spring DAO support classes.
- What annotation is used to mark DAO in Spring?
- What is Spring data JDBC?
- What tasks are performed by Spring JDBC?
- What is the difference between JDBC and Spring JDBC?
- Name the classes in Spring JDBC API?
- What are the advantages of JdbcTemplate in Spring?
- Why is NamedParameterJdbcTemplate class used?
- How are records from a database fetched when using JdbcTemplate?
- What is the difference between a RowMapper and ResultSetExtractor?
- Explain Spring’s exception handling support with DataAccessException.
- What is SQLExceptionTranslator?
- What are JdbcTemplate callback interfaces?
- What is Spring Data?
- What is JPA?
- What is the difference between JPA and Spring Data JPA?
- What is the difference between CrudRepository and JpaRepository?
- Which No SQL databases does Spring support?
- Which ORMs are supported by Spring?
- What is Hibernate?
- What are the different types of transaction management supported by Spring?
- Which transaction management type is preferred in Spring?
- What are some benefits of using Spring Transactions?
- What happens when the @Transactional annotation is used on a method?
- What is the difference between JdbcTemplate and JPA?
How does Spring provide DAO support?
Spring DAO support makes it easy to work with different data access technologies in a consistent way which makes it easy to switch between technologies like JDBC, Hibernate, or JDO. Main features of Spring DAO support are:
- Exception handling: Technology specific exception like
SQLException
is translated to Spring’sDataAccessException
. Checked exceptions in Hibernate, JPA, and JDO can be converted to runtime exceptions. - Abstract DAO support classes: Spring provides abstract classes for different data access technologies which provide methods for data source and other configuration settings. These include the
JdbcDaoSupport
,HibernateDaoSupport
,JdoDaoSupport
, andJpaDaoSupport
classes.
Describe Spring DAO support classes.
Spring provides technology specific abstract classes for DAO support. These classes have methods for providing data source and other configuration settings specific to the data access technology. Abstract DAO support classes are:
JdbcDaoSupport
class requiresDataSource
and returnsJdbcTemplate
instance.HibernateDaoSupport
class requiresSessionFactory
and returnsHibernateTemplate
instance.JdoDaoSupport
class requiresPersistenceManagerFactory
and returnsJdoTemplate
instance.JpaDaoSupport
class requiresEntityManagerFactory
and returnsJpaTemplate
instance.
What annotation is used to mark DAO in Spring?
The @Repository
annotation is used for DAO classes. This annotation provides the Spring exception translation facility which converts a technology specific expectation to a DataAccessException
.
What is Spring data JDBC?
Spring Data JDBC framework is part of Spring Data. It provides support for JDBC repositories. Spring Data JDBC is a simple but limited ORM. It does not implement features like caching, lazy loading, and write-behind. Unlike Spring Data JPA, it does not provide schema generation and the developer must explicitly create the schema.
Spring JDBC uses methods like ...