What is JPA?
- JPA Is also called as Java persistence API
- It is a standard form oracle to map object to database relations.
- Provides specification and API for JPA
- Specification is for the JPA provider and vendors like hibernate, Eclipse link, top link or open JPA.
- API is for developers.
- Which create an entity and file persist.xml and DAO.
- Session factory is replaced with Entity Manager Factory
- Session becomes Entity Manager.
- We can perform operation operations like persist, merge, find.
- It also gives us call back annotations like
- PerPersist
- PostPersist
- PreUpdate
What are the various, advantages and disadvantages of hibernate?
- Advantages
- Hibernate is database, independent.
- Hibernate application developed for one database, for example, My sql can be configured for oracle database.
- It provides a layered architecture.
- Hibernate is an implementation of JPA only.
- Mapping of domain object to relational databases.
- Caching Framework.
- Disadvantages
- We have to give a separate configuration file.
- Hard to debug
- Lots of API to learn.
- Slower than JDBC.
- Not suitable against batch processing.
What is association in hibernate?
- Association is a relationship between two database Tables as entries in our Java classes based on their attributes.
- It is of four types
- One to one
- One to many
- Many to one
- Many to many
- We use @join column for column reference in main entity.
- We use @oneToOne(mappedBy=“”) in our referenced entity.
How can we connect more than one databases Or use more than one types of database pools in a spring boot project?
- Define different data source object beans With respective qualifiers.
- Define different JDBC template beans with respective qualifiers.
- Use the implementation respective Datasource along with its Jdbc template to fetch data.
How do we implement Pagination in JPA?
- Client sends a page size, page number and sorting parameters.
- We find pageable object and find all method.
- Pagerequest.of(int page, int page size)
- We must use JPA repository.
- In JPA repository Pass this pageable object in findAll method.
What is the difference between JPA repository and CRUD repository?
JpaRepository extends PagingAndSortingRepository that extends CrudRepository.
CrudRepository mainly provides CRUD operations.
PagingAndSortingRepository provide methods to perform pagination and sorting of records.
JpaRepository provides JPA related methods such as flushing the persistence context and deleting of records in batch.
Due to their inheritance nature, JpaRepository will have all the behaviors of CrudRepository and PagingAndSortingRepository. So if you don't need the repository to have the functions provided by JpaRepository and PagingAndSortingRepository , use CrudRepository.
No comments:
Post a Comment