What is the difference between "Persistent Class" and "Entity" in JPA or in Hibernate?

1k Views Asked by At

First things first:

  1. I'm aware of this question, which, at the high level of abstraction, asks same what I ask here; however, I find it (and its answer) lacking, therefore, I would like to elaborate on this topic here;
  2. I would kindly ask anyone, who has a good understanding of the formal definitions in the JPA and Hibernate, to help me clarify this mess.

Disclaimer: I am not looking for the basic explanations of what the Entity is and how to use it. I am using Hibernate/JPA for quite some time now.

My question is more about formal definitions, in order to clarify the distinctions of the formal semantics (definitions) between Entity and Persistent Class, to make it crystal-clear how one differs from another and how they are defined.

Initially, I expected the answer to be found in JSR 338: JPA Specification 2.2 and Hibernate 5.0.0 Reference Documentation, but I got quite confused, as I was left with a feeling, that, in this context, both documents are lacking, leaving a big room for these concepts/definitions to overlap and introduce some speculations.


Let's start with JSR 338: JPA 2.2 Specification:

For some (very strange to me) reason, in this entire specification, keyword phrases "Persistent Object" and "Persistent Class" are not found, to wit, they are found only once and twice respectively:

  • Persistent Object - found in the Javadoc comment, saying nothing interesting (page 221)
  • Persistent Class - found in the xsd documentation, again, saying nothing much (pages: 378, 529)

and, unfortunately, that is it in this specification, nothing more at all.

The Entity Class, on the other hand, is defined as follows:

An entity is a lightweight persistent domain object. The primary programming artifact is the entity class. An entity class may make use of auxiliary classes that serve as helper classes or that are used to represent the state of the entity.

which, again, I find extremely lacking for the formal specification document, but at least, this says that it is a persistent domain object.

In the same document, Entity is also defined as a Class (2.1 The Entity Class, page 23).

Even though this might not seem important, it's quite confusing whether it's defined as a Persistent Object or as a Class.

My conclusion based on this specification document:

  1. Persistent Class/Object is not defined in the JPA 2.2 Specification at all;
  2. Entity is defined as persistent object, and(!), as a Class, which is really unclear to me;

In my conceptual understanding, Persistent Object, sounds very much as an object, which is persistable if(!) it will be mapped correspondingly; hence, Persistent Class - as a Class, instances of which are possible to get persisted (i.e. they can be stored into database layer, by conforming to POJO concept), if the class is mapped accordingly. I.e. Persistent Class is a persistable class, and it transforms to Entity, if it has its corresponding mapping definition (either with .hbm.xml or JPA annotations).


Hibernate 5 Reference Documentation

On the other hand, Hibernate defines Persistent Classes, and it does this as follows:

Persistent classes are classes in an application that implement the entities of the business problem (e.g. Customer and Order in an E-commerce application). The term "persistent" here means that the classes are able to be persisted, not that they are in the persistent state.

Am I getting this correct, that Persistent Classes implement the entity means, that my upper understanding is correct, and Persistent Class is just a Java class, which implements the Entity model (without mapping), and in conjunction with its mapping (.hbm.xml or JPA) it becomes an Entity? is Persistent Class + Mapping Definition = Entity correct?


I would also plug here in some snippets from the Java Persistence with Hibernate, Second Edition book:

You wrote a persistent class and its mapping with annotations. - (Summary 2.4);

kind of, differentiates persistent class from its mapping, and that, again, sort of supports the idea, that persistent class and its mapping are distinct concepts, which logically brings the point, that persistent class alone, is not an Entity.

The persistent classes are unaware of—and have no dependency on—the persistence mechanism. - (Chapter 3.2);

clearly states, that Persistent Classes are unaware of the persistence mechanism.

You can reuse persistent classes outside the context of persistence, in unit tests or in the presentation layer, for example. You can create instances in any runtime environment with the regular Java new operator, preserving testability and reusability. - (Chapter 3.2).

also supports the idea, that Persistent Class is not necessarily a mapped Entity.

My conclusion:
(based on my initial understanding, which seems to be supported by "Hibernate Reference Documentation" and "Java Persistence with Hibernate, Second Edition" book.)

Persistent Classes are Java classes, which (if needed!) would be conforming with the JPA Specification (according to which, they need to be POJOs and they can be mapped), but they are not Entities if they are not mapped, and they can be used in the outside-of-JPA context, to just operate on them as on ordinary Java objects, or even to present them at the Presentation Layer.


Finally:

As you can see, my question is a bit of a mixture of different resources, showing my evident confusion, as I cannot clearly define the formal, so called scientifically adopted definitions for these two concepts.

Can we clearly define what is the Persistent Class, what is Entity, and what is the the semantic and definitive difference/overlapping between them in terms of JPA and Hibernate?

0

There are 0 best solutions below