1.

Explain the Database/Repository annotations used in Spring?

Answer»

Below are the significant annotations used for connecting a database with Spring application:

  • @Repository: This annotation is a specialization of the @Component annotation i.e. automatic component scanning enabled so that Spring imports the beans into a CONTAINER and inject to dependencies. It is typically used on the DAO (Data Access Object) classes. It also catches persistence related exceptions and rethrows them as Spring’s unified unchecked exception.
  • @Entity: This annotation is used to indicate that the class is a JPA entity and their state is managed by the underlying Persistence Context. In case @Table annotation is not added, it is assumed that this entity will be mapped to a table, with the same name as the class.
  • @Table: This is used to map the entity class to a given table. It allows having two different names for the Java class and the Database Table.
  • @Id: This annotation is added on a field that captures the object ID and is an IDEAL CANDIDATE for the primary key
  • @Column: This is used to explicitly define the column name, default is field/property name.
  • @Transactional: It is convenient, readable and recommended an approach to handle TRANSACTIONS in Spring. This annotation defines the scope of a single database transaction, that happens inside the scope of a persistence context. The persistence context is in JPA the EntityManager, with default implemented of Hibernate Session.


Discussion

No Comment Found