Hibernate / Hibernate 7 Basics Interview Questions
How does @Embeddable and @Embedded mapping work in Hibernate 7?
@Embeddable marks a class as a value type stored within the parent entity's table. Hibernate 7 adds @EmbeddedColumnNaming (incubating) to simplify prefix/suffix mapping for multiple embeddables of the same type.
@Embeddable public class Address { @Column(name="street") private String street; @Column(name="city") private String city; @Column(name="postcode", length=10) private String postcode; } @Entity public class Customer { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private Long id; private String name; @Embedded private Address shippingAddress; // columns: street, city, postcode // Hibernate 6 approach (verbose - one @AttributeOverride per field): @Embedded @AttributeOverrides({ @AttributeOverride(name="street", column=@Column(name="billing_street")), @AttributeOverride(name="city", column=@Column(name="billing_city")), @AttributeOverride(name="postcode", column=@Column(name="billing_postcode")) }) private Address billingAddress; // Hibernate 7 NEW (incubating): much simpler! // @Embedded // @EmbeddedColumnNaming("billing_{column_name}") // private Address billingAddress; // Generates: billing_street, billing_city, billing_postcode }
More Related questions...