Skip to main content

Hibernate - Value Types

Greetings!

When we design our domain models we can have more important classes and less important classes like Address, String, etc. In other word we have fine-grained object model which means more classes than tables.
For an example User can have an Address. We can create separate class for Address fields and add it as a property in User using composition.


public class User {
    private Long id;
    private String name;
    private Integer age;
    private Address address;
}


Here User is an entity. Name, Age, Address are value types.

  • Entity classes need an identifier.
  • Value type doesn't have an identifier because instances are identified through owning entity.

What is an Entity?

  • Has a database identity.
  • Has it's own life cycle.
  • Object reference is persisted in the database.

What is a Value Type?

  • Doesn't have own identity.
  • Embedded into owning entity.
  • Represent table columns.

How to map?

  • Basic types can be directly mapped using @Column. (String, Integer, etc)
  • Collection value types using collection mapping.
  • Composite value types can be mapped using @Embedable annotation.




Comments