Monday, August 5, 2024

Java Persistence API

What is Second Level Caching in JPA?
What is the difference between hibernate and JPA?
  • JPA defines the specification. It is an API.
    • Provides following definitions
      • How we define the entities
      • How do we define primary key?
      • Who manages the entities?
      • How we map attributes
    • JPA stands for Java persistence API
    • The JPA specification was developed by Java as a standard way to map objects to relational databases.
    • JPA just provides specification It does not provide the implementation.
    • So we cannot use JPA By itself, we need to use a JPA provider That implements that JPA framework Like eclipselink, toplink and hibernate.
    • The advantage of using a framework that implements the JPA specification Is that you can easily switch the framework.
    • So if we are using top link, we can easily change our framework to eclipse link or hibernate without having to change code.
    • If we use JPA methods In our code, we will be easily able to switch to some other JPA provider Like eclipse link or top link.
    • Spring Data JPA Is an abstraction layer on top of JPA To reduce the amount of boiler plate code required to implement data access object(DAO) layer.
    • Spring data JPA Uses hibernate as a default JPA provider.
    • Spring data JPA cannot work without a JPA provider.
  • A JPA repository abstracts our database operations.
  • Hibernate is an implementation of JPA
    • Using hibernate directly would result in a lock into hibernate.
      • There are other JPA implementation available like top link, et cetera.
    • When we use hibernate in our code, we can use the proprietary methods Off hibernate, or we can use the JPA methods implemented by hibernate.
    • Hibernate is a Java based ORM Tool that provides a framework for mapping application domain objects to Relational database tables, and vice versa.
    • Hibernate is a JPA provider(JPA specification implementation) Spring data JPA is not a JPA provider. It simply hides the Java persistent API.(And the JPA provider) Behind its repository Abstraction.
What is the difference between @Entity and @Table annotations in JPA?
  • We can use the name attribute of the @Entity annotation to provide an explicit entity name to match with the database table name. For our example we could have used @Entity(name = “tbl_address”) in our Address class if our table name was tbl_address.
    • @Entity is needed for annotating a POJO class as an entity
  • We can use a @Table (defined in the javax.persistence package) annotation just below the @Entity annotation and use its name attribute to specify the table name explicitly
    • @Table is optional
Q What are the difference values of spring.jpa.hibernate.ddl-auto?
Ans 
  • Create
    • Drops and Creates the tables whenever we start the server
  • Create-Drop
    • Drops all the tables as soon as we stop the server.
    • Recreates them fresh when we start the server
  • update
    • Only updates the database when we start the server.
  • insert
  • validate

No comments:

Post a Comment