JoonT
JoonT

Reputation: 1236

What is the entity in JPA?

Somebody asked me, what is the entity?
Unfortunately, I didn't know correctly definition.

First I answered
Entity is just the class that related by Relational table.
But I thought that is not whole meaning..

And Second I answered
Entity is the one that is managed by persistence context.

And then he asked me,
So.. The table that created by @ManyToMany relation is entity?

And then I started to confuse.
What is the entitiy's correct meaning?

And please answer about table that created by @ManyToMany question.

Upvotes: 1

Views: 332

Answers (2)

MWiesner
MWiesner

Reputation: 9043

The JPA specification document (version 2.2) says it all on page 23:

An entity is a lightweight persistent domain object.

Per se, an Entity is just a marker that a a certain type can be persisted to some persistence store, typically a (relational) database. It does not define, however, how relationships to other types are mapped / expressed within that domain.

In the context of ORM, such domain objects (i.e., instances of a certain type in the domain) are mapped to the schema of a database which - in relational databases - is expressed by a set of Tables.

For a correct mapping of a n:m relationships (@ManyToMany), additional structures in the persistence store might be required, typically expressed by an additional table, e.g. TableA_TableB. Thus, one can not claim that an entity always corresponds to one table exactly, in particular when relationships to other types are needed.

From a bidirectional perspective, tables in a (relational) database can not necessarily be mapped to domain types in the sense that they would exactly represent a type of a domain. Or in other words: Tuples/rows in a relational table do not necessarily correspond to an instance (object) of a certain type. For the background and further details see Database Normalization Theory by Mike Hillyer.

Hope it helps.

Upvotes: 1

Simon Martinelli
Simon Martinelli

Reputation: 36103

Have a look a the Java EE tutorial:

https://docs.oracle.com/javaee/7/tutorial/persistence-intro001.htm#BNBQA

There you will find the definition:

7.1 Entities

An entity is a lightweight persistence domain object. Typically, an entity represents a table in a relational database, and each entity instance corresponds to a row in that table. The primary programming artifact of an entity is the entity class, although entities can use helper classes.

Or you could also have a look at the JPA specification:

http://download.oracle.com/otndocs/jcp/persistence-2_2-mrel-eval-spec/index.html

Upvotes: 1

Related Questions