Chapter 5. Mapping value types

 

In this chapter

  • Mapping basic properties
  • Mapping embeddable components
  • Controlling mapping between Java and SQL types

After spending the previous chapter almost exclusively on entities and the respective class- and identity-mapping options, we now focus on value types in their various forms. We split value types into two categories: basic value-typed classes that come with the JDK, such as String, Date, primitives, and their wrappers; and developer-defined value-typed classes, such as Address and MonetaryAmount in CaveatEmptor.

In this chapter, we first map persistent properties with JDK types and learn the basic mapping annotations. You see how to work with various aspects of properties: overriding defaults, customizing access, and generated values. You also see how SQL is used with derived properties and transformed column values. We wrap up basic properties with temporal properties and mapping enumerations. We then discuss custom value-typed classes and map them as embeddable components. You learn how classes relate to the database schema and make your classes embeddable, while allowing for overriding embedded attributes. We complete embeddable components by mapping nested components. Finally, we discuss how to customize loading and storing of property values at a lower level with flexible JPA converters, a standardized extension point of every JPA provider.

5.1. Mapping basic properties

Path: /model/src/main/java/org/jpwh/model/advanced/Item.java

5.2. Mapping embeddable components

Path: /model/src/main/java/org/jpwh/model/simple/Address.java

Path: /model/src/main/java/org/jpwh/model/simple/User.java

Path: /model/src/main/java/org/jpwh/model/simple/User.java

Path: /model/src/main/java/org/jpwh/model/advanced/Address.java

@Embeddable
public class Address {
<enter/>
    @NotNull
    @Column(nullable = false)
    protected String street;
<enter/>
    @NotNull
    @AttributeOverrides(
        @AttributeOverride(
            name = "name",
            column = @Column(name = "CITY", nullable = false)
        )
    )
    protected City city;
<enter/>
    // ...
}

Path: /model/src/main/java/org/jpwh/model/advanced/City.java

5.3. Mapping Java and SQL types with converters

Path: /examples/src/test/java/org/jpwh/test/advanced/LazyProperties.java

Path: /model/src/main/java/org/jpwh/model/advanced/MonetaryAmount.java

Path: /model/src/main/java/org/jpwh/converter/MonetaryAmountConverter.java

Path: /model/src/main/java/org/jpwh/model/advanced/converter/Item.java

Path: /model/src/main/java/org/jpwh/model/advanced/converter/Zipcode.java

abstract public class Zipcode {
<enter/>
    protected String value;
<enter/>
    public Zipcode(String value) {
        this.value = value;
    }
<enter/>
    public String getValue() {
        return value;
    }
<enter/>
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Zipcode zipcode = (Zipcode) o;
        return value.equals(zipcode.value);
    }
<enter/>
    @Override
    public int hashCode() {
        return value.hashCode();
    }
}

Path: /model/src/main/java/org/jpwh/model/advanced/converter/GermanZipcode.java

public class GermanZipcode extends Zipcode {
<enter/>
    public GermanZipcode(String value) {
        super(value);
    }
}

Path: /model/src/main/java/org/jpwh/converter/ZipcodeConverter.java

The extension points

sitemap