📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. What is the Java Persistence API?
JPA defines only specifications, it does not provide an implementation. JPA implementation is provided as a reference implementation by the vendors developing O/R Mappers such as Hibernate, EclipseLink, and Apache OpenJPA.
2. What is Object-Relational Mapping?
3. What are the advantages of JPA?
The advantages of JPA are given below.- The burden of interacting with the database reduces significantly by using JPA.
- The user programming becomes easy by concealing the O/R mapping and database access processing.
- The cost of creating the definition file is reduced by using annotations.
- We can merge the applications used with other JPA providers
- Using different implementations can add the features to the standard Implementation which can later be the part of JPA specification.
4. What is the Difference Between JPA and Hibernate?
In short, JPA is the interface while Hibernate is the implementation.
Traditionally there have been multiple Java ORM solutions:
Each of the above implementations defines its own mapping definition or client API. The JPA expert group gathered the best of all these tools and so they created the Java Persistence API standard.
5. Defining a JPA Entity Class
The Point Entity Class Example
@Entity
public class Point {
// Persistent Fields:
private int x;
private int y;
// Constructor:
Point (int x, int y) {
this.x = x;
this.y = y;
}
// Accessor Methods:
public int getX() { return this.x; }
public int getY() { return this.y; }
// String Representation:
@Override
public String toString() {
return String.format("(%d, %d)", this.x, this.y);
}
}
6. What are the Requirements or Rules to Create JPA Entity Classes?
- The class must be annotated with the javax.persistence.Entity annotation.
- The class must have a public or protected, no-argument constructor. The class may have other constructors.
- The class must not be declared final. No methods or persistent instance variables must be declared final.
- If an entity instance is passed by value as a detached object, such as through a session bean's remote business interface, the class must implement the Serializable interface.
- Entities may extend both entity and non-entity classes, and non-entity classes may extend entity classes.
- Persistent instance variables must be declared private, protected, or package-private and can be accessed directly only by the entity class's methods. Clients must access the entity's state through accessor or business methods.
7. Java Persistence Query language
JPQL is Java Persistence Query Language defined in the JPA specification. It is used to create queries against entities to store in a relational database. JPQL is developed based on SQL syntax. But it won’t affect the database directly.JPQL can retrieve information or data using the SELECT clause and can do bulk updates using the UPDATE clause and DELETE clause. EntityManager.createQuery() API will support querying language.
8. Explain JPA Architecture or Explain the core classes and interfaces of JPA API?
Class Level Architecture
- EntityManagerFactory - This is a factory class of EntityManager. It creates and manages multiple EntityManager instances.
- EntityManager - It is an Interface, it manages the persistence operations on objects. It works like a factory for Query instance.
- Entity - Entities are the persistence objects, stored as records in the database.
- EntityTransaction - It has one-to-one relationship with EntityManager. For each EntityManager, operations are maintained by EntityTransaction class.
- Persistence - This class contains static methods to obtain EntityManagerFactory instance.
- Query - This interface is implemented by each JPA vendor to obtain relational objects that meet the criteria.
9. What is an Entitymanager?
10. Name required Steps to Persist JPA entity in a database?
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("PERSISTENCE");
- Persistence - Persistence is a bootstrap class that is used to obtain an EntityManagerFactory interface.
- createEntityManagerFactory() method - The role of this method is to create and return an EntityManagerFactory for the named persistence unit. Thus, this method contains the name of the persistence unit passed in the Persistence.xml file.
EntityManager entityManager = entityManagerFactory.createEntityManager();
- EntityManager - An EntityManager is an interface
- createEntityManager() method - It creates new application-managed EntityManager
entityManager.getTransaction().begin();
- getTransaction() method - This method returns the resource-level EntityTransaction object.
- begin() method - This method is used to start the transaction.
entityManagerFactory.close();
entityManager.close();
11. What are the steps to insert an entity?
12. What are the steps to find an entity?
13. What are the steps to update an entity?
14. What are the steps to delete an entity?
15. What Data Types Are Allowed in the Attributes of the Entity Class (Fields or Properties)?
- java.lang.String
- Other serializable types, including:
- Wrappers of Java primitive types
- java.math.BigInteger
- java.math.BigDecimal
- java.util.Date
- java.util.Calendar
- java.sql.Date
- java.sql.Time
- java.sql.TimeStamp
- User-defined serializable types
- byte[]
- Byte[]
- char[]
- Character[]
- Other entities and/or collections of entities
16. What Is the Embeddable Class in JPA?
17. What are the different types of entity mapping?
Following are the types of object-relational mapping: -- One-to-one mapping: The one-to-one mapping represents a single-valued association where an instance of one entity is associated with an instance of another entity. In this type of association, one instance of the source entity can be mapped with at most one instance of the target entity.
- One-To-Many mapping: The One-To-Many mapping comes into the category of collection-valued association where an entity is associated with a collection of other entities. In this type of association, the instance of one entity can be mapped with any number of instances of another entity.
- Many-to-one mapping: The Many-To-One mapping represents a single-valued association where a collection of entities can be associated with a similar entity. In the relational database, more than one row of an entity can refer to the same row of another entity.
- Many-to-many mapping: The Many-To-Many mapping represents a collection-valued association where any number of entities can be associated with a collection of other entities. In the relational database, more than one row of one entity can refer to more than one row of another entity.
18. What is the JPA Cascade Types?
- ALL - cascades all entity state transitions
- PERSIST - cascades the entity persist operation.
- MERGE - cascades the entity merge operation.
- REMOVE - cascades the entity remove operation.
- REFRESH - cascades the entity refresh operation.
- DETACH - cascades the entity detach operation.
19. What are the features of JPQL?
Some of the essential features of JPQL are: -- It is simple and robust.
- It is a platform-independent query language.
- JPQL queries can be declared statically into metadata or can also be dynamically built-in code.
- It can be used with any database such as MySQL, or Oracle.
20. What is the JPA Primary Key Generation Strategies?
21. Explain JPA Inheritance Strategies?
- MappedSuperclass - Inheritance is implemented in the domain model only without reflecting it in the database schema. See MappedSuperclass.
- Single table - The domain model class hierarchy is materialized into a single table that contains entities belonging to different class types. See Single table.
- Joined table - The base class and all the subclasses have their own database tables and fetching a subclass entity requires a join with the parent table as well. See Joined table.
- Table per class - Each subclass has its own table containing both the subclass and the base class properties.
Learn everything about JPA at JPA Tutorial - Java Persistence API
Comments
Post a Comment
Leave Comment