Skip to content

Commit

Permalink
Merge pull request #17 from djkeh/feature/#13-jpa
Browse files Browse the repository at this point in the history
Spring Data JPA 와 테크닉
  • Loading branch information
djkeh authored Sep 26, 2021
2 parents af392e4 + fb6c11e commit 3bb32c4
Show file tree
Hide file tree
Showing 20 changed files with 251 additions and 83 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
runtimeOnly 'com.h2database:h2'
runtimeOnly 'mysql:mysql-connector-java'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/com/uno/getinline/controller/AdminController.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,17 @@ public ModelAndView adminEventDetail(@PathVariable Long eventId) {
Map<String, Object> map = new HashMap<>();
map.put("event", EventDto.of(
eventId,
1L,
PlaceDto.of(
1L,
PlaceType.SPORTS,
"배드민턴장",
"서울시 그리구 그래동",
"010-2222-3333",
33,
null,
LocalDateTime.now(),
LocalDateTime.now()
),
"오후 운동",
EventStatus.OPENED,
LocalDateTime.of(2021, 1, 1, 13, 0, 0),
Expand Down
27 changes: 25 additions & 2 deletions src/main/java/com/uno/getinline/domain/Admin.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package com.uno.getinline.domain;

import lombok.*;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.LinkedHashSet;
import java.util.Objects;
import java.util.Set;

@Getter
@ToString
@EqualsAndHashCode
@Table(indexes = {
@Index(columnList = "phoneNumber"),
@Index(columnList = "createdAt"),
Expand Down Expand Up @@ -46,6 +50,12 @@ public class Admin {
private String memo;


@ToString.Exclude
@OrderBy("id")
@OneToMany(mappedBy = "admin")
private final Set<AdminPlaceMap> adminPlaceMaps = new LinkedHashSet<>();


@Column(nullable = false, insertable = false, updatable = false,
columnDefinition = "datetime default CURRENT_TIMESTAMP")
@CreatedDate
Expand All @@ -71,4 +81,17 @@ public static Admin of(String email, String nickname, String password, String ph
return new Admin(email, nickname, password, phoneNumber, memo);
}


@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
return id != null && id.equals(((Admin) obj).getId());
}

@Override
public int hashCode() {
return Objects.hash(email, nickname, phoneNumber, createdAt, modifiedAt);
}

}
36 changes: 23 additions & 13 deletions src/main/java/com/uno/getinline/domain/AdminPlaceMap.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.uno.getinline.domain;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
Expand All @@ -10,13 +9,11 @@

import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.Objects;

@Getter
@ToString
@EqualsAndHashCode
@Table(indexes = {
@Index(columnList = "adminId"),
@Index(columnList = "placeId"),
@Index(columnList = "createdAt"),
@Index(columnList = "modifiedAt")
})
Expand All @@ -30,12 +27,12 @@ public class AdminPlaceMap {


@Setter
@Column(nullable = false)
private Long adminId;
@ManyToOne(optional = false)
private Admin admin;

@Setter
@Column(nullable = false)
private Long placeId;
@ManyToOne(optional = false)
private Place place;


@Column(nullable = false, insertable = false, updatable = false,
Expand All @@ -51,13 +48,26 @@ public class AdminPlaceMap {

protected AdminPlaceMap() {}

protected AdminPlaceMap(Long adminId, Long placeId) {
this.adminId = adminId;
this.placeId = placeId;
protected AdminPlaceMap(Admin admin, Place place) {
this.admin = admin;
this.place = place;
}

public static AdminPlaceMap of(Long adminId, Long placeId) {
return new AdminPlaceMap(adminId, placeId);
public static AdminPlaceMap of(Admin admin, Place place) {
return new AdminPlaceMap(admin, place);
}


@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
return id != null && id.equals(((AdminPlaceMap) obj).getId());
}

@Override
public int hashCode() {
return Objects.hash(place, admin, createdAt, modifiedAt);
}

}
44 changes: 31 additions & 13 deletions src/main/java/com/uno/getinline/domain/Event.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package com.uno.getinline.domain;

import com.uno.getinline.constant.EventStatus;
import lombok.*;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.Objects;

@Getter
@ToString
@EqualsAndHashCode
@Table(indexes = {
@Index(columnList = "placeId"),
@Index(columnList = "eventName"),
@Index(columnList = "eventStartDatetime"),
@Index(columnList = "eventEndDatetime"),
Expand All @@ -31,15 +32,15 @@ public class Event {


@Setter
@Column(nullable = false)
private Long placeId;
@ManyToOne(optional = false)
private Place place;

@Setter
@Column(nullable = false)
private String eventName;

@Setter
@Column(nullable = false, columnDefinition = "varchar default 'OPENED'")
@Column(nullable = false, columnDefinition = "varchar(20) default 'OPENED'")
@Enumerated(EnumType.STRING)
private EventStatus eventStatus;

Expand All @@ -48,16 +49,20 @@ public class Event {
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
private LocalDateTime eventStartDatetime;

@Setter @Column(nullable = false, columnDefinition = "datetime")
@Setter
@Column(nullable = false, columnDefinition = "datetime")
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
private LocalDateTime eventEndDatetime;

@Setter @Column(nullable = false, columnDefinition = "integer default 0")
@Setter
@Column(nullable = false, columnDefinition = "integer default 0")
private Integer currentNumberOfPeople;

@Setter @Column(nullable = false)
@Setter
@Column(nullable = false)
private Integer capacity;


@Setter
private String memo;

Expand All @@ -76,7 +81,7 @@ public class Event {
protected Event() {}

protected Event(
Long placeId,
Place place,
String eventName,
EventStatus eventStatus,
LocalDateTime eventStartDatetime,
Expand All @@ -85,7 +90,7 @@ protected Event(
Integer capacity,
String memo
) {
this.placeId = placeId;
this.place = place;
this.eventName = eventName;
this.eventStatus = eventStatus;
this.eventStartDatetime = eventStartDatetime;
Expand All @@ -96,7 +101,7 @@ protected Event(
}

public static Event of(
Long placeId,
Place place,
String eventName,
EventStatus eventStatus,
LocalDateTime eventStartDatetime,
Expand All @@ -106,7 +111,7 @@ public static Event of(
String memo
) {
return new Event(
placeId,
place,
eventName,
eventStatus,
eventStartDatetime,
Expand All @@ -117,4 +122,17 @@ public static Event of(
);
}


@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
return id != null && id.equals(((Event) obj).getId());
}

@Override
public int hashCode() {
return Objects.hash(eventName, eventStartDatetime, eventEndDatetime, createdAt, modifiedAt);
}

}
31 changes: 28 additions & 3 deletions src/main/java/com/uno/getinline/domain/Place.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.uno.getinline.domain;

import com.uno.getinline.constant.PlaceType;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
Expand All @@ -11,10 +10,12 @@

import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.LinkedHashSet;
import java.util.Objects;
import java.util.Set;

@Getter
@ToString
@EqualsAndHashCode
@Table(indexes = {
@Index(columnList = "placeName"),
@Index(columnList = "address"),
Expand All @@ -32,7 +33,7 @@ public class Place {


@Setter
@Column(nullable = false, columnDefinition = "varchar default 'COMMON'")
@Column(nullable = false, columnDefinition = "varchar(20) default 'COMMON'")
@Enumerated(EnumType.STRING)
private PlaceType placeType;

Expand Down Expand Up @@ -68,6 +69,17 @@ public class Place {
private LocalDateTime modifiedAt;


@ToString.Exclude
@OrderBy("id")
@OneToMany(mappedBy = "place")
private final Set<Event> events = new LinkedHashSet<>();

@ToString.Exclude
@OrderBy("id")
@OneToMany(mappedBy = "place")
private final Set<AdminPlaceMap> adminPlaceMaps = new LinkedHashSet<>();


protected Place() {}

protected Place(
Expand Down Expand Up @@ -97,4 +109,17 @@ public static Place of(
return new Place(placeType, placeName, address, phoneNumber, capacity, memo);
}


@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
return id != null && id.equals(((Place) obj).getId());
}

@Override
public int hashCode() {
return Objects.hash(placeName, address, phoneNumber, createdAt, modifiedAt);
}

}
Loading

0 comments on commit 3bb32c4

Please sign in to comment.