Home Hibernate Hibernate Types and Entity vs Value Types

Hibernate Types and Entity vs Value Types

In the broadest sense, Hibernate Types categorizes into two groups. In this article we will see overview Hibernate mapping types and distinguishes between them.

1. Hibernate types

To understand the behavior of various Java language-level objects with respect to the persistence service, hibernate classify them into two groups:

  1. Entity types
  2. Value types

2. Big Picture of Entity types vs Value Types :

Hibernate Entity Types vs Value Types

1. 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).

Mapping Entity Type Example :

The entity class must be annotated with the javax.persistence.Entity annotation.

@Entity(name = "STUDENT")
public class Student {

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO, generator = "native")
	@GenericGenerator(name = "native", strategy = "native")
	@Column(name = "ID")
	private Long studentId;

	@Column(name = "FNAME")
	private String firstName;

	@Column(name = "LNAME")
	private String lastName;

	@Column(name = "CONTACT_NO")
	private String contactNo;

        // Setters and Getters
}

2. 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.

2.1. 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, java.sql.Date, java.net.URL, java.sql.Blob, java.util.UUID … etc are the built-in basic value types.
  • You can find all basic types in hibernate documentation Hibernate Basic Value Types

2.2. Composite Value Types (Embeddable 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.
  • Mapping Composite Value Types Example

2.3. Collection Value Types :

Hibernate allows to persist collections. Collection value types sub categorized into 3 types

  1. Collection of basic value types
  2. Collection of Composite or embeddable types.
  3. Collection of custom types

3. Conclusion

In this guide we have covered what are Hibernate Types and distinguishes between Hibernate Entity types and Value Types.

Satish Varma
Satish Varmahttps://javabydeveloper.com
Satish is post graduated in master of computer applications and experienced software engineer with focus on Spring, JPA, REST, TDD and web development. Follow him on LinkedIn or Twitter or Facebook

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Stay in Touch

Categories