Interview Questions on Java EE – Java Persistence API (JPA) – Part 2

What is an Embeddable class?

An Embeddable class is a class that is not used by itself, only as part of one or more Entity classes. Entity classes can contain both single embedded classes and collections of such classes. At runtime, each embedded class belongs to only one Entity class object and cannot be used to transfer data between Entity classes (that is, this class is not a common data structure for different objects). In general, this class serves to render the definition of common attributes for several Entity; we can assume that JPA embeds merely in Entity instead of the object of such class those attributes that it contains.

What types of relations (relationship) between Entity you know (list eight types, or specify four types of links, each of which can be divided into two more types)?

There are the following four types of links:

  1. OneToOne (one-to-one relationship, that is, one Entity can be associated with no more than one object of another Entity),
  2. OneToMany (one-to-many relationship, one Entity can be associated with a whole collection of other Entity),
  3. ManyToOne (many-to-one communication, feedback for OneToMany),
  4. ManyToMany (many-to-many relationship) Each of which can be divided into two more types:
  5. Bidirectional
  6. Unidirectional – the link to the link is set for all Entity, that is, in the case of OneToOne AB in Entity A there is a reference to Entity B, in Entity B there is a link to Entity A, Entity A is considered the owner of this connection (this is important for cases of cascading data deletion, then when removing A, B will also be deleted, but not vice versa.). Unidirectional – the link to the link is set only on one side, that is, in the case of OneToOne AB just Entity A will have a reference to Entity B; for Entity B, the reference to A will not be.

What is Mapped Superclass?

Mapped Superclass is a class from which Entity is inherited, it can contain JPA annotations, but the class itself is not Entity, it does not have to fulfil all the requirements set for Entity (for example, it may not contain a primary key). This class cannot be used in EntityManager or Query operations. This class should be marked with the MappedSuperclass annotation or, respectively, described in the XML file.

What are the three types of Inheritance Mapping Strategies described in JPA?

In JPA, three Inheritance Mapping Strategies are described, that is, how JPA will work with Entity class inheritance classes:

1) One table for the entire hierarchy of inheritance – All entity, with all heirs recorded in one table, the special column “discriminator column” is defined to identify the entity type. For example, if there are an Entity Animals with the Cats and Dogs descendant classes, with this strategy, all the entities are written to the Animals table, but they have an additional animalType column in which the value cat or dog is respectively written. The minuses’ that in the common table, all fields unique for each of the descendant classes that will be empty for all other child classes will be created. For example, in the table of animals, there will also be a speed of climbing a tree from cats and whether a dog can bring sneakers from dogs that will always have a null for dog and cat, respectively.

2) Joined subclass strategy – In this strategy, each entity class stores data in its table, but unique columns (not inherited from the ancestor classes) and the primary key, and all legacy columns are written to the ancestor class tables, additionally the relationships between these tables are established, for example, in the case of the Animals classes (see above), there will be three tables of animals, cats, dogs, and in cats only the key and the speed of the lasagna will be recorded, in dogs – the key and whether the dog can bring a stick , and in animals all other data cats and dogs clink to the appropriate table. The downside here is the performance loss from joining the tables for any operations.

3) One table for each class – Here everything is simple, each separate class-heir has its table, i.e. for cats and dogs (see above), all data will be written merely into the cats and dogs tables as if they did not have a common superclass at all. The downside is the poor support of polymorphic relationships and the fact that sampling all classes of the hierarchy will require a large number of individual SQL queries or the use of a UNION query.

What are the two types of fetch strategies in JPA you know?

JPA describes two types of fetch strategies:

1) LAZY – the field data will be loaded only during the first access to this field,

2) EAGER – the field data will be loaded immediately.

Close

About The Author

Ruchi Singla
An enthusiast writer with years of experience, who loves to create beautiful thoughts into words. She writes with a zeal and enjoys reading vogue with a good cup of coffee when not writing. Ruchi is working as a content writer for many businesses, helping them gain an extra edge over their competitors. As a content writer, she is responsible for enhancing engagement and traffic on the blog by brainstorming exciting content ideas.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.