From 178690c7b94657e176f6fd94ec709af9ccd3d9f0 Mon Sep 17 00:00:00 2001 From: s-choi Date: Thu, 10 Sep 2026 15:47:37 +0900 Subject: [PATCH] =?UTF-8?q?[STYLE]=20entity=E3=81=AE=E7=84=A1=E9=A7=84?= =?UTF-8?q?=E3=81=AA=E6=94=B9=E8=A1=8C=E3=82=84=E3=82=B9=E3=83=9A=E3=83=BC?= =?UTF-8?q?=E3=82=B9=E3=81=AA=E3=81=A9=E3=81=AE=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/infra/entity/DepartmentEntity.java | 81 +++---- .../entity/EmployeeDepartmentEntity.java | 144 +++++------- .../app/infra/entity/EmployeeEntity.java | 208 ++++++------------ 3 files changed, 151 insertions(+), 282 deletions(-) diff --git a/src/main/java/com/example/rhizome/app/infra/entity/DepartmentEntity.java b/src/main/java/com/example/rhizome/app/infra/entity/DepartmentEntity.java index fc043e1..ec183f2 100644 --- a/src/main/java/com/example/rhizome/app/infra/entity/DepartmentEntity.java +++ b/src/main/java/com/example/rhizome/app/infra/entity/DepartmentEntity.java @@ -1,25 +1,20 @@ -/* - * Department.java - * 作成 IT-College 2016 - *------------------------------------------------------------ - * Copyright(c) Rhizome Inc. All Rights Reserved. - */ package com.example.rhizome.app.infra.entity; +import java.util.List; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; import com.example.rhizome.app.domain.model.Department; + import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; -import javax.persistence.*; -import java.util.List; - -/** - * Department テーブルの Entity - * - * @author IT-College - */ @Entity @Table(name = "department") @Data @@ -27,46 +22,26 @@ import java.util.List; @AllArgsConstructor public class DepartmentEntity { - /** - * 部署ID - */ - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - @Column(name = "id_department", nullable = false) - private Integer idDepartment; - /** - * 部署名 - */ - @Column(name = "nm_department", nullable = false) - private String nmDepartment; + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "id_department", nullable = false) + private Integer idDepartment; + @Column(name = "nm_department", nullable = false) + private String nmDepartment; - /** - * DepartmentEntity(DataAccess層依存)から Department(BusinessLogic層依存型)に変換します。 - * - * @param departmentEntityList department Table のリスト - * @return List リスト - */ - public static List createDepartmentList(List departmentEntityList) { + public static List createDepartmentList(List departmentEntityList) { - // Stream API を利用するとこうなります。 - return departmentEntityList - .stream() - .map(departmentEntity -> new Department( - departmentEntity.getIdDepartment(), - departmentEntity.getNmDepartment() - )).toList(); - } + return departmentEntityList + .stream() + .map(departmentEntity -> new Department( + departmentEntity.getIdDepartment(), + departmentEntity.getNmDepartment())) + .toList(); + } - /** - * Department(BusinessLogic層依存型) から DepartmentEntity(DataAccess層依存)に変換します。 - * - * @param department department モデルの値 - * @return DepartmentEntity に変換したインスタンス - */ - public static DepartmentEntity convertFrom(Department department) { - return new DepartmentEntity( - department.getIdDepartment(), - department.getNmDepartment() - ); - } + public static DepartmentEntity convertFrom(Department department) { + return new DepartmentEntity( + department.getIdDepartment(), + department.getNmDepartment()); + } } diff --git a/src/main/java/com/example/rhizome/app/infra/entity/EmployeeDepartmentEntity.java b/src/main/java/com/example/rhizome/app/infra/entity/EmployeeDepartmentEntity.java index 989d2cc..423e702 100644 --- a/src/main/java/com/example/rhizome/app/infra/entity/EmployeeDepartmentEntity.java +++ b/src/main/java/com/example/rhizome/app/infra/entity/EmployeeDepartmentEntity.java @@ -1,118 +1,86 @@ package com.example.rhizome.app.infra.entity; +import java.util.List; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.NamedEntityGraph; +import javax.persistence.Table; + +import org.hibernate.annotations.Fetch; +import org.hibernate.annotations.FetchMode; + import com.example.rhizome.app.domain.model.Department; import com.example.rhizome.app.domain.model.Employee; import com.example.rhizome.app.domain.model.EmployeeInfo; + import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; -import org.hibernate.annotations.Fetch; -import org.hibernate.annotations.FetchMode; -import javax.persistence.*; -import java.util.List; - -/** - * Employee + Department の Join 情報を保持するEntity - * - * @author IT-College - */ @Entity @Table(name = "employee") @Data @AllArgsConstructor @NoArgsConstructor -@NamedEntityGraph( - name = "employee_with_department", - includeAllAttributes = true -) +@NamedEntityGraph(name = "employee_with_department", includeAllAttributes = true) public class EmployeeDepartmentEntity { - /** - * 社員ID - */ - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - @Column(name = "id_employee", nullable = false) - private Integer idEmployee; + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "id_employee", nullable = false) + private Integer idEmployee; - /** - * 社員名 - */ - @Column(name = "nm_employee", nullable = false) - private String nmEmployee; + @Column(name = "nm_employee", nullable = false) + private String nmEmployee; - /** - * 社員名カナ - */ - @Column(name = "kn_employee", nullable = false) - private String knEmployee; + @Column(name = "kn_employee", nullable = false) + private String knEmployee; - /** - * メールアドレス - */ - @Column(name = "mail_address", nullable = false) - private String mailAddress; + @Column(name = "mail_address", nullable = false) + private String mailAddress; - /** - * パスワード - */ - @Column(name = "password", nullable = false) - private String password; + @Column(name = "password", nullable = false) + private String password; - /** - * 管理者権限 - */ - @Column(name = "admin_auth", nullable = false) - private Integer adminAuth; + @Column(name = "admin_auth", nullable = false) + private Integer adminAuth; - @ManyToOne - @JoinColumn(name = "id_department", referencedColumnName = "id_department", insertable = false, updatable = false) - @Fetch(FetchMode.JOIN) - private DepartmentEntity department; + @ManyToOne + @JoinColumn(name = "id_department", referencedColumnName = "id_department", insertable = false, updatable = false) + @Fetch(FetchMode.JOIN) + private DepartmentEntity department; - /** - * EmployeeDepartmentEntity(DataAccess層依存) を EmployeeInfo(BusinessLogic層依存) に変換します。 - * - * @param employeeDepartmentEntity  EmployeeDepartmentEntity のインスタンス - * @return EmployeeInfo のインスタンス - */ - public static EmployeeInfo createEmployeeInfo(EmployeeDepartmentEntity employeeDepartmentEntity) { + public static EmployeeInfo createEmployeeInfo(EmployeeDepartmentEntity employeeDepartmentEntity) { - // コンストラクタを使って書くとSetterを使用するより簡略化出来ます。 - // 混乱してしまう人は落ち着いて EmployeeInfo のフィールドを見に行って、 - // どうしてこの書き方が出来るのかを理解できる様になりましょう。 - return new EmployeeInfo( - new Employee( - employeeDepartmentEntity.getIdEmployee(), - employeeDepartmentEntity.getNmEmployee(), - employeeDepartmentEntity.getKnEmployee(), - employeeDepartmentEntity.getMailAddress(), - employeeDepartmentEntity.getPassword(), - employeeDepartmentEntity.getAdminAuth(), - employeeDepartmentEntity.getDepartment() == null ? null : employeeDepartmentEntity.getDepartment().getIdDepartment() - ), - new Department( - employeeDepartmentEntity.getDepartment() == null ? null : employeeDepartmentEntity.getDepartment().getIdDepartment(), - employeeDepartmentEntity.getDepartment() == null ? null : employeeDepartmentEntity.getDepartment().getNmDepartment() - ) - ); + return new EmployeeInfo( + new Employee( + employeeDepartmentEntity.getIdEmployee(), + employeeDepartmentEntity.getNmEmployee(), + employeeDepartmentEntity.getKnEmployee(), + employeeDepartmentEntity.getMailAddress(), + employeeDepartmentEntity.getPassword(), + employeeDepartmentEntity.getAdminAuth(), + employeeDepartmentEntity.getDepartment() == null ? null + : employeeDepartmentEntity.getDepartment().getIdDepartment()), + new Department( + employeeDepartmentEntity.getDepartment() == null ? null + : employeeDepartmentEntity.getDepartment().getIdDepartment(), + employeeDepartmentEntity.getDepartment() == null ? null + : employeeDepartmentEntity.getDepartment().getNmDepartment())); - } + } - /** - * EmployeeDepartmentEntity(DataAccess層依存) を EmployeeInfo(BusinessLogic層依存) に変換します。 - * (リスト版) - * - * @param employeeDepartmentEntityList EmployeeDepartmentEntity インスタンスのリスト - * @return EmployeeInfo インスタンスのリスト - */ - public static List createEmployeeInfoList(List employeeDepartmentEntityList) { + public static List createEmployeeInfoList( + List employeeDepartmentEntityList) { - // StreamAPI を使うとこうなります。 - return employeeDepartmentEntityList.stream().map(EmployeeDepartmentEntity::createEmployeeInfo).toList(); - - } + return employeeDepartmentEntityList.stream().map(EmployeeDepartmentEntity::createEmployeeInfo).toList(); + } } diff --git a/src/main/java/com/example/rhizome/app/infra/entity/EmployeeEntity.java b/src/main/java/com/example/rhizome/app/infra/entity/EmployeeEntity.java index 17721b5..263e9e8 100644 --- a/src/main/java/com/example/rhizome/app/infra/entity/EmployeeEntity.java +++ b/src/main/java/com/example/rhizome/app/infra/entity/EmployeeEntity.java @@ -1,27 +1,22 @@ -/* - * Employee.java - * 作成 IT-College 2016 - *------------------------------------------------------------ - * Copyright(c) Rhizome Inc. All Rights Reserved. - */ package com.example.rhizome.app.infra.entity; +import java.util.List; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; import com.example.rhizome.app.domain.model.Employee; import com.example.rhizome.app.domain.model.LoginUser; import com.example.rhizome.app.domain.model.SearchConditionEmployee; + import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; -import javax.persistence.*; -import java.util.List; - -/** - * EmployeeテーブルのEntity - * - * @author IT-College - */ @Entity @Table(name = "employee") @Data @@ -29,146 +24,77 @@ import java.util.List; @AllArgsConstructor public class EmployeeEntity { - /** - * 社員ID - */ - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - @Column(name = "id_employee", nullable = false) - private Integer idEmployee; + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "id_employee", nullable = false) + private Integer idEmployee; - /** - * 社員名 - */ - @Column(name = "nm_employee", nullable = false) - private String nmEmployee; + @Column(name = "nm_employee", nullable = false) + private String nmEmployee; - /** - * 社員名カナ - */ - @Column(name = "kn_employee", nullable = false) - private String knEmployee; + @Column(name = "kn_employee", nullable = false) + private String knEmployee; - /** - * メールアドレス - */ - @Column(name = "mail_address", nullable = false) - private String mailAddress; + @Column(name = "mail_address", nullable = false) + private String mailAddress; - /** - * パスワード - */ - @Column(name = "password", nullable = false) - private String password; + @Column(name = "password", nullable = false) + private String password; - /** - * 管理者権限 - */ - @Column(name = "admin_auth", nullable = false) - private Integer adminAuth; + @Column(name = "admin_auth", nullable = false) + private Integer adminAuth; - /** - * 部署ID - */ - @Column(name = "id_department", nullable = false) - private Integer idDepartment; + @Column(name = "id_department", nullable = false) + private Integer idDepartment; + public static EmployeeEntity convertFrom(Employee employee) { + return new EmployeeEntity( + employee.getIdEmployee(), + employee.getNmEmployee(), + employee.getKnEmployee(), + employee.getMailAddress(), + employee.getPassword(), + employee.getAdminAuth(), + employee.getIdDepartment()); + } - /** - * Employee(BusinessLogic 依存)を EmployeeEntity(DataAccess層依存)に変換します。 - * - * @param employee Employee Table の中身 - * @return EmployeeEntity のインスタンス(1レコード分) - */ - public static EmployeeEntity convertFrom(Employee employee) { - // 値を既に持ったインスタンスの生成に、コンストラクタを使用するとこうなります。 - return new EmployeeEntity( - employee.getIdEmployee(), - employee.getNmEmployee(), - employee.getKnEmployee(), - employee.getMailAddress(), - employee.getPassword(), - employee.getAdminAuth(), - employee.getIdDepartment() - ); - } + public static EmployeeEntity convertFrom(SearchConditionEmployee searchConditionEmployee) { + EmployeeEntity employeeEntity = new EmployeeEntity(); - /** - * SearchConditionEmployee(BusinessLogic 依存)を EmployeeEntity(DataAccess層依存)に変換します。 - * (コンストラクタを使用せず、Setterを利用したパターン) - * - * @param searchConditionEmployee 検索条件 - * @return EmployeeEntity のインスタンス(1レコード分) - */ - public static EmployeeEntity convertFrom(SearchConditionEmployee searchConditionEmployee) { - EmployeeEntity employeeEntity = new EmployeeEntity(); + employeeEntity.setIdEmployee(searchConditionEmployee.getIdEmployee()); + employeeEntity.setNmEmployee(searchConditionEmployee.getNmEmployee()); + employeeEntity.setKnEmployee(searchConditionEmployee.getKnEmployee()); + employeeEntity.setMailAddress(searchConditionEmployee.getMailAddress()); + employeeEntity.setIdDepartment(searchConditionEmployee.getIdDepartment()); - employeeEntity.setIdEmployee(searchConditionEmployee.getIdEmployee()); - employeeEntity.setNmEmployee(searchConditionEmployee.getNmEmployee()); - employeeEntity.setKnEmployee(searchConditionEmployee.getKnEmployee()); - employeeEntity.setMailAddress(searchConditionEmployee.getMailAddress()); - employeeEntity.setIdDepartment(searchConditionEmployee.getIdDepartment()); + return employeeEntity; + } - return employeeEntity; + public static Employee createEmployee(EmployeeEntity employeeEntity) { + return new Employee( + employeeEntity.getIdEmployee(), + employeeEntity.getNmEmployee(), + employeeEntity.getKnEmployee(), + employeeEntity.getMailAddress(), + employeeEntity.getPassword(), + employeeEntity.getAdminAuth(), + employeeEntity.getIdDepartment()); + } - } + public static List createEmployeeList(List employeeEntityList) { + return employeeEntityList.stream().map(EmployeeEntity::createEmployee).toList(); + } - /** - * EmployeeEntity(DataAccess層依存)から Employee (BusinessLogic層依存型)に変換します。 - * - * @param employeeEntity Employee Table のインスタンス - * @return employee インスタンス - */ - public static Employee createEmployee(EmployeeEntity employeeEntity) { - // 値を既に持ったインスタンスの生成に、コンストラクタを使用するとこうなります。 - return new Employee( - employeeEntity.getIdEmployee(), - employeeEntity.getNmEmployee(), - employeeEntity.getKnEmployee(), - employeeEntity.getMailAddress(), - employeeEntity.getPassword(), - employeeEntity.getAdminAuth(), - employeeEntity.getIdDepartment() - ); - } - - /** - * EmployeeEntity(DataAccess層依存) から Employee (BusinessLogic層依存型)に変換します。(リスト版) - * - * @param employeeEntityList EmployeeEntity インスタンスのリスト - * @return Employee インスタンスのリスト - */ - public static List createEmployeeList(List employeeEntityList) { - /* 従来の書き方 - List employeeList = new ArrayList<>(); - for(EmployeeEntity employeeEntity:employeeEntityList){ - Employee employee = EmployeeEntity.createEmployee(employeeEntity); - employeeList.add(employee); - } - return employeeList; - */ - - // Stream APIを使用すると1行で書けます。 - return employeeEntityList.stream().map(EmployeeEntity::createEmployee).toList(); - } - - /** - * EmployeeEntity(DataAccess層依存) から LoginUser (BusinessLogic層依存型)情報を作成します。 - * - * @return loginUser (ログイン者の情報) インスタンス - */ - public LoginUser createLoginUser() { - // 値を既に持ったインスタンスの生成に、コンストラクタを使用するとこうなります。 - return new LoginUser( - getIdEmployee(), - getNmEmployee(), - getKnEmployee(), - getMailAddress(), - getPassword(), - getAdminAuth(), - getIdDepartment() - ); - } + public LoginUser createLoginUser() { + return new LoginUser( + getIdEmployee(), + getNmEmployee(), + getKnEmployee(), + getMailAddress(), + getPassword(), + getAdminAuth(), + getIdDepartment()); + } }