1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

springでのモデル定義例 ~外部キー設定が必要な社員・役職・部署テーブル~

Posted at

対象読者

  • springのエンティティ定義の仕方を知りたい人
  • 複数テーブル間の外部キーによる紐づけ方法を知りたい人

前提

springだとモデルパッケージを用意しその中に複数のエンティティ定義をします
今回のその例の一つとして以下のような構成です
※重要なのは赤枠の部分だけです

image.png

まずは全エンティティの外観をつかむ

社員テーブルエンティティ

Employee.java
package com.example.employee.model;

import java.time.LocalDateTime;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;

@Entity
@Table(name = "employees")
public class Employee {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;

    @Column(name = "birth_place")
    private String birthPlace;
    
    @ManyToOne
    @JoinColumn(name = "department_id", referencedColumnName = "id")
    private Department department;
    
    @ManyToOne
    @JoinColumn(name = "position_id", referencedColumnName = "id")
    private Position position;

    @Column(name = "create_date")
    private LocalDateTime createDate;

    @Column(name = "update_date")
    private LocalDateTime updateDate;

    public Long getId() { return id; }
    public void setId(Long id) { this.id = id; }
    
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    
    public String getBirthPlace() { return birthPlace; }
    public void setBirthPlace(String birthPlace) { this.birthPlace = birthPlace; } 
    
    public Department getDepartment() { return department; }
    public void setDepartment(Department department) { this.department = department; }

    public Position getPosition() { return position; }
    public void setPosition(Position position) { this.position = position; }
    
    public LocalDateTime getCreateDate() { return createDate; }
    public void setCreateDate(LocalDateTime createDate) { this.createDate = createDate; }
    
    public LocalDateTime getUpdateDate() { return updateDate; }
    public void setUpdateDate(LocalDateTime updateDate) { this.updateDate = updateDate; }
}
Position.java
package com.example.employee.model;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;

@Entity
@Table(name = "positions")
public class Position {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "position_name")
    private String positionName;

    // Getter/Setter
    public Long getId() { return id; }
    public void setId(Long id) { this.id = id; }

    public String getPositionName() { return positionName; }
    public void setPositionName(String positionName) { this.positionName = positionName; }
}
Department.java
package com.example.employee.model;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;

@Entity
@Table(name = "departments")
public class Department {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "department_name")
    private String departmentName;

    // Getter/Setter
    public Long getId() { return id; }
    public void setId(Long id) { this.id = id; }

    public String getDepartmentName() { return departmentName; }
    public void setDepartmentName(String departmentName) { this.departmentName = departmentName; }
}

ポイント解説

@ManyToOne でエンティティ間の関係を定義する

@ManyToOne は、現在のエンティティが 相手エンティティ1件に紐づく ことを示すアノテーション
今回の例では、社員は1つの部署・1つの役職に属するため、以下のように多対一の関係を定義します

@ManyToOne
private Department department;

@ManyToOne
private Position position;

これにより、
Employee → Department
Employee → Position
という「多対一」の関係性がモデル上で表現されます


@JoinColumn で外部キーと参照先を明示する

@JoinColumn は、実際のテーブルで
どのカラムを外部キーとして使用するか
(外部キーとして宣言すると同時にテーブルにもname="この部分"のこの部分のところで設定した名前のカラムが用意される)

相手のどのカラムを参照するか
を指定するためのアノテーションです

@JoinColumn(name = "department_id", referencedColumnName = "id")
private Department department;

name

  • このエンティティ側(employees)の 外部キーのカラム名
  • 例:department_id

referencedColumnName

  • 相手側(departments)の 参照対象となるカラム名
  • 例:id(通常はPrimary Key)

これにより、
employees.department_id → departments.id
という明確な外部キー制約が定義され、
Spring/JPA が適切に結合関係を扱えるようになる。


以上外部キー設定が必要なモデル定義例でした

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?