diff --git "a/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/.gitignore" "b/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/.gitignore" new file mode 100644 index 00000000..c2065bc2 --- /dev/null +++ "b/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/.gitignore" @@ -0,0 +1,37 @@ +HELP.md +.gradle +build/ +!gradle/wrapper/gradle-wrapper.jar +!**/src/main/**/build/ +!**/src/test/**/build/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ diff --git "a/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/build.gradle" "b/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/build.gradle" new file mode 100644 index 00000000..94ffc95f --- /dev/null +++ "b/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/build.gradle" @@ -0,0 +1,41 @@ +plugins { + id 'java' + id 'org.springframework.boot' version '2.7.17' + id 'io.spring.dependency-management' version '1.0.15.RELEASE' +} + +group = 'study' +version = '0.0.1-SNAPSHOT' + +java { + sourceCompatibility = '11' +} + +configurations { + compileOnly { + extendsFrom annotationProcessor + } +} + +repositories { + mavenCentral() +} + +dependencies { + implementation 'org.springframework.boot:spring-boot-starter-data-jpa' + implementation 'org.springframework.boot:spring-boot-starter-web' + compileOnly 'org.projectlombok:lombok' + runtimeOnly 'com.h2database:h2' + annotationProcessor 'org.projectlombok:lombok' + testImplementation 'org.springframework.boot:spring-boot-starter-test' + implementation 'com.github.gavlyukovskiy:p6spy-spring-boot-starter:1.5.7' + +} + +tasks.named('bootBuildImage') { + builder = 'paketobuildpacks/builder-jammy-base:latest' +} + +tasks.named('test') { + useJUnitPlatform() +} diff --git "a/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/gradle/wrapper/gradle-wrapper.jar" "b/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/gradle/wrapper/gradle-wrapper.jar" new file mode 100644 index 00000000..7f93135c Binary files /dev/null and "b/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/gradle/wrapper/gradle-wrapper.jar" differ diff --git "a/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/gradle/wrapper/gradle-wrapper.properties" "b/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/gradle/wrapper/gradle-wrapper.properties" new file mode 100644 index 00000000..3fa8f862 --- /dev/null +++ "b/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/gradle/wrapper/gradle-wrapper.properties" @@ -0,0 +1,7 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip +networkTimeout=10000 +validateDistributionUrl=true +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git "a/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/gradlew" "b/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/gradlew" new file mode 100644 index 00000000..1aa94a42 --- /dev/null +++ "b/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/gradlew" @@ -0,0 +1,249 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) +APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + if ! command -v java >/dev/null 2>&1 + then + die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Collect all arguments for the java command: +# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# and any embedded shellness will be escaped. +# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be +# treated as '${Hostname}' itself on the command line. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git "a/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/gradlew.bat" "b/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/gradlew.bat" new file mode 100644 index 00000000..93e3f59f --- /dev/null +++ "b/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/gradlew.bat" @@ -0,0 +1,92 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%"=="" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if %ERRORLEVEL% equ 0 goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git "a/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/settings.gradle" "b/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/settings.gradle" new file mode 100644 index 00000000..89b5a933 --- /dev/null +++ "b/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/settings.gradle" @@ -0,0 +1 @@ +rootProject.name = 'data-jpa' diff --git "a/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/src/main/java/study/datajpa/DataJpaApplication.java" "b/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/src/main/java/study/datajpa/DataJpaApplication.java" new file mode 100644 index 00000000..ecb266e9 --- /dev/null +++ "b/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/src/main/java/study/datajpa/DataJpaApplication.java" @@ -0,0 +1,25 @@ +package study.datajpa; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; +import org.springframework.data.domain.AuditorAware; +import org.springframework.data.jpa.repository.config.EnableJpaAuditing; + +import java.util.Optional; +import java.util.UUID; + +@SpringBootApplication +@EnableJpaAuditing +public class DataJpaApplication { + + public static void main(String[] args) { + SpringApplication.run(DataJpaApplication.class, args); + } + + @Bean + public AuditorAware auditorProvider(){ + return ()-> Optional.of(UUID.randomUUID().toString()); + } + +} diff --git "a/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/src/main/java/study/datajpa/controller/HelloController.java" "b/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/src/main/java/study/datajpa/controller/HelloController.java" new file mode 100644 index 00000000..66394eed --- /dev/null +++ "b/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/src/main/java/study/datajpa/controller/HelloController.java" @@ -0,0 +1,52 @@ +package study.datajpa.controller; + +import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; +import org.springframework.data.web.PageableDefault; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import study.datajpa.dto.MemberDto; +import study.datajpa.entity.Member; +import study.datajpa.repository.MemberRepository; + +import javax.annotation.PostConstruct; + +@RestController +@RequiredArgsConstructor +public class HelloController { + + private final MemberRepository memberRepository; + + + @RequestMapping("/hello") + public String hello(){ + return "hello"; + } + + @GetMapping("/members") + public Page list(Pageable pageable){ + Page page=memberRepository.findAll(pageable); + + return page; + } + + @GetMapping("/members-dto") + public Page list2(Pageable pageable){ + Page page=memberRepository.findAll(pageable); + Page pageDto=page.map(MemberDto::new); + + return pageDto; + } + + + + @PostConstruct + public void init(){ + for(int i=0;i<100;i++){ + memberRepository.save(new Member("user"+i,i)); + } + } +} diff --git "a/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/src/main/java/study/datajpa/dto/MemberDto.java" "b/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/src/main/java/study/datajpa/dto/MemberDto.java" new file mode 100644 index 00000000..b616f5a8 --- /dev/null +++ "b/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/src/main/java/study/datajpa/dto/MemberDto.java" @@ -0,0 +1,22 @@ +package study.datajpa.dto; + +import lombok.Data; +import study.datajpa.entity.Member; + +@Data +public class MemberDto { + private Long id; + private String username; + private String teamName; + + public MemberDto(Long id,String username,String teamName){ + this.id=id; + this.username=username; + this.teamName=teamName; + } + + public MemberDto(Member m){ + this.id=m.getId(); + this.username=m.getUsername(); + } +} diff --git "a/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/src/main/java/study/datajpa/entity/BaseEntity.java" "b/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/src/main/java/study/datajpa/entity/BaseEntity.java" new file mode 100644 index 00000000..45d2e018 --- /dev/null +++ "b/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/src/main/java/study/datajpa/entity/BaseEntity.java" @@ -0,0 +1,33 @@ +package study.datajpa.entity; + +import lombok.Getter; +import org.springframework.data.annotation.CreatedBy; +import org.springframework.data.annotation.CreatedDate; +import org.springframework.data.annotation.LastModifiedBy; +import org.springframework.data.annotation.LastModifiedDate; +import org.springframework.data.jpa.domain.support.AuditingEntityListener; + +import javax.persistence.Column; +import javax.persistence.EntityListeners; +import javax.persistence.MappedSuperclass; +import java.time.LocalDateTime; + +@EntityListeners(AuditingEntityListener.class) +@MappedSuperclass +@Getter +public class BaseEntity { + @CreatedDate + @Column(updatable = false) + private LocalDateTime createdDate; + + @LastModifiedDate + private LocalDateTime lastModifiedDate; + + @CreatedBy + @Column(updatable = false) + private String createdBy; + + @LastModifiedBy + private String lastModifiedBy; + +} diff --git "a/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/src/main/java/study/datajpa/entity/JpaBaseEntity.java" "b/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/src/main/java/study/datajpa/entity/JpaBaseEntity.java" new file mode 100644 index 00000000..67f91a6e --- /dev/null +++ "b/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/src/main/java/study/datajpa/entity/JpaBaseEntity.java" @@ -0,0 +1,30 @@ +package study.datajpa.entity; + +import lombok.Getter; + +import javax.persistence.Column; +import javax.persistence.MappedSuperclass; +import javax.persistence.PrePersist; +import javax.persistence.PreUpdate; +import java.time.LocalDateTime; + +@MappedSuperclass +@Getter +public class JpaBaseEntity { + @Column(updatable = false) + private LocalDateTime createdDate; + + private LocalDateTime updatedDate; + + @PrePersist + public void prePersist(){ + LocalDateTime now=LocalDateTime.now(); + createdDate=now; + updatedDate=now; + + } + @PreUpdate + public void preUpdate(){ + updatedDate=LocalDateTime.now(); + } +} diff --git "a/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/src/main/java/study/datajpa/entity/Member.java" "b/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/src/main/java/study/datajpa/entity/Member.java" new file mode 100644 index 00000000..6025db10 --- /dev/null +++ "b/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/src/main/java/study/datajpa/entity/Member.java" @@ -0,0 +1,48 @@ +package study.datajpa.entity; + +import lombok.*; + +import javax.persistence.*; + +@Entity +@Getter @Setter +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@ToString(of = {"id","username","age"}) +public class Member extends BaseEntity{ + @Id @GeneratedValue + @Column(name = "member_id") + private Long id; + + private String username; + + private int age; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "team_id") + private Team team; + + + + public Member(String username) { + this.username=username; + } + + public Member(String username,int age){ + this.username=username; + this.age=age; + } + + public Member(String username,int age,Team team){ + this.username=username; + this.age=age; + + if(team!=null){ + changeTeam(team); + } + } + + public void changeTeam(Team team){ + this.team=team; + team.getMembers().add(this); + } +} diff --git "a/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/src/main/java/study/datajpa/entity/Team.java" "b/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/src/main/java/study/datajpa/entity/Team.java" new file mode 100644 index 00000000..c0a45b8f --- /dev/null +++ "b/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/src/main/java/study/datajpa/entity/Team.java" @@ -0,0 +1,26 @@ +package study.datajpa.entity; + +import lombok.*; + +import javax.persistence.*; +import java.util.ArrayList; +import java.util.List; + +@Entity +@Getter @Setter +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@ToString(of = {"id", "name"}) +public class Team { + @Id @GeneratedValue + @Column(name = "team_id") + private Long id; + + private String name; + + @OneToMany(mappedBy = "team") + List members=new ArrayList<>(); + + public Team(String name){ + this.name=name; + } +} diff --git "a/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/src/main/java/study/datajpa/repository/MemberJpaRepository.java" "b/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/src/main/java/study/datajpa/repository/MemberJpaRepository.java" new file mode 100644 index 00000000..5b3ad968 --- /dev/null +++ "b/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/src/main/java/study/datajpa/repository/MemberJpaRepository.java" @@ -0,0 +1,80 @@ +package study.datajpa.repository; + +import org.springframework.stereotype.Repository; +import study.datajpa.entity.Member; + +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import java.util.List; +import java.util.Optional; + +@Repository +public class MemberJpaRepository { + @PersistenceContext + private EntityManager em; + + + public Member save(Member member){ + em.persist(member); + return member; + } + + public void delete(Member member){ + em.remove(member); + } + + public List findAll(){ + return em.createQuery("select m from Member m",Member.class).getResultList(); + } + + public Optional findById(Long id){ + Member member=em.find(Member.class,id); + return Optional.ofNullable(member); + } + + public long count(){ + return em.createQuery("select count(m) from Member m",Long.class).getSingleResult(); + } + + public Member find(Long id){ + return em.find(Member.class,id); + } + + public List findByUsernameAndAgeGreaterThan(String username, int age){ + return em.createQuery("select m from Member m where m.username = :username and m.age > :age") + .setParameter("username",username) + .setParameter("age",age) + .getResultList(); + } + + + public List findByPage(int age,int offset, int limit){ + return em.createQuery("select m from Member m where m.age = :age order by m.username desc") + .setParameter("age",age) + .setFirstResult(offset) + .setMaxResults(limit) + .getResultList(); + } + + public long totalCount(int age){ + return em.createQuery("select count(m) from Member m where m.age = :age",Long.class) + .setParameter("age",age) + .getSingleResult(); + } + + public int bulkAgePlus(int age){ + int resultCount = em.createQuery( + "update Member m set m.age = m.age+1" + + "where m.age >=:age") + .setParameter("age",age) + .executeUpdate(); + + return resultCount; + + + } + + + + +} diff --git "a/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/src/main/java/study/datajpa/repository/MemberRepository.java" "b/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/src/main/java/study/datajpa/repository/MemberRepository.java" new file mode 100644 index 00000000..885a7644 --- /dev/null +++ "b/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/src/main/java/study/datajpa/repository/MemberRepository.java" @@ -0,0 +1,76 @@ +package study.datajpa.repository; + +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Slice; +import org.springframework.data.domain.Sort; +import org.springframework.data.jpa.repository.*; +import org.springframework.data.repository.query.Param; +import org.springframework.web.bind.annotation.PathVariable; +import study.datajpa.dto.MemberDto; +import study.datajpa.entity.Member; + +import javax.persistence.QueryHint; +import java.util.List; +import java.util.Optional; + +public interface MemberRepository extends JpaRepository,MemberRepositoryCustom { + public List findByUsernameAndAgeGreaterThan(String username,int age); + + @Query("select m from Member m where m.username = :username and m.age = :age") + List findUser(@Param("username") String username, @Param("age") int age); + + + @Query("select m.username from Member m") + List findUsernameList(); + + @Query("select new study.datajpa.dto.MemberDto(m.id,m.username,t.name) "+ "from Member m join m.team t") + List findMemberDto(); + + @Query("select m from Member m where m.username in :names") + List findByNames(@Param("names") List names); + + + List findListByUsername(String name); + + Member findMemberByUsername(String name); + + Optional findOptionalByUsername(String username); + + + Page findPageByUsername(String name, Pageable pageable); + + Slice findSliceByUsername(String name,Pageable pageable); + + List findListByUsername(String name,Pageable pageable); + + List findByUsername(String name, Sort sort); + + Page findByAge(int age,Pageable pageable); + + Slice findSliceByAge(int age,Pageable pageable); + + @Query(value = "select m from Member m left join m.team t",countQuery = "select count(m) from Member m") + Page findJoinByAge(int age,Pageable pageable); + + + @Modifying(clearAutomatically = true) + @Query("update Member m set m.age = m.age+1 where m.age >= :age") + int bulkAgePlus(@Param("age") int age); + + @Query("select m from Member m left join fetch m.team") + List findMemberFetchJoin(); + + @Override + @EntityGraph(attributePaths = {"team"}) + List findAll(); + + @QueryHints(value = @QueryHint(name="org.hibernate.readOnly",value = "true")) + Member findReadOnlyByUsername(String username); + + @Query(value = "select age from member where username =?",nativeQuery = true) + int findByNativeQuery(String username); + + + +} diff --git "a/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/src/main/java/study/datajpa/repository/MemberRepositoryCustom.java" "b/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/src/main/java/study/datajpa/repository/MemberRepositoryCustom.java" new file mode 100644 index 00000000..7ba12646 --- /dev/null +++ "b/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/src/main/java/study/datajpa/repository/MemberRepositoryCustom.java" @@ -0,0 +1,9 @@ +package study.datajpa.repository; + +import study.datajpa.entity.Member; + +import java.util.List; + +public interface MemberRepositoryCustom { + List findMemberCustom(); +} diff --git "a/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/src/main/java/study/datajpa/repository/MemberRepositoryImpl.java" "b/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/src/main/java/study/datajpa/repository/MemberRepositoryImpl.java" new file mode 100644 index 00000000..e06eb44a --- /dev/null +++ "b/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/src/main/java/study/datajpa/repository/MemberRepositoryImpl.java" @@ -0,0 +1,18 @@ +package study.datajpa.repository; + +import lombok.RequiredArgsConstructor; +import study.datajpa.entity.Member; + +import javax.persistence.EntityManager; +import java.util.List; + +@RequiredArgsConstructor +public class MemberRepositoryImpl implements MemberRepositoryCustom{ + private final EntityManager em; + + @Override + public List findMemberCustom(){ + return em.createQuery("select m from Member m") + .getResultList(); + } +} diff --git "a/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/src/main/java/study/datajpa/repository/TeamJpaRepository.java" "b/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/src/main/java/study/datajpa/repository/TeamJpaRepository.java" new file mode 100644 index 00000000..c6a7647c --- /dev/null +++ "b/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/src/main/java/study/datajpa/repository/TeamJpaRepository.java" @@ -0,0 +1,42 @@ +package study.datajpa.repository; + +import org.springframework.stereotype.Repository; +import study.datajpa.entity.Member; +import study.datajpa.entity.Team; + +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import java.util.List; +import java.util.Optional; + +@Repository +public class TeamJpaRepository { + + @PersistenceContext + private EntityManager em; + + + public Team save(Team team){ + em.persist(team); + return team; + } + + public void delete(Team team){ + em.remove(team); + } + + public List findAll(){ + return em.createQuery("select t from Team t",Team.class).getResultList(); + } + + public Optional findById(Long id){ + Team team=em.find(Team.class,id); + return Optional.ofNullable(team); + } + + public long count(){ + return em.createQuery("select count(t) from Team t",Long.class).getSingleResult(); + } + + +} diff --git "a/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/src/main/java/study/datajpa/repository/TeamRepository.java" "b/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/src/main/java/study/datajpa/repository/TeamRepository.java" new file mode 100644 index 00000000..8c84ee15 --- /dev/null +++ "b/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/src/main/java/study/datajpa/repository/TeamRepository.java" @@ -0,0 +1,8 @@ +package study.datajpa.repository; + +import org.springframework.data.jpa.repository.JpaRepository; +import study.datajpa.entity.Team; + +public interface TeamRepository extends JpaRepository { + +} diff --git "a/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/src/main/resources/application.yml" "b/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/src/main/resources/application.yml" new file mode 100644 index 00000000..2f81c769 --- /dev/null +++ "b/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/src/main/resources/application.yml" @@ -0,0 +1,19 @@ +spring: + datasource: + url: jdbc:h2:tcp://localhost/~/datajpa + username: sa + password: + driver-class-name: org.h2.Driver + + jpa: + hibernate: + ddl-auto: create + properties: + hibernate: + # show_sql: true + format_sql: true + + +logging.level: + org.hibernate.SQL: debug +# org.hibernate.type: trace diff --git "a/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/src/test/java/study/datajpa/DataJpaApplicationTests.java" "b/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/src/test/java/study/datajpa/DataJpaApplicationTests.java" new file mode 100644 index 00000000..bdeeef1a --- /dev/null +++ "b/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/src/test/java/study/datajpa/DataJpaApplicationTests.java" @@ -0,0 +1,13 @@ +package study.datajpa; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class DataJpaApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git "a/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/src/test/java/study/datajpa/entity/MemberTest.java" "b/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/src/test/java/study/datajpa/entity/MemberTest.java" new file mode 100644 index 00000000..da30de29 --- /dev/null +++ "b/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/src/test/java/study/datajpa/entity/MemberTest.java" @@ -0,0 +1,50 @@ +package study.datajpa.entity; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.annotation.Rollback; +import org.springframework.transaction.annotation.Transactional; + +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; +@SpringBootTest + +class MemberTest { + + @PersistenceContext + EntityManager em; + + @Test + @Transactional + @Rollback(false) + public void testEntity() { + Team teamA = new Team("teamA"); + Team teamB = new Team("teamB"); + em.persist(teamA); + em.persist(teamB); + Member member1 = new Member("member1", 10, teamA); + Member member2 = new Member("member2", 20, teamA); + Member member3 = new Member("member3", 30, teamB); + Member member4 = new Member("member4", 40, teamB); + em.persist(member1); + em.persist(member2); + em.persist(member3); + em.persist(member4); + + em.flush(); + em.clear(); + + List members = em.createQuery("select m from Member m", + Member.class) + .getResultList(); + for (Member member : members) { + System.out.println("member=" + member); + System.out.println("-> member.team=" + member.getTeam()); + } + } +} + diff --git "a/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/src/test/java/study/datajpa/repository/MemberJpaRepositoryTest.java" "b/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/src/test/java/study/datajpa/repository/MemberJpaRepositoryTest.java" new file mode 100644 index 00000000..8d9aceb5 --- /dev/null +++ "b/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/src/test/java/study/datajpa/repository/MemberJpaRepositoryTest.java" @@ -0,0 +1,140 @@ +package study.datajpa.repository; + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.annotation.Rollback; +import org.springframework.transaction.annotation.Transactional; +import study.datajpa.entity.Member; + +import javax.persistence.EntityManager; +import java.lang.management.MemoryManagerMXBean; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; +@SpringBootTest +@Transactional +@Rollback(false) +class MemberJpaRepositoryTest { + + @Autowired + MemberJpaRepository memberJpaRepository; + + @Autowired + EntityManager em; + + @Test + public void testMember(){ + Member member=new Member("memberA"); + Member savedMember=memberJpaRepository.save(member); + + Member findMember=memberJpaRepository.find(savedMember.getId()); + + Assertions.assertThat(findMember.getId()).isEqualTo(member.getId()); + Assertions.assertThat(findMember.getUsername()).isEqualTo(member.getUsername()); + + Assertions.assertThat(findMember).isEqualTo(member); + + + + } + + + @Test + public void basicCRUD() { + Member member1 = new Member("member1"); + Member member2 = new Member("member2"); + memberJpaRepository.save(member1); + memberJpaRepository.save(member2); + + Member findMember1 = memberJpaRepository.findById(member1.getId()).get(); + Member findMember2 = memberJpaRepository.findById(member2.getId()).get(); + Assertions.assertThat(findMember1).isEqualTo(member1); + Assertions.assertThat(findMember2).isEqualTo(member2); + + List all = memberJpaRepository.findAll(); + Assertions.assertThat(all.size()).isEqualTo(2); + + long count = memberJpaRepository.count(); + Assertions.assertThat(count).isEqualTo(2); + + memberJpaRepository.delete(member1); + memberJpaRepository.delete(member2); + long deletedCount = memberJpaRepository.count(); + Assertions.assertThat(deletedCount).isEqualTo(0); + } + + + @Test + public void findByUsernameAndAgeGreaterThan(){ + Member m1= new Member("AAA",10); + Member m2 = new Member("AAA",20); + memberJpaRepository.save(m1); + memberJpaRepository.save(m2); + + List result= + memberJpaRepository.findByUsernameAndAgeGreaterThan("AAA",15); + Assertions.assertThat(result.get(0).getUsername()).isEqualTo("AAA"); + Assertions.assertThat(result.get(0).getAge()).isEqualTo(20); + Assertions.assertThat(result.size()).isEqualTo(1); + + + } + + @Test + public void paging() throws Exception { + memberJpaRepository.save(new Member("member1", 10)); + memberJpaRepository.save(new Member("member2", 10)); + memberJpaRepository.save(new Member("member3", 10)); + memberJpaRepository.save(new Member("member4", 10)); + memberJpaRepository.save(new Member("member5", 10)); + + int age=10; + int offset=0; + int limit=3; + + List members = memberJpaRepository.findByPage(age,offset,limit); + long totalCount=memberJpaRepository.totalCount(age); + + Assertions.assertThat(members.size()).isEqualTo(3); + Assertions.assertThat(totalCount).isEqualTo(5); + + + + } + + @Test + public void bulkUpdate() throws Exception{ + memberJpaRepository.save(new Member("member1",10)); + memberJpaRepository.save(new Member("member2",19)); + memberJpaRepository.save(new Member("member3",20)); + memberJpaRepository.save(new Member("member4",21)); + memberJpaRepository.save(new Member("member5",40)); + + int resultCount = memberJpaRepository.bulkAgePlus(20); + + Assertions.assertThat(resultCount).isEqualTo(3); + } + + @Test + public void jpaEventBaseEntity() throws Exception{ + Member member=new Member("member1"); + memberJpaRepository.save(member); + + Thread.sleep(100); + member.setUsername("member2"); + + em.flush(); + em.clear(); + + Member findMember=memberJpaRepository.findById(member.getId()).get(); + + System.out.println("findMember.createdDate = "+findMember.getCreatedDate()); + // System.out.println("findMember.updatedDate = "+findMember.getUpdatedDate()); + + + } + + +} \ No newline at end of file diff --git "a/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/src/test/java/study/datajpa/repository/MemberRepositoryTest.java" "b/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/src/test/java/study/datajpa/repository/MemberRepositoryTest.java" new file mode 100644 index 00000000..08ca0fd2 --- /dev/null +++ "b/\354\235\264\354\247\204\354\232\260/spring_data_jpa_jinu/data-jpa/src/test/java/study/datajpa/repository/MemberRepositoryTest.java" @@ -0,0 +1,337 @@ +package study.datajpa.repository; + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Slice; +import org.springframework.data.domain.Sort; +import org.springframework.test.annotation.Rollback; +import org.springframework.transaction.annotation.Transactional; +import study.datajpa.dto.MemberDto; +import study.datajpa.entity.Member; +import study.datajpa.entity.Team; + +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import javax.swing.plaf.metal.MetalMenuBarUI; +import java.lang.management.MemoryManagerMXBean; +import java.util.Arrays; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; +@SpringBootTest +@Transactional +@Rollback(false) +class MemberRepositoryTest { + + + @Autowired + MemberRepository memberRepository; + + @Autowired + TeamRepository teamRepository; + + @PersistenceContext + EntityManager em; + + @Test + public void testMember(){ + Member member=new Member("memberA"); + Member savedMember=memberRepository.save(member); + + Member findMember=memberRepository.findById(savedMember.getId()).get(); + + Assertions.assertThat(findMember.getId()).isEqualTo(member.getId()); + Assertions.assertThat(findMember.getUsername()).isEqualTo(member.getUsername()); + + Assertions.assertThat(findMember).isEqualTo(member); + + + + } + + @Test + public void basicCRUD() { + Member member1 = new Member("member1"); + Member member2 = new Member("member2"); + memberRepository.save(member1); + memberRepository.save(member2); + + Member findMember1 = memberRepository.findById(member1.getId()).get(); + Member findMember2 = memberRepository.findById(member2.getId()).get(); + Assertions.assertThat(findMember1).isEqualTo(member1); + Assertions.assertThat(findMember2).isEqualTo(member2); + + List all = memberRepository.findAll(); + Assertions.assertThat(all.size()).isEqualTo(2); + + long count = memberRepository.count(); + Assertions.assertThat(count).isEqualTo(2); + + memberRepository.delete(member1); + memberRepository.delete(member2); + long deletedCount = memberRepository.count(); + Assertions.assertThat(deletedCount).isEqualTo(0); + } + + + @Test + public void findByUsernameAndAgeGreaterThan(){ + Member m1= new Member("AAA",10); + Member m2 = new Member("AAA",20); + memberRepository.save(m1); + memberRepository.save(m2); + + List result= memberRepository.findByUsernameAndAgeGreaterThan("AAA",15); + Assertions.assertThat(result.get(0).getUsername()).isEqualTo("AAA"); + Assertions.assertThat(result.get(0).getAge()).isEqualTo(20); + Assertions.assertThat(result.size()).isEqualTo(1); + + + } + + @Test + public void testQuery(){ + Member m1=new Member("AAA",10); + Member m2=new Member("BBB",20); + + memberRepository.save(m1); + memberRepository.save(m2); + + List result= memberRepository.findUser("AAA",10); + Assertions.assertThat(result.get(0)).isEqualTo(m1); + + + } + + + @Test + public void findUsernameList(){ + Member m1=new Member("AAA",10); + Member m2= new Member("BBB",20); + memberRepository.save(m1); + memberRepository.save(m2); + + List usernameList = memberRepository.findUsernameList(); + + for (String s : usernameList) { + System.out.println("s = "+ s); + } + } + + @Test + public void findMemberDto(){ + Team team=new Team("teamA"); + teamRepository.save(team); + + Member m1=new Member("AAA",10); + m1.setTeam(team); + memberRepository.save(m1); + + List memberDto=memberRepository.findMemberDto(); + + for (MemberDto dto : memberDto) { + System.out.println("dto = "+dto); + } + } + + @Test + public void findByNames(){ + Member m1=new Member("AAA",10); + Member m2= new Member("BBB",20); + memberRepository.save(m1); + memberRepository.save(m2); + + List result = memberRepository.findByNames(Arrays.asList("AAA","BBB")); + for (Member member : result) { + System.out.println("member = "+ member); + } + } + + @Test + public void returnType(){ + Member m1=new Member("AAA",10); + Member m2= new Member("BBB",20); + memberRepository.save(m1); + memberRepository.save(m2); + + List aaa = memberRepository.findListByUsername("AAA"); + for (Member member : aaa) { + System.out.println(aaa); + } + + } + + @Test + public void page() throws Exception{ + memberRepository.save(new Member("member1",10)); + memberRepository.save(new Member("member2",10)); + memberRepository.save(new Member("member3",10)); + memberRepository.save(new Member("member4",10)); + memberRepository.save(new Member("member5",10)); + + PageRequest pageRequest = PageRequest.of(0,3, Sort.by(Sort.Direction.DESC,"username")); + Page page = memberRepository.findByAge(10,pageRequest); + + + + List content=page.getContent(); + Assertions.assertThat(content.size()).isEqualTo(3); + Assertions.assertThat(page.getTotalElements()).isEqualTo(5); + Assertions.assertThat(page.getNumber()).isEqualTo(0); + Assertions.assertThat(page.getTotalPages()).isEqualTo(2); + Assertions.assertThat(page.isFirst()).isTrue(); + Assertions.assertThat(page.hasNext()).isTrue(); + + } + + @Test + public void pageJoin() throws Exception{ + memberRepository.save(new Member("member1",10)); + memberRepository.save(new Member("member2",10)); + memberRepository.save(new Member("member3",10)); + memberRepository.save(new Member("member4",10)); + memberRepository.save(new Member("member5",10)); + + PageRequest pageRequest = PageRequest.of(0,3, Sort.by(Sort.Direction.DESC,"username")); + Page page = memberRepository.findJoinByAge(10,pageRequest); + + + + List content=page.getContent(); + Assertions.assertThat(content.size()).isEqualTo(3); + Assertions.assertThat(page.getTotalElements()).isEqualTo(5); + Assertions.assertThat(page.getNumber()).isEqualTo(0); + Assertions.assertThat(page.getTotalPages()).isEqualTo(2); + Assertions.assertThat(page.isFirst()).isTrue(); + Assertions.assertThat(page.hasNext()).isTrue(); + + } + + @Test + public void slice() throws Exception{ + memberRepository.save(new Member("member1",10)); + memberRepository.save(new Member("member2",10)); + memberRepository.save(new Member("member3",10)); + memberRepository.save(new Member("member4",10)); + memberRepository.save(new Member("member5",10)); + + PageRequest pageRequest = PageRequest.of(0,3, Sort.by(Sort.Direction.DESC,"username")); + Slice page = memberRepository.findSliceByAge(10,pageRequest); + + + + List content=page.getContent(); + Assertions.assertThat(content.size()).isEqualTo(3); + //Assertions.assertThat(page.getTotalElements()).isEqualTo(5); + Assertions.assertThat(page.getNumber()).isEqualTo(0); + //Assertions.assertThat(page.getTotalPages()).isEqualTo(2); + Assertions.assertThat(page.isFirst()).isTrue(); + Assertions.assertThat(page.hasNext()).isTrue(); + + } + + @Test + public void bulkUpdate() throws Exception{ + memberRepository.save(new Member("member1",10)); + memberRepository.save(new Member("member2",19)); + memberRepository.save(new Member("member3",20)); + memberRepository.save(new Member("member4",21)); + memberRepository.save(new Member("member5",40)); + + int resultCount = memberRepository.bulkAgePlus(20); + + Member findMember=memberRepository.findMemberByUsername("member5"); + + System.out.println("age="+findMember.getAge()); + + Assertions.assertThat(resultCount).isEqualTo(3); + } + + @Test + public void findMemberLazy() throws Exception { + //given + //member1 -> teamA + //member2 -> teamB + Team teamA = new Team("teamA"); + Team teamB = new Team("teamB"); + teamRepository.save(teamA); + teamRepository.save(teamB); + memberRepository.save(new Member("member1", 10, teamA)); + memberRepository.save(new Member("member2", 20, teamB)); + em.flush(); + em.clear(); + //when + List members = memberRepository.findAll(); + //then + for (Member member : members) { + member.getTeam().getName(); + } + } + + @Test + public void queryHint() throws Exception{ + memberRepository.save(new Member("member1",10)); + em.flush(); + em.clear(); + + Member member=memberRepository.findReadOnlyByUsername("member1"); + member.setUsername("member2"); + + em.flush(); + } + + @Test + public void custom(){ + List result=memberRepository.findMemberCustom(); + } + + + @Test + public void jpaEventBaseEntity() throws Exception{ + Member member=new Member("member1"); + memberRepository.save(member); + + Thread.sleep(100); + member.setUsername("member2"); + + em.flush(); + em.clear(); + + Member findMember=memberRepository.findById(member.getId()).get(); + + System.out.println("findMember.createdDate = "+findMember.getCreatedDate()); + System.out.println("findMember.updatedDate = "+findMember.getLastModifiedDate()); + System.out.println("findMember.createdBy = "+findMember.getCreatedBy()); + System.out.println("findMember.updatedModifiedBy = "+findMember.getLastModifiedBy()); + + + } + @Test + public void nativeQuery(){ + Team teamA = new Team("teamA"); + em.persist(teamA); + + Member m1=new Member("m1",0,teamA); + Member m2=new Member("m2",0,teamA); + + em.persist(m1); + em.persist(m2); + + em.flush(); + em.clear(); + + int age=memberRepository.findByNativeQuery("m1"); + System.out.println("result= "+age); + + + + + } + + + +} \ No newline at end of file