Bank Application

[Bank Application] 1-4. 테이블 별 엔티티 생성

겨르 2024. 9. 26. 13:45

domain 하위에 account, user, transaction 폴더를 만들고

각각의 엔티티를 생성해 주었다.

  1. USER
    @NoArgsConstructor //스프링이 User 객체생성할때 빈 생성자로 new 하기 때문
    @EntityListeners(AuditingEntityListener.class)
    @Table(name = "user_tb")
    @Entity
    public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Column(unique = true, nullable = false, length = 20)
    private String username;
    @Column(nullable = false, length = 60) //패스워드 인코딩(Bcrypt)
    private String password;
    @Column(nullable = false, length = 20)
    private String email;
    @Column(nullable = false, length = 20)
    private String fullname;
    @Enumerated(EnumType.STRING)
    @Column(nullable = false)
    private UserEnum role; //ADMIN, CUSTOMER
    @CreatedDate
    @Column(nullable = false)
    private LocalDateTime createdAt;
    @LastModifiedDate
    @Column(nullable = false)
    private LocalDateTime updatedAt;
     this.id = id;
     this.username = username;
     this.password = password;
     this.email = email;
     this.fullname = fullname;
     this.role = role;
     this.createdAt = createdAt;
     this.updatedAt = updatedAt;
    }
    }
  2. @Builder
    public User(Long id, String username, String password, String email, String fullname, UserEnum role, LocalDateTime createdAt, LocalDateTime updatedAt) {
  3. ENUMCLASS 생성
    @Column(nullable = false)
    private UserEnum role; //ADMIN, CUSTOMER

@Getter
@AllArgsConstructor
public enum UserEnum {
ADMIN("관리자"), CUSTOMER("고객");

private String value;

}

  1. Account 생성
    @NoArgsConstructor
    @EntityListeners(AuditingEntityListener.class)
    @Table(name = "account_tb")
    @Entity
    public class Account {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Column(unique = true, nullable = false, length = 4)
    private Long number;
    @Column(nullable = false, length = 4)
    private Long password;
    @Column(nullable = false)
    private Long balance;@CreatedDate
    @Column(nullable = false)
    private LocalDateTime createdAt;
    @LastModifiedDate
    @Column(nullable = false)
    private LocalDateTime updatedAt;
     this.id = id;
     this.number = number;
     this.password = password;
     this.balance = balance;
     this.user = user;
     this.createdAt = createdAt;
     this.updatedAt = updatedAt;
    }
    }
  2. @Builder
    public Account(Long id, Long number, Long password, Long balance, User user, LocalDateTime createdAt, LocalDateTime updatedAt) {
  3. @ManyToOne(fetch = FetchType.LAZY) //account.getUser().아무필드() === 호출됨
    private User user;
  1. Transaction 생성
    @NoArgsConstructor
    @EntityListeners(AuditingEntityListener.class)
    @Table(name = "transactioin_tb")
    @Entity
    public class Transaction {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;@JoinColumn(foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
    @ManyToOne(fetch = FetchType.LAZY)
    private Account depositAccount;@Enumerated(EnumType.STRING)
    @Column(nullable = false)
    private TransactionEnum gubun; //WITHDRAW, DEPOSIT, TRANSFER, ALL@CreatedDate
    @Column(nullable = false)
    private LocalDateTime createdAt;
    @LastModifiedDate
    @Column(nullable = false)
    private LocalDateTime updatedAt;
     this.id = id;
     this.withdrawAccount = withdrawAccount;
     this.depositAccount = depositAccount;
     this.amount = amount;
     this.withdrawAccountBalance = withdrawAccountBalance;
     this.depositAccountBalance = depositAccountBalance;
     this.gubun = gubun;
     this.sender = sender;
     this.receiver = receiver;
     this.tel = tel;
     this.createdAt = createdAt;
     this.updatedAt = updatedAt;
    }
    }
  2. public Transaction(Long id, Account withdrawAccount, Account depositAccount, Long amount, Long withdrawAccountBalance, Long depositAccountBalance, TransactionEnum gubun, String sender, String receiver, String tel, LocalDateTime createdAt, LocalDateTime updatedAt) {
  3. //계좌가 사라져도 로그는 남아야 함
    private String sender;
    private String receiver;
    private String tel;
  4. private Long amount;
    private Long withdrawAccountBalance;
    private Long depositAccountBalance;
  5. @JoinColumn(foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
    @ManyToOne(fetch = FetchType.LAZY)
    private Account withdrawAccount;

여기에도 있는 ENUM
@AllArgsConstructor
@Getter
public enum TransactionEnum {
WITHDRAW("출금"), DEPOSIT("입금"), TRANSFER("이체"), ALL("입출금내역");

private String value;

}