Map Mappings


Map Mappings

A Map is a Java collection that holds elements in key-value pairs and prevents the list from having duplicate members. A map's contents can be seen as a set of keys, a collection of values, or a set of key-value mappings using the Map interface's three collection views. An unordered map may be started using java.util.HashMap and a Map is mapped with a map> element in the mapping table.

All of the properties of a persistent class, whether it's an entity or an embeddable type (more on these in section 5.2), are considered persistent by default when you map it. The default JPA rules for permanent class properties are as follows:

  • Suppose the property is of type String, BigInteger, BigDecimal, java.util.Date, java.util.Calendar, java.sql.Date, java.sql.Time, java.sql.Calendar, java.sql.Calendar, java.sql.Calendar, java.sql.Calendar, java.sql.Calendar, java It's automatically persistent, whether it's a timestamp, byte[], Byte[], char[], or Character[]. Hibernate loads and saves the property's value in a column with the same name as the property and an appropriate SQL type.
  • Otherwise, Hibernate maps the property as an embedded component of the owning class if the property's class is annotated as @Embeddable or the property itself is mapped as @Embedded. The Address and MonetaryAmount embeddable classes of CaveatEmptor are used to explore component embedding later in this chapter.
  • If the property's type is java.io.Serializable, the value is saved in its serialised form otherwise. This isn't usually what you want, and instead of keeping a heap of bytes in the database, you should always map Java classes. Imagine maintaining a database with this binary data when the programme is no longer available in a few years.
  • Otherwise, Hibernate will issue an exception on startup, claiming that it doesn't recognise the property's type.

This configuration by exception method eliminates the need to annotate a property to make it permanent; instead, you only need to specify the mapping in exceptional circumstances. JPA provides several annotations for customising and controlling basic property mappings.