Hibernate Multi-Table Join Query Annotations: Understanding Foreign Key Column Mapping

This discussion focuses on Hibernate annotation-based multi-table join queries, particularly addressing parameter configuration when primary and foreign key field names differ between tables.

Development Environment and Scope

  • Annotation-based Hibernate multi-table join implementations
  • Using IntelliJ IDEA development environment
  • Covers scenarios where primary and foreign key field names are inconsistent

Problem Scenario

Consider two tables: a user table (usernote) and a note table (note). When attempting to establish a join relationship, issues arise when field names don't match between tables.

Incorrect Implementation Example

Here's how the entities were initially configured:

@Entity @Table(name = "note") @Data public class NoteEntity implements Serializable {

@Id
@Column(name = "id")
@GeneratedValue(generator = "identity")
private Integer noteId;

@Column(name = "context")
private String content;

@Column(name = "publish_time")
private Date publicationDate;

@Column(name = "user_id")
private Integer userIdRef;

@Column(name = "like_count")
private Integer likesCount;

@ManyToOne(optional = false)
@JoinColumn(name = "user_id",
    insertable = false, updatable = false, referencedColumnName = "userId")
private UserEntity userEntity;

}


</div>Corresponding user entity:

<div class="code-block">```

@Entity
@Table(name = "usernote")
@Data
public class UserEntity {
    
    @Id
    @Column(name = "userId")
    @GeneratedValue(generator = "identity")
    private Integer userId;
    
    @Column(name = "username")
    private String userName;
    
    @Column(name = "address")
    private String userAddress;
    
    @Column(name = "phone")
    private String phoneNumber;

    @OneToMany
    @JoinColumn(name = "userId", referencedColumnName = "user_id") // Incorrect configuration
    private List<noteentity> userNotes;
}
</noteentity>

IntelliJ IDEA highlights errors in both locations. The deployment fails with the following exception:

Caused by: org.hibernate.MappingException: Unable to find column with logical name: user_id in org.hibernate.mapping.Table(usernote) and its related supertables and secondary tables

Solution

The fix involves correctly cnofiguring the @JoinColumn annotation. The name attribute should reference the actual foreign key column name in the database table.

Corrected configuration:

@OneToMany @JoinColumn(name = "user_id", referencedColumnName = "userId") private List userNotes;


</div>Understanding the name Attribute
--------------------------------

The `name` attribute in `@JoinColumn` specifies the foreign key column name. According to the JPA specification:

The foreign key column location depends on the relationship type and mapping strategy:

- For OneToOne or ManyToOne with foreign key strategy: column exists in source entity table
- For unidirectional OneToMany with foreign key strategy: column exists in target entity table
- For ManyToMany or bidirectional relationships with join table: foreign keys exist in join table

The default naming convention concatenates the referencing property name with the referenced primary key column name, separated by an underscore.

Tags: hibernate jpa annotations mapping foreign-key

Posted on Mon, 11 May 2026 07:36:25 +0000 by backinblack