[ShareMore] 2. Entity ์ž‘์„ฑ

1

[ํ† ์ดํ”„๋กœ์ ํŠธ] Share More

๋ชฉ๋ก ๋ณด๊ธฐ
3/6
post-thumbnail

๐Ÿ”” ํ”„๋กœ์ ํŠธ๋ฅผ ์‹œ์ž‘ํ•˜๋ฉฐ Entity๋ฅผ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค!


์ง€๋‚œ ERD ์„ค๊ณ„์— ๋งž๊ฒŒ Entity๋ฅผ ๊ตฌ์„ฑํ•ด ์ค๋‹ˆ๋‹ค!

๐Ÿ“„ Users.java

์œ ์ €

@Getter
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Entity
@Builder
public class Users {

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

    private String name;

    private String email;

    private String password;

    private String address;
}

๐Ÿ“„ Item.java

์•„์ดํ…œ

@Entity
@Builder
@Getter
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Item {

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

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "owner_id")
    private Users users;

    private String name;

    private String description;

    private String category;

    private int price;

}

๐Ÿ“„ Reservation.java

์˜ˆ์•ฝ

@Entity
@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Reservation {

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

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id")
    private Users userId;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "item_id")
    private Users itemId;

    private LocalDateTime startDate;

    private LocalDateTime endDate;

}

๐Ÿ“„ Review.java

๋ฆฌ๋ทฐ

@Entity
@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Review {

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

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id")
    private Users userId;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "item_id")
    private Item itemId;

    private String content;

    private int rating;
}

BaseEntity

BaseEntity

@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
@Getter
public abstract class BaseEntity {

    @CreatedDate
    private LocalDateTime createdAt;

    @LastModifiedDate
    private LocalDateTime updatedAt;
}


์ด๊ฒƒ ์ฒ˜๋Ÿผ ๋ชจ๋“  ์—”ํ‹ฐํ‹ฐ์— BaseEntity๋ฅผ ์ƒ์†๋ฐ›๊ฒŒ ํ•ด์ค๋‹ˆ๋‹ค.

EnableJpaAuditing
springboot์—์„œ๋„ ์ž๋™์œผ๋กœ ๊ฐ’์„ ๋ฐ”๊ฟ”์ฃผ๊ธฐ ์œ„ํ•ด ์–ด๋…ธํ…Œ์ด์…˜์„ ๋‹ฌ์•„์ค๋‹ˆ๋‹ค


ํ›„ ์‹คํ–‰ํ•˜๋ฉด

์ฟผ๋ฆฌ๊ฐ€ ์ฝ˜์†”์— ์ฐํž™๋‹ˆ๋‹ค!

ํ…Œ์ด๋ธ” ์ƒ์„ฑ

๊ทธ๋ฆฌ๊ณ  h2 ์ฝ˜์†”์„ ํ™•์ธํ•ด์ฃผ๋ฉด

ํ…Œ์ด๋ธ”๋“ค์ด ์ž˜ ์ƒ์„ฑ๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!


์ด์ œ ์„ธ๋ถ€์„ค์ • & ์–‘๋ฐฉํ–ฅ ๋งคํ•‘์„ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

@Entity
@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Reservation extends BaseEntity {

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

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id",nullable = false)
    private Users user;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "item_id",nullable = false)
    private Item item;

    @Column(nullable = false)
    private LocalDateTime startDate;

    @Column(nullable = false)
    private LocalDateTime endDate;

}

@Entity
@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Review extends BaseEntity {

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

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id", nullable = false)
    private Users user;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "item_id", nullable = false)
    private Item item;

    @Column(nullable = true)
    private String comment;

    @Column(nullable = false)
    private int rating;
}

@Entity
@Builder
@Getter
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Item extends BaseEntity {

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

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "owner_id", nullable = false)
    private Users user;

    @Column(nullable = false)
    private String name;

    @Column(nullable = true)
    private String description;

    @Column(nullable = false)
    private String category;

    @Column(nullable = false)
    private int price;

    @OneToMany(mappedBy = "item", cascade = CascadeType.ALL)
    private List<Review> reviewList = new ArrayList<>();

    @OneToMany(mappedBy = "item", cascade = CascadeType.ALL)
    private List<Reservation> reservationList = new ArrayList<>();
}

@Getter
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Entity
@Builder
public class Users extends BaseEntity {

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

    @Column(nullable = false)
    private String name;

    @Column(nullable = false)
    private String email;

    @Column(nullable = false)
    private String password;

    @Column(nullable = false)
    private String address;

    @OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
    private List<Item> itemList = new ArrayList<>();

    @OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
    private List<Review> reviewList = new ArrayList<>();

    @OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
    private List<Reservation> reservationList = new ArrayList<>();
}
profile
์ฝ”๋”ฉ์€์žฌ๋ฐŒ์–ด

0๊ฐœ์˜ ๋Œ“๊ธ€