Can a JPA Entity Class Have Multiple Embedded Fields?

Question

Is it possible for a JPA entity class to include multiple embedded fields, and how can I customize their column names without using @AttributeOverrides for each attribute?

@Entity
public class Person {
    @Embedded
    public Address home;

    @Embedded
    public Address work;
}

public class Address {
    public String street;
    public String city;
    public String zipCode;
}

Answer

Yes, it is possible for a JPA entity to include multiple embedded fields, such as two instances of the same embedded class. However, customizing the column names when using multiple embedded fields can be challenging because JPA requires you to use @AttributeOverrides on each field manually, which can be cumbersome for large embedded classes.

@Entity
public class Person {
    @Embedded
    @AttributeOverrides({
        @AttributeOverride(name = "street", column = @Column(name = "home_street")),
        @AttributeOverride(name = "city", column = @Column(name = "home_city")),
        @AttributeOverride(name = "zipCode", column = @Column(name = "home_zip_code"))
    })
    public Address home;

    @Embedded
    @AttributeOverrides({
        @AttributeOverride(name = "street", column = @Column(name = "work_street")),
        @AttributeOverride(name = "city", column = @Column(name = "work_city")),
        @AttributeOverride(name = "zipCode", column = @Column(name = "work_zip_code"))
    })
    public Address work;
}

Causes

  • JPA does allow multiple @Embedded annotations within a single entity class.
  • The default behavior of Hibernate does not automatically distinguish differing embedded instances by column names if not specified.

Solutions

  • Utilize @AttributeOverrides to manually specify column names for each embedded field, though this can become tedious with larger classes.
  • Consider creating separate embedded classes if customization of many fields is necessary, which allows for clearer structure in the database schema.

Common Mistakes

Mistake: Failing to use @AttributeOverrides when expecting the column names to auto-generate based on the embedded fields.

Solution: Always specify @AttributeOverrides to customize column names for embedded fields.

Mistake: Not separating concerns if an embedded class becomes too large and complex.

Solution: If the embedded class has many fields, consider creating specific embedded classes tailored to the context they are used in.

Helpers

  • JPA
  • Hibernate
  • Embedded fields
  • @Embedded
  • @AttributeOverrides
  • JPA entity
  • multiple embedded fields
  • customizing column names

Related Questions

⦿How to Modify Android Option Menu Items Programmatically?

Learn how to change Android option menu items programmatically and disable specific items for click events.

⦿How to Effectively Profile Your Android App for Performance Bottlenecks?

Discover the best profiling tools and techniques for identifying performance bottlenecks in your Android app to enhance its efficiency.

⦿How to Effectively Use Intent.FLAG_ACTIVITY_CLEAR_TOP to Clear the Activity Stack in Android

Learn how to use Intent.FLAGACTIVITYCLEARTOP to manage the Android activity stack effectively with expert tips and code examples.

⦿How to Disable Spacebar Autocomplete Trigger in Eclipse?

Learn how to stop the spacebar from triggering autocomplete in Eclipse and customize key bindings for a smoother coding experience.

⦿Understanding Stateless Objects in Java: A Comprehensive Guide

Learn about stateless objects in Java their importance in concurrency and how they ensure thread safety.

⦿Why is Spring @Value Not Resolving Values from the Properties File?

Discover common reasons and solutions when Spring Value fails to resolve properties from your configuration files.

⦿How to Resolve the "Disabling Contextual LOB Creation" Error in Hibernate with Oracle?

Learn how to fix the Disabling contextual LOB creation error in Hibernate with Oracle Database 10g. Comprehensive guide and solutions included.

⦿How to Resolve Duplicate Files in APK META-INF When Including License Files in Android

Learn how to fix DuplicateFileException in APK METAINF while ensuring compliance with library licenses in your Android project.

⦿How to Download Source Code for a JAR File Using Maven?

Learn how to download source code and Javadoc for Maven dependencies in your project. Stepbystep instructions and tips included.

⦿What Is the Difference Between Java Generics Wildcard `<? extends Number>` and `<T extends Number>`?

Learn about the differences between Java Generics wildcards extends Number and T extends Number with detailed explanations and examples.

© Copyright 2025 - CodingTechRoom.com