Property access maps persistent state through getter and setter methods. For an embeddable component, the owning entity’s access strategy determines how Hibernate reads and writes values and where mapping annotations belong. section 6.2.2
Apply the strategy consistently across an entity and its embeddable components:
- With property access, place mapping annotations on getter methods and let the persistence provider invoke getters and setters. section 6.2.2
- With field access, place mapping annotations on fields and let the provider read and write fields directly; getters and setters are optional. section 6.2.2
- An embedded property can override the owning entity’s strategy with @Access, changing the expected annotation locations for the component. section 6.2.2
Listing 6.7 Address class: An embeddable component
Path: Ch06/mapping-value-types/src/main/java/com/manning/javapersistence /ch06/model/Address.java @Embeddable #1 public class Address { @NotNull #2 @Column(nullable = false) #3 private String street; @NotNull @Column(nullable = false, length = 5) #4 private String zipcode; @NotNull #5 @Column(nullable = false) #5 private String city; #5 public Address() { #6 } public Address(String street, String zipcode, String city) { #7 this.street = street; this.zipcode = zipcode; this.city = city; } //getters and setters } #1 Instead of <kbd>@Entity</kbd>, this component POJO is marked with <kbd>@Embeddable</kbd>. It has no identifier property. #2 The <kbd>@NotNull</kbd> annotation is ignored by the DDL generation. #3 <kbd>@Column(nullable=false)</kbd> is used for DDL generation. #4 The length argument of the <kbd>@Column</kbd> annotation will override the default generation of a column as <kbd>VARCHAR(255)</kbd>. #5 The type of the <kbd>city</kbd> column will by default be <kbd>VARCHAR(255)</kbd>. #6 Hibernate or Spring Data JPA using Hibernate call this no-argument constructor to create an instance and then populate the fields directly. #7 We can have additional (public) constructors for convenience.
The Address embeddable provides a concrete mapping context for comparing field and property access. section 6.2.2
