4
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?

More than 3 years have passed since last update.

[Java] JPAで1対Nで中間テーブルのあるDBをModelに落とし込む

Last updated at Posted at 2021-08-11

Table構成

usersテーブルとrolesテーブルが1対Nの関係であり、中間テーブルがある構成。
ER.png

users

                                  Table "public.users"
  Column  |          Type          |                        Modifiers
----------+------------------------+----------------------------------------------------------
 id       | integer                | not null default nextval('users_id_seq'::regclass)
 name     | character varying(255) | not null

Indexes:
    "users_pkey" PRIMARY KEY, btree (id)

mappings

                             Table "public.mappings"
    Column    |   Type   |                          Modifiers
--------------+----------+-------------------------------------------------------------
 id           | integer  | not null default nextval('mappings_id_seq'::regclass)
 user_id      | integer  | not null
 role_id      | integer  | not null

Indexes:
    "mappings_pkey" PRIMARY KEY, btree (id)
    "mappings_user_id_role_id_key" UNIQUE CONSTRAINT, btree (user_id, role_id)

roles

                                    Table "public.roles"
    Column    |          Type          |                         Modifiers
--------------+------------------------+------------------------------------------------------------
 id           | integer                | not null default nextval('roles_id_seq'::regclass)
 name         | character varying(255) | not null

Indexes:
    "roles_pkey" PRIMARY KEY, btree (id)

Model作成

UserModel

UserModel
@Getter
@Entity
@Table(name="users")
public class UserModel {
    @Id
    private Long id;
    private String name;

    @OneToMany(fetch = FetchType.EAGER)
    @JoinTable(
        // 中間テーブル名
        name="mappings",
        joinColumns = {
           /*
            * mappings -> usersの関係を定義
            * name=mappingsテーブルのカラム名
            * referencedColumnName=usersテーブルのカラム名
            */
            @JoinColumn(name="user_id", referencedColumnName="id")
        },
        inverseJoinColumns = {
           /*
            * mappings -> rolesの関係を定義
            * name=mappingsテーブルのカラム名
            * referencedColumnName=rolesテーブルのカラム名
            */
            @JoinColumn(name="role_id", referencedColumnName="id")
        }
    )
    private List<RoleModel> roles;

RoleModel

RoleModel
@Getter
@Entity
@Table(name="role")
public class RoleModel {
    @Id
    private Long id;
    private String name;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinTable(
        // 中間テーブル名
        name="mappings",
        joinColumns = {
           /*
            * mappings -> rolesの関係を定義
            * name=mappingsテーブルのカラム名
            * referencedColumnName=roleテーブルのカラム名
            */
            @JoinColumn(name ="role_id", referencedColumnName ="id")
        },
        inverseJoinColumns = {
           /*
            * mappings -> usersの関係を定義
            * name=mappingsテーブルのカラム名
            * referencedColumnName=usersテーブルのカラム名
            */
            @JoinColumn(name ="user_id", referencedColumnName ="id")
        }
    )
    private List<UserModel> user;

Usage

public interface UserRepository extends JpaRepository<UserModel, Long> {
}

public interface RoleRepository extends JpaRepository<RoleModel, Long> {
}

public class Sample {
    private UserRepository userRepository;
    private RoleRepository roleRepository;

    public void users() {
        List<UserModel> users = userRepository.findAll();
        // usersのレコードに関連付くrolesテーブルのnameカラムの1つ目を取得
        System.out.println(users.get(0).getRoles().get(0).getName());
        // usersのレコードに関連付くrolesテーブルのnameカラムの2つ目を取得
        System.out.println(users.get(0).getRoles().get(1).getName());
    }

    public void roles() {
        List<RoleModel> role = roleRepository.findAll();
        // rolesのレコードに関連付くusersテーブルのnameカラムの1つ目を取得
        System.out.println(roles.get(0).getUsers().get(0).getName());
        // rolesのレコードに関連付くusersテーブルのnameカラムの2つ目を取得
        System.out.println(roles.get(0).getUsers().get(1).getName());
    }
}

参考

4
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
4
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?