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:


1. Overview

The annotation javax.persistence.JoinColumn marks a column as a join column for an entity association or an element collection.

In this quick tutorial, we'll show some examples of basic @JoinColumn usage.

2. @OneToOne Mapping Example

The @JoinColumn annotation combined with a @OneToOne mapping indicates that a given column in the owner entity refers to a primary key in the reference entity:

public class Office {
    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "addressId")
    private Address address;

The above code example will create a foreign key linking the Office entity with the primary key from the Address entity. The name of the foreign key column in the Office entity is specified by name property.

3. @OneToMany Mapping Example

When using a @OneToMany mapping, we can use the mappedBy parameter to indicate that the given column is owned by another entity:

public class Employee {
    private Long id;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "employee")
    private List<Email> emails;

public class Email {
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "employee_id")
    private Employee employee;

In the above example, Email (the owner entity) has a join column employee_id that stores the id value and has a foreign key to the Employee entity.


4. @JoinColumns

In situations when we want to create multiple join columns, we can use the @JoinColumns annotation:

public class Office {
    @ManyToOne(fetch = FetchType.LAZY)
        @JoinColumn(name="ADDR_ID", referencedColumnName="ID"),
        @JoinColumn(name="ADDR_ZIP", referencedColumnName="ZIP")
    private Address address;

The above example will create two foreign keys pointing to ID and ZIP columns in the Address entity:


5. Conclusion

In this article, we learned how to use the @JoinColumn annotation. We looked at how to create both a single entity association and an element collection.

As always, all source code is available over on GitHub.

Persistence bottom

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

Persistence footer banner
Comments are closed on this article!