Hibernate Entity Types vs Value Types

/, JPA/Hibernate Entity Types vs Value Types
Note:
Maven dependencies used all over JPA tutorials same. Please follow "Setting up Project" section to understand about project configuration.
persistence.xml and other code files reused from How to Persist an Entity in data base using JPA?
Each Sample application from download includes SQL file also.

Big Picture of Entity types vs Value Types :

Entity Types :

  • If an object has its own database identity (primary key value) then it’s type is Entity Type.
  • An entity has its own lifecycle. It may exist independently of any other entity.
  • An object reference to an entity instance is persisted as a reference in the database (a foreign key value).
  • From the above picture, College is an Entity Type. It has it’s own database identity (It has primary key).

Value Types :

  • If an object don’t have its own database identity (no primary key value) then it’s type is Value Type.
  • Value Type object belongs to an Entity Type Object.
  • It’s embedded in the owning entity and it represents the table column in the database.
  • The lifespan of a value type instance is bounded by the lifespan of the owning entity instance.

Basic, Composite, Collection Value Types :

Basic Value Types :

  • Basic value types are : they map a single database value (column) to a single, non-aggregated Java type.
  • Hibernate provides a number of built-in basic types.
  • String, Character, Boolean, Integer, Long, Byte, … etc.
  • You can find all basic types in hibernate documentation Hibernate Basic Value Types

Composite Value Types :

  • In JPA composite types also called Embedded Types. Hibernate traditionally called them Components.
  • Composite Value type looks like exactly an Entity, but does not own lifecycle and identifier.
  • Composite Value types will be discussed in next section more clearly.
  • Mapping Composite Value Types Example

Collection Value Types :

2017-09-06T15:43:24+00:00 Categories: Hibernate, JPA|Tags: , , , , |0 Comments

Leave a Reply