9

What is the difference between entity and value types at the object level. I understand that entity will have an id but value won't but why do we need different ways to map entity vs value types?

Is it done so that hibernate can apply any optimization to value type?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
sab
  • 9,767
  • 13
  • 44
  • 51
  • This site has got all about this, https://javabydeveloper.com/hibernate-entity-types-vs-value-types/ – Ram Jul 27 '18 at 12:35

2 Answers2

15

An entity already defines the table where it's persisted. So when you have a list of B entities in an entity A, there is no need to define the target table for the Bs: B already defines it. Value types don't have any associated table, so the mapping of a List<String> in entity A must define which table will be used to store this list.

Moreover, value types, by definition are always completely owned by their containing entity. Once you delete the entity, you also delete all the Strings associated to this entity. This is not the case with entities: when you delete a Course entity, you don't delete all its students.

These are just two examples showing that different mapping properties must be defined.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
0

An object of entity type has its own identity where as An object of value type has no database identity,it belongs to an entity. Value type objects are identified through owning entities