@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;
}
@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;
}
@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;
}
@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;
}
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
@Getter
public abstract class BaseEntity {
@CreatedDate
private LocalDateTime createdAt;
@LastModifiedDate
private LocalDateTime updatedAt;
}
์ด๊ฒ ์ฒ๋ผ ๋ชจ๋ ์ํฐํฐ์ BaseEntity๋ฅผ ์์๋ฐ๊ฒ ํด์ค๋๋ค.
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<>();
}