λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
SpringπŸ€/κ°„λ‹¨ν•œ κ²Œμ‹œνŒ λ§Œλ“€κΈ°

[JPA] κ°„λ‹¨ν•œ κ²Œμ‹œνŒ λ§Œλ“€κΈ° - user (UserEntity μ—”ν‹°ν‹°)

by @ENFJ 2023. 2. 11.

 

λ¨Όμ € user νŒ¨ν‚€μ§€μ˜ entity λΆ€ν„° μ„€λͺ…ν•˜κ² μŠ΅λ‹ˆλ‹€.πŸ˜€

 

κ°€μž₯ λ¨Όμ € νšŒμ› 엔티티에 뭐가 λ“€μ–΄κ°€μ•Ό 될지 생각을 ν–ˆμŠ΅λ‹ˆλ‹€. ! (ν‰μ†Œ 자주 μ‚¬μš©ν•˜λ˜ '넀이버' 포털 μ‚¬μ΄νŠΈ λ‚˜ '카카였'λ“±,, 둜그인, νšŒμ› κ°€μž…μ‹œ ν•„μš”ν–ˆλ˜ 정보듀을 λ– μ˜¬λ ΈμŒ!)

 

1. 식별 μ½”λ“œ,

2. 이메일,

3. λΉ„λ°€λ²ˆν˜Έ,

4. νšŒμ›μ΄λ¦„,

5. κ°€μž…λ‚ μ§œ

 

κ°„λ‹¨ν•˜κ²Œ νšŒμ› κ°€μž…, 둜그인 , κ²Œμ‹œνŒμ„ λ§Œλ“€ κ±°λΌμ„œ μ—”ν‹°ν‹° λ˜ν•œ μ΅œλŒ€ν•œ μ‹¬ν”Œν•˜κ²Œ μ„€μ •ν•˜μ˜€μŠ΅λ‹ˆλ‹€.  

 

 

 

UserEntity 전체 μ½”λ“œ 

package com.example.projectpicker.user.entity;


import com.example.projectpicker.post.entity.PostEntity;
import lombok.*;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Setter @Getter @ToString(exclude = "PostId")
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(of = "id") // id 만 비ꡐ해라. ꡳ이 λΉ„λ²ˆ,μ΄λ¦„κΉŒμ§€ 비ꡐ할 ν•„μš”x
@Builder
@Entity
@Table(name = "tbl_user")
public class UserEntity {
    @Id
    @Column(name = "user_id")
    @GeneratedValue(generator = "system-uuid")
    @GenericGenerator(name = "system-uuid", strategy = "uuid") // idκ°€ μ€‘λ³΅λ˜μ§€ μ•Šλ„λ‘
    private String userId; // 식별 μ½”λ“œ

    @Column(name = "user_email",unique = true, nullable = false) // unique=true: 쀑볡x / nullable=false : ν•„μˆ˜ κ°’
    private String userEmail; // 이메일

    @Column(name = "user_password",nullable = false)
    private String userPassword; // λΉ„λ°€λ²ˆν˜Έ

    @Column(name ="user_name",nullable = false)
    private String userName; // νšŒμ› 이름

    @CreationTimestamp
    private LocalDateTime joinDate; // κ°€μž… λ‚ μ§œ


    /**
     * κ²Œμ‹œνŒ κ³Ό κ΄€κ³„ν˜• 맀핑
     */
    @OneToMany(mappedBy = "userEntity")
    private List<PostEntity> PostId = new ArrayList<>(); //ν•΄μ‹œνƒœκ·Έ λͺ©λ‘


}

 

 

전체 μ½”λ“œλ₯Ό μ‚΄νŽ΄λ³΄λ©΄ @(μ–΄λ…Έν…Œμ΄μ…˜)  섀정을 ν•©λ‹ˆλ‹€.

 

μ–΄λ…Έν…Œμ΄μ…˜μ— λŒ€ν•΄ μ„€λͺ…ν•˜κΈ°μ „! ν”„λ‘œμ νŠΈ 초기 μ„€μ •ν• λ•Œ, dependencies μ—μ„œ lombok(둬볡)을 μΆ”κ°€ν–ˆμ—ˆλ‹€.

Lombok이 이런 @(μ–΄λ…Έν…Œμ΄μ…˜)을 μ œκ³΅ν•˜λŠ” 라이브러리 이닀.

 

κ·Έ 쀑 λͺ‡κ°œμ˜ μ–΄λ…Έν…Œμ΄μ…˜μ— λŒ€ν•΄ μ„€λͺ…ν•˜μžλ©΄

@Getter , @Setter κ°€ μžˆλŠ”λ°, μ΄λŠ” 각각 μ ‘κ·Όμžμ™€ μ„€μ •μž λ©”μ„œλ“œλ₯Ό μž‘μ„±ν•΄μ£ΌλŠ” Lombok μ–΄λ…Έν…Œμ΄μ…˜μ΄λ‹€.

이 μ–΄λ…Έν…Œμ΄μ…˜μ€ 정말 자주 μ‚¬μš©λ˜λ©° νŽΈλ¦¬ν•˜λ‹€.

 

μ΄λ ‡κ²Œ μ„€λͺ…ν•˜λ©΄ 이해가 잘 μ•ˆλ μˆ˜λ„ μžˆμ„κ±° 같은데

@Getter λŠ” λ­”κ°€ 'κΊΌλ‚΄λŠ” 녀석' 이고,

@Setter λŠ” 'μ„€μ •ν•΄μ£ΌλŠ” 녀석' 이라고 μƒκ°ν•˜λ©΄ μ΄ν•΄ν•˜κΈ° 쉽닀. (개인적.으둜?😁)

 

@NorgsConstructor : νŒŒλΌλ―Έν„°κ°€ μ—†λŠ” κΈ°λ³Έ μƒμ„±μž 생성

@AllArgsConstructor : λͺ¨λ“  ν•„λ“œ 값을 νŒŒλΌλ―Έν„°λ‘œ λ°›λŠ” μƒμ„±μžλ₯Ό λ§Œλ“€μ–΄ μ€€λ‹€.

@RequiredArgsConstructor : finalμ΄λ‚˜ @NonNull 인 ν•„λ“œ κ°’λ§Œ νŒŒλΌλ―Έν„°λ‘œ λ°›λŠ” μƒμ„±μžλ₯Ό λ§Œλ“€μ–΄ μ€€λ‹€.

@ToString : toString()λ©”μ„œλ“œλ₯Ό μžλ™ 생성

@EqualsAndHashCode : equals, hashCode μžλ™ 생성

  • equals : 두 객체의 λ‚΄μš©μ΄ 같은지, 동등성(equality) λ₯Ό λΉ„κ΅ν•˜λŠ” μ—°μ‚°μž
  • hashCode : 두 객체가 같은 객체인지, 동일성(identity) λ₯Ό λΉ„κ΅ν•˜λŠ” μ—°μ‚°μž

 

++

 DB와 μ§μ ‘μ μœΌλ‘œ λ§€ν•‘λ˜μ–΄μžˆλŠ” Entity