Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity and value types in hibernate

Tags:

hibernate

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?

like image 391
sab Avatar asked Nov 07 '11 15:11

sab


1 Answers

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.

like image 167
JB Nizet Avatar answered Oct 09 '22 04:10

JB Nizet