Top 25 EclipseLink Interview Questions and Answers

EclipseLink is an open source object-relational mapping (ORM) framework in Java It implements the Java Persistence API (JPA) specifications and provides additional features on top of it EclipseLink is used to persist data between Java objects and relational databases.

In interviews candidates are often asked EclipseLink and JPA related questions to test their knowledge. In this article I will go over the top 25 EclipseLink interview questions and sample answers to help you prepare for your next Java developer role.

1. What is EclipseLink?

EclipseLink is the open source Eclipse Persistence Services project from the Eclipse Foundation. It implements the Java Persistence API (JPA) specifications and provides additional features on top of it. EclipseLink supports a number of relational and NoSQL databases including Oracle, MySQL, PostgreSQL, MongoDB etc.

Some key advantages of using EclipseLink are:

  • Provides object-relational mapping capabilities to persist Java objects to databases.
  • Implements JPA 2.1 and 2.2 specifications.
  • Supports advanced mappings like composite primary keys, inheritance etc.
  • Provides support for caching, queries, change tracking and more.
  • Available under EPL open source license.

2. How does EclipseLink implement the JPA specifications?

The Java Persistence API (JPA) is a specification that provides an ORM interface to persist objects in Java applications. It does not include an actual implementation. EclipseLink serves as an implementation of the JPA specification by providing the required interfaces and classes defined in JPA.

For example, EntityManager interface and Query classes defined in JPA are implemented by EclipseLink’s EntityManagerImpl and EclipseLink query classes. So applications using JPA can leverage EclipseLink as the JPA provider by adding it as a dependency.

3. What are the key components of EclipseLink architecture?

Some key components and services provided by EclipseLink are:

  • Object-relational (O/R) Mapping – Responsible for mapping between entities and database tables.

  • Entity Manager – Manages persistent entity instances and acts as the interface for the persistence services.

  • Querying – Provides JPQL query support and additional EclipseLink query features.

  • Change Tracking – Detects changes made to entities to synchronize with database.

  • Caching – First and second level caching of entities to optimize performance.

  • Transaction Management – Integrates with JTA for container managed transactions.

4. What are the pros and cons of using EclipseLink over Hibernate?

Some pros of using EclipseLink are:

  • More lightweight and better performance than Hibernate.
  • Provides advanced O/R mapping capabilities.
  • Open source and free compared to some Hibernate editions.
  • Integrates well with other Eclipse technologies.

Some cons are:

  • Not as popular as Hibernate in Java community.
  • Lesser third party tool support compared to Hibernate.
  • Less flexible and feature-rich than Hibernate for complex stuff.

5. Explain JPA entity relationships supported in EclipseLink

EclipseLink supports all the entity relationships defined in the JPA specification:

  • One-to-One – One instance of an entity is related to one instance of another entity. For example, Employee to ParkingSpace.

  • One-to-Many / Many-to-One – One instance of an entity can be related to multiple instances of another entity. For example, Department to Employees.

  • Many-to-Many – Multiple instances of an entity can be related to multiple instances of another entity. For example, Employee to Projects.

  • ElementCollection – One entity instance related to a collection of basic types or embeddable objects. For example, Person to PhoneNumbers.

EclipseLink provides annotations like @OneToOne, @OneToMany etc to define these relationships in entities.

6. How does change tracking work in EclipseLink?

EclipseLink provides change tracking capability to identify changes made to entity objects so that those changes can be synchronized back to the database. This avoids having to re-query the persistent object from the database before updating it.

The @ChangeTracking annotation in EclipseLink is used to configure change tracking at the entity class level.

Change tracking modes supported are:

  • AUTO (default) – Changes are detected automatically
  • DEFERRED – Changes detected lazily
  • ATTRIBUTE – Only changes to mapped attributes are tracked
  • OBJECT – Track changes to entire object graph

7. What is the EclipseLink shared cache?

EclipseLink provides a shared object cache called the EclipseLink Cache that caches entities fetched from the database in memory. This improves performance by avoiding frequent trips to the database to read the same data.

The shared cache is common across all entity manager instances in an application. Cache size, expiry duration and other policies can be configured using persistence.xml or @Cache annotation.

The shared cache can store entities, queries as well as query results. It uses soft references so cached objects can be garbage collected under memory pressure.

8. How can you configure second level cache in EclipseLink?

EclipseLink provides support for second level caching in addition to the shared first level cache. This enables caching entities across multiple JVMs or EclipseLink sessions.

Some ways to configure the EclipseLink second level cache are:

  • Define @Cache(type=CacheType.SOFT_WEAK) on the entity to cache.

  • Specify <shared-cache-mode> in persistence.xml

  • Configure cache regions, expiry, storage type etc using eclipselink-orm.xml

  • Use @Cache annotation on entity to configure caching.

The most commonly used second level cache storage options are Ehcache, Coherence, and Oracle Coherence.

9. What are the different ways to fetch data in EclipseLink?

Some ways of fetching data with EclipseLink are:

  • Find – Fetch a single entity by primary key using em.find()

  • JPQL Queries – Write JPQL queries to fetch entities and data

  • Criteria API – Programmatically build criteria queries to fetch entities

  • Native SQL Queries – Write native SQL queries for specialized fetching

  • Lazy Loading – Fetch related entities on demand when accessed

  • Eager Loading – Use JOIN FETCH to eagerly load related entities

  • Batch Reading – Fetch entities efficiently in batches

10. What are named queries in EclipseLink JPA?

Named queries allow you to define reusable queries using static Java constants or XML configuration. This avoids duplicates and makes code more readable.

To define a JPQL named query in EclipseLink:

java

   @Entity   @NamedQueries({      @NamedQuery(         name="get_employees_by_last_name",         query="SELECT e FROM Employee e WHERE e.lastName = :lastname"       )   })

This named query can then be executed as:

java

   Query query = em.createNamedQuery("get_employees_by_last_name");   query.setParameter("lastname", "Smith");

11. How does EclipseLink support composite primary keys?

A composite primary key allows you to use a combination of multiple fields to uniquely identify an entity record.

EclipseLink supports composite primary keys in the following ways:

  • Define an @Embeddable class with @Embeddable annotation to hold the composite key fields

  • Specify @EmbeddedId on the entity and use EmbeddedId class as the property type

  • Use @IdClass annotation on entity to specify Composite PK class

  • Add @Id annotation on multiple fields in entity that make up the composite key

The EmbeddedId or @IdClass approach is usually the best.

12. How can you define object-relational mappings in EclipseLink?

Some ways to define ORM mappings in EclipseLink are:

  • Annotations – Define mappings using JPA annotations like @Entity, @Table etc.

  • XML – Provide ORM definitions in eclipselink-orm.xml file.

  • Programmatically – Use the EclipseLink API and descriptors to define mappings.

  • By convention – Rely on EclipseLink defaults for simple mappings.

For most applications use of JPA annotations on entities is sufficient to define mappings. XML and programmatic mapping allow more control for complex cases.

13. Explain the persist operation in EclipseLink

The persist() operation is used to persist new entities to the database in EclipseLink. Here is a typical usage:

java

EntityManager em = ...; em.getTransaction().begin();Employee emp = new Employee(); em.persist(emp);em.getTransaction().commit();

The steps are:

  • Create new entity object
  • Start a transaction
  • Call em.persist() to save new entity
  • Commit transaction to write to database

The persist operation assigns a unique ID to new entities before committing to database

Explain how can you generate JavaDoc documentation for your code?To generate JavaDoc documentation for your code,In Eclipse, you have to follow the following steps,

  • Go to File menu
  • Select Export
  • Select Java
  • JavaDoc
  • Pick out the projects, other settings, and output directory that JavaDoc will be made for.
  • Click Finish

Explain what are activities you can do in Eclipse?In Eclipse, you can do following activities,

  • Create generic projects
  • Edit files in a generic text editor
  • You can use a CVS (Concurrent Version System) server to share files and projects.

JPA with Eclipse Link Implementation || by Nagoor Babu Sir On 12-08-2018

FAQ

What is the N 1 problem in hibernate?

The N+1 problem is the situation when, for a single request, for example, fetching Users, we make additional requests for each User to get their information. Although this problem often is connected to lazy loading, it’s not always the case. We can get this issue with any type of relationship.

What is the difference between Hibernate and JPA?

JPA is the Java specification and not the implementation. Hibernate is an implementation of JPA and uses common standards of Java Persistence API. It is the standard API that allows developers to perform database operations smoothly. It is used to map Java data types with database tables and SQL data types.

What is JPA in Java interview questions?

JPA is the acronym for Java Persistent API, a Java specification used to persist data between the Java object and the relational database. It acts as the link between the object-oriented domain models and relational database systems. It requires an implementation as it doesn’t perform any function itself.

What is orm in an interview?

Online reputation management (ORM) is taking a proactive position in how you or your business is presented online to your companions, prospective clients, stakeholders, etc.

What is EclipseLink & how does it work?

EclipseLink allows you to integrate Java applications with any data source, without compromising ideal application design or data integrity. Components GlassFish Server 3.1.2. EclipseLink 2.3.0 or later. Any compliant JDBC database including Oracle Database, Oracle Database Express Edition, MySQL, and so on.

What makes EclipseLink a good JPA implementation?

EclipseLink includes a number of performance features that make it the industry’s best performing and most scalable JPA implementation. These features include: The EclipseLink cache is an in-memory repository that stores recently read or written objects based on class and primary key values.

How are EclipseLink applications deployed?

EclipseLink applications are deployed the same way in application server clusters as they are in standalone server environments. However, additional planning and configuration is required to ensure cache consistency in an application server cluster.

Does EclipseLink support Java Persistence?

EclipseLink supports Java persistence in Jakarta EE, Java SE and web containers including integration with various application servers including: EclipseLink lets you quickly capture and define object-to-data source and object-to-data representation mappings in a flexible, efficient metadata format.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *