Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

회원 관련 엔티티 추가 (그리고 H2 환경 추가) #8

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ repositories {
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'com.h2database:h2:1.4.197'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.shopping.jpa.converter;

import javax.persistence.AttributeConverter;
import javax.persistence.Converter;

@Converter
public class BooleanToYOrNConverter implements AttributeConverter<Boolean, String> {
@Override
public String convertToDatabaseColumn(Boolean attribute) {
return attribute ? "Y" : "N";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AttributeConverter 라니..! 새로운걸 배워갑니다!

그런데 만약 Boolean attributenull 이면 조건식이 어떻게 되는건가요?
별도의 NPE 체크 없이 사용 가능한가요??

Copy link
Author

@greenn-lab greenn-lab May 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

당연히 안되겠죠^^;
좋은 지적 감사합니다, 수정할게요. 항상 대충하는 버릇이...

코드 리뷰 하니까 좋네요, 성준님😍

}

@Override
public Boolean convertToEntityAttribute(String dbData) {
return dbData.equals("Y");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

만약 dbData 값이 null 이면 NPE 가 발생할 것 같습니다..!
"Y".equals(dbData); 로 해 보시는 것은 어떨까요?
(또는 StringUtils 등을 적극 활용해보는 것도 좋을 것 같습니다!!)

요즘 NPE 로 고생중이라 JPA 외적인 부분들만 눈에 들어오네요 ㅠㅠ

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

최근에 저도 실무에 코딩 가이드 적용하면서 NPE 지적을 엄청 받았으면서 또 이랬어요.
수정할게요~

}
}
50 changes: 50 additions & 0 deletions src/main/java/com/shopping/jpa/member/model/Address.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.shopping.jpa.member.model;

import com.shopping.jpa.converter.BooleanToYOrNConverter;
import lombok.Getter;
import lombok.Setter;

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

@Entity
@Setter @Getter
public class Address {

@Id
@Column(name = "ADDR_ID"/*, columnDefinition = "char(20)"*/)
private Long id;

@Column(name = "NAME", length = 20, nullable = false)
private String name;

@Column(name = "ZIPCD", length = 10, nullable = false)
private String zipCode;

@Column(name = "ADDR", length = 50, nullable = false)
private String address;

@Column(name = "ADDR_DTL", length = 50, nullable = false)
private String detail;

@Column(name = "IS_MAS", nullable = false, columnDefinition = "char(1)")
@Convert(converter = BooleanToYOrNConverter.class)
private boolean isMaster;

@Column(name = "LST_USE_DT")
//@Temporal(TemporalType.TIMESTAMP)
private LocalDateTime lastUsedDateTime;

@Column(name = "REG_DT")
//@Temporal(TemporalType.TIMESTAMP)
private LocalDateTime registerDateTime;

@Column(name = "UPT_DT")
//@Temporal(TemporalType.TIMESTAMP)
private LocalDateTime updateDateTime;

@ManyToOne
@JoinColumn(name = "id")
private Member member;

}
16 changes: 16 additions & 0 deletions src/main/java/com/shopping/jpa/member/model/AdminMember.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.shopping.jpa.member.model;

import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;

@Entity
@PrimaryKeyJoinColumn(name = "MID")
@Setter @Getter
public class AdminMember extends Member {

@Column(name = "PA_IDX", columnDefinition = "char(5)")
private String partCode;

}
22 changes: 22 additions & 0 deletions src/main/java/com/shopping/jpa/member/model/CommonMember.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.shopping.jpa.member.model;

import lombok.Getter;
import lombok.Setter;

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

@Entity
@PrimaryKeyJoinColumn(name = "MID")
@Getter @Setter
public class CommonMember extends Member {

@Column(name = "GRD_IDX", columnDefinition = "char(5)")
private String gradeCode;

@OneToMany(mappedBy = "member")
@OrderBy("priority")
private List<Address> addresses = new ArrayList<>();

}
46 changes: 46 additions & 0 deletions src/main/java/com/shopping/jpa/member/model/LoginHistory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.shopping.jpa.member.model;

import lombok.Getter;
import lombok.Setter;

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

@Entity
@Table(name = "LOGIN_HIS")
@Setter @Getter
public class LoginHistory {

@Id
@GeneratedValue
private Integer seq;

@Column(nullable = false, columnDefinition = "char(5)")
@Enumerated(value = EnumType.STRING)
private LoginResult result;

@Column(name = "REG_IP", nullable = false, length = 30)
private String ip;

@Column(name = "REG_DT")
//@Temporal(TemporalType.TIMESTAMP)
private LocalDateTime registerDateTime;

@ManyToOne
@JoinColumn(name = "id")
private Member member;


@Converter(autoApply = true)
public static class LoginResultConverter implements AttributeConverter<LoginResult, String> {
@Override
public String convertToDatabaseColumn(LoginResult attribute) {
return attribute.getValue();
}

@Override
public LoginResult convertToEntityAttribute(String dbData) {
return LoginResult.valueOf(dbData);
}
}
}
18 changes: 18 additions & 0 deletions src/main/java/com/shopping/jpa/member/model/LoginResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.shopping.jpa.member.model;

public enum LoginResult {
SUCCESS("00000"),
FAILURE("99999"),
EXPIRED("99998"),
DENIED("99997");

private String value;

LoginResult(String value) {
this.value = value;
}

public String getValue() {
return value;
}
}
40 changes: 40 additions & 0 deletions src/main/java/com/shopping/jpa/member/model/Member.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.shopping.jpa.member.model;

import lombok.Getter;
import lombok.Setter;

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

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Setter @Getter
public abstract class Member {

@Id
@GeneratedValue
@Column(name = "MID", columnDefinition = "char(10)")
private String id;

@Column(name = "UID", length = 50, unique = true, nullable = false)
private String username;

@Column(name = "PWD", length = 100,nullable = false)
private String password;

@Column(name = "NAME", length = 20, nullable = false)
private String name;

@Column(name = "REG_DT", nullable = false)
//@Temporal(TemporalType.TIMESTAMP)
private LocalDateTime registerDate;

@Column(name = "ADDR_USE_IDX", nullable = false, columnDefinition = "char(5)")
private String addressUseIndex;

@OneToMany(mappedBy = "member")
private List<LoginHistory> loginHistories = new ArrayList<>();

}
12 changes: 12 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
spring:

jpa:
show-sql: true
hibernate:
ddl-auto: create

datasource:
hikari:
driver-class-name: org.h2.Driver
jdbc-url: jdbc:h2:tcp://localhost/~/shopping-site
username: sa
password: ""