Freshers / Beginner level questions
Freshers / Beginner level questions & answers
Ques 1. What is JPA?
JPA (Java Persistence API) is a Java EE and Java SE specification that describes a management system for saving java objects to relational database tables in a convenient form.
Java itself does not contain JPA implementations, however, there are many implementations of this specification from different companies (open and not). This is not the only way to save java objects in databases (ORM systems), but one of the most popular in the Java world.
Ques 2. What is JPA and its features?
- JPA supports pluggable, third-party persistence providers such as Hibernate and TopLink, etc.
- JDK 11 annotations are fully supported.
- Few java classes are required to develop persistence applications.
- JPA application can run outside the container also. So, developers can use JPA capabilities in desktop applications also.
- No need to write deployment descriptors. Annotations-based meta-data are supported in JPA applications.
- Annotation defaults can be used in the model class, which saves a lot of development time.
- Provides cleaner, easier, standardized object-relational mapping.
- JPA supports inheritance, polymorphism, and polymorphic queries.
- JPA also supports named (static) and dynamic queries.
- JEB QL is a very powerful query language provided by JPA.
- JPA helps you build a persistence layer that is vendor-neutral and any persistence provider can be used.
- Many IDEs are also available to ease the development of JPA applications.
- Some IDEs can generate the model and persistence code from the database schema.
- JPA application can also be configured to generate database schema based on the persistence model.
- It is also very easy to switch to the most performing persistence provider. You can easily move to any commercial persistence provider.
Ques 3. Why to use JPA?
- JPA is the standard, and standards are good!
- Using JPA does not tie you to Hibernate.
- JPA gives you most of the features of plain old Hibernate, except:
- No criteria queries in JPA 2.0. Criteria query is a neat feature of Hibernate that constructs queries using Java-based combinators instead of the alternate query language, getting the benefit of IntelliSense and Eclipse's refactoring tools.
- JPA doesn't have Hibernate's DeleteOrphan cascade type.
- Delete Orphan is a useful annotation that directs Hibernate to delete entities in a collection if the parent is deleted, preventing orphaning.
- JPA doesn't have an equivalent to Hibernate's ScrollableResults.
Ques 4. What is ORM in JPA?
Mapping between database tables and java objects called ORM (Object Relational Mapping). JPA (Java Persistence API) provides and ORM facility for managing relational tables in Java applications. It is a specification and few implementations are like Hibernate, JDO, EJB, Toplink. By using JPA can be fetched data, insert, update etc.
Ques 5. What is an Entity?
A class which should be persisted in a database it must be annotated with javax.persistence.Entity. Such a class is called Entity.
While insert, update or fetch record to or from the database we use entity as mapping with relational tables.
Ques 6. What is the difference between persistence.xml and hibernate.cfg.xml?
When using JPA need persistence.xml and while using Hibernate API need hibernate.cfg.xml. When using JPA or Hibernate not needed both xmls, need the xml configuration file according to JPA or Hibernate.
Ques 7. What is the difference between JPA and Hibernate?
Hibernate is one of the most popular open-source implementations of the latest specification (JPA 3.0). Even more likely the most popular, almost standard de facto.
That is, JPA only describes rules and APIs, and Hibernate implements these descriptions, however Hibernate (like many other JPA implementations) has additional features not described in JPA (and is not portable to other JPA implementations).
For solid differences, check JPA vs Hibernate.
Ques 8. What are the two types of elements in Entity classes?
JPA indicates that it can work both with properties of classes (property), designed in the style of JavaBeans, or with fields (field), that is, class variables (instance variables). Accordingly, the type of access will be either property access or field access.
Ques 9. What types of connections (relationships) between Entities?
There are four types of connections
- One-To-One: one-to-one connection, that is, one Entity object can be associated with no more than one object of another Entity
- One-To-Many: one-to-many connection, one Entity object can be associated with a whole collection of Entity
- Many-To-One: A relationship where one entity (column or set of columns) is/are referenced with another entity (column or set of columns) that contains unique values. In relational databases, these relations are applicable by using foreign key/primary key between tables.
- Many-To-Many: A relationship is a connection between two types of entities. In the case of a many-to-many relationship, both sides can relate to multiple instances of the other side. Note that it's possible for entity types to be in a relationship with themselves.
Bi-directional: A mapping is the most common way to model this relationship with JPA and Hibernate. It uses an attribute on the Order and the OrderItem entity. This allows you to navigate the association in both directions in your domain model and your JPQL queries.
Uni-directional: A mapping is an association between one persistence object and another one related persistence object. If one persistence object uses another and in back if other is not using the first persistence object then it becomes unidirectional.
Ques 10. What is Persistent Fields?
- If the entity class uses persistent fields, the Persistence runtime accesses entity-class instance variables directly.
- All fields not annotated javax.persistence.Transient or not marked as Java transient will be persisted to the data store.
- The object/relational mapping annotations must be applied to the instance variables.
Ques 11. Is it possible to use JPA with NoSQL databases?
In general, the JPA specification says only about mapping java objects into relational database tables, but there are a number of implementations of this standard for NoSQL databases: Kundera, DataNucleus, ObjectDB, and a number of others. Naturally, not all specification-specific features for relational databases are transferred to NoSQL databases completely.
Ques 12. What JPA requirements for Entity classes can you list?
1) Entity class must be annotated with Entity or described in the XML configuration file JPA,
2) Entity class must contain a public or protected constructor with no arguments (it can also have constructors with arguments),
3) Entity class must be a top-level class (top-level class),
4) Entity class cannot be an enum or interface,
5) Entity class cannot be the final class,
6) Entity class cannot contain final fields or methods if they participate in the mapping (persistent final methods or persistent final instance variables),
7) If an Entity class object is passed by value as a separate object (detached object), for example through a remote interface (through a remote interface), it must also implement a Serializable interface,
8) The Entity class fields should be directly accessible only to the methods of the Entity class and should not be directly accessible to other classes using this entity. Such classes should refer only to methods (getter/setter methods or other business logic methods in the Entity class),
9) The Entity class must contain a primary key, that is, an attribute or group of attributes that uniquely defines the record of this Entity class in the database.
Ques 13. What is Persistent Properties?
- If the entity uses persistent properties, the entity must follow the method conventions of JavaBeans components.
- JavaBeans-style properties use getter and setter methods that are typically named after the entity class's instance variable names.
- For every persistent property of type Type of the entity, there is a getter method getProperty and a setter method setProperty.
- If the property is a Boolean, you may use isProperty instead of getProperty. For example, if a Customer entity uses persistent properties and has a private instance variable called firstName, the class defines a getFirstName and setFirstName method for retrieving and setting the state of the firstName instance variable.
The method signature for single-valued persistent properties are as follows:
Type getProperty()
void setProperty(Type type)
The object/relational mapping annotations for persistent properties must be applied to the getter methods. Mapping annotations cannot be applied to fields or properties annotated @Transient or marked transient.
Ques 14. Explain Life Cycle of a JPA Entity.
Key states that an entity might be in:
- New / Transient: An object is instantiated but not yet associated with an Entity Manager and has no representation in the database.
- Managed / Persisted.
- Detached: Detached entity objects are objects in a special state in which they are not managed by any EntityManager but still represent objects in the database. Detached objects are often returned from a persistence tier to the web layer where they can be displayed to the end user in some form. Changes can be made to a detached object, but these changes won't be persisted in the database until the entity is reassociated with a persistence context (the entity is merged back to an EntityManager to become managed again).
- Removed.
- The merge method's major task is to transfer the state from an unmanaged entity (passed as the argument) to its managed counterpart within the persistence context.
- Merge deals with both new and detached entities. Merge causes either INSERT or UPDATE operation according to the sub-scenario (on the one hand it is more robust, on the other hand, this robustness needn't be required.)
- Persist always causes INSERT SQL operation is executed (i.e. an exception may be thrown if the entity has already been inserted and thus the primary key violation happens.)
Ques 15. Insert a record mechanism using JPA. Provide an example.
@Override
@Transactional
public void create(Category entity) throws MeetingAppDAOException {
try {
logger.info("Enter - create()");
super.create(entity);
logger.info("Exit - create()");
} catch (PersistenceException exception) {
logger.error("create()::REASON OF EXCEPTION=" + exception.getMessage(), e);
}
}
Ques 16. What is the attribute of the Entity class in JPA terminology?
JPA indicates that it can work both with properties of classes (property), designed in the style of JavaBeans, or with fields (field), that is, class variables (instance variables). Both types of elements of the Entity class are called attributes of the Entity class.
Most helpful rated by users:
- What is ORM in JPA?
- What is an EntityManager?
- What is the difference between persistence.xml and hibernate.cfg.xml?
- What is an Entity?
- Why to use JPA?