Partner – JPA Buddy – NPI (tag=JPA/Hibernate)
announcement - icon

The right tools can and will save a lot of time. As long as you are using Hibernate and IntelliJ IDEA you can boost your coding speed and quality with JPA Buddy. It will help in a lot of the day-to-day work:

  • Creating JPA entities that follow best practices for efficient mapping
  • Creating DTOs from entities and MapStruct mappers using convenient visual tools
  • Generating entities from the existing database or Swagger-generated POJOs
  • Visually composing methods for Spring Data JPA repositories
  • Generating differential SQL to update your schema in accordance with your changes in entities
  • Autogenerating Flyway migrations and Liquibase changelogs comparing entities with the database or two databases
  • … and a lot more

Simply put, you'll learn and use the best practices of Hibernate and surrounding technology and become a lot more!

Definitely visit the JPA Buddy site to see its features in action closer.

Persistence top

Get started with Spring Data JPA through the reference Learn Spring Data JPA course:

>> CHECK OUT THE COURSE

1. Overview

In this tutorial, we'll see how we can map one entity that contains embedded properties to a single database table.

For this purpose, we'll use the @Embeddable and @Embedded annotations provided by the Java Persistence API (JPA).

2. Data Model Context

First, let's define a table called company.

The company table will store basic information such as company name, address and phone as well as the information of a contact person:

public class Company {

    private Integer id;

    private String name;

    private String address;

    private String phone;

    private String contactFirstName;

    private String contactLastName;

    private String contactPhone;

    // standard getters, setters
}

However, it seems like the contact person should be abstracted out to a separate class.

The problem is that we don't want to create a separate table for those details.

So, let's see what we can do.

3. @Embeddable

JPA provides the @Embeddable annotation to declare that a class will be embedded by other entities.

Let's define a class to abstract out the contact person details:

@Embeddable
public class ContactPerson {

    private String firstName;

    private String lastName;

    private String phone;

    // standard getters, setters
}

4. @Embedded

The JPA annotation @Embedded is used to embed a type into another entity.

Let's next modify our Company class.

We'll add the JPA annotations, and we'll also change to use ContactPerson instead of separate fields:

@Entity
public class Company {

    @Id
    @GeneratedValue
    private Integer id;

    private String name;

    private String address;

    private String phone;

    @Embedded
    private ContactPerson contactPerson;

    // standard getters, setters
}

As a result, we have our entity Company, embedding contact person details and mapping to a single database table.

We still have one more problem, though, and that is how JPA will map these fields to database columns.

5. Attributes Override

Our fields were called things like contactFirstName in our original Company class and now firstName in our ContactPerson class. So, JPA will want to map these to contact_first_name and first_name, respectively.

Aside from being less than ideal, it will actually break us with our now-duplicated phone column.

So, we can use @AttributeOverrides and @AttributeOverride to override the column properties of our embedded type.

Let's add this to the ContactPerson field in our Company entity:

@Embedded
@AttributeOverrides({
  @AttributeOverride( name = "firstName", column = @Column(name = "contact_first_name")),
  @AttributeOverride( name = "lastName", column = @Column(name = "contact_last_name")),
  @AttributeOverride( name = "phone", column = @Column(name = "contact_phone"))
})
private ContactPerson contactPerson;

Note that since these the annotations go on the field, we can have different overrides for each enclosing entity.

6. Conclusion

In this article, we configured an entity with some embedded attributes and mapped them to the same database table as the enclosing entity.

To do this, we used the @Embedded, @Embeddable, @AttributeOverrides and @AttributeOverride annotations provided by the Java Persistence API.

As always, the source code of the example is available over on GitHub.

Persistence bottom

Get started with Spring Data JPA through the reference Learn Spring Data JPA course:

>> CHECK OUT THE COURSE
Persistence footer banner
Comments are closed on this article!