@Entity
package com.example.practice5.jpa.model;
import jakarta.persistence.*;
import lombok.*;
import java.time.LocalDateTime;
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Data
@Entity
public class Member {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", updatable = false)
private Long id;
@NonNull
private String name;
private String email;
private LocalDateTime createAt;
private LocalDateTime updateAt;
}
application.yml 파일
jpa:
show-sql: true
defer-datasource-initialization: ture
properties:
hibernate:
format_sql: true
data.sql 파일
insert into member(`id`, `name`, `email`, `create_at`, `update_at`) values (1, '홍길동', 'mars@thejoeun.com', now(), now());
insert into member(`id`, `name`, `email`, `create_at`, `update_at`) values (2, '박남순', 'namsun@thejoeun.com', now(), now());
insert into member(`id`, `name`, `email`, `create_at`, `update_at`) values (3, '이순신', 'leesunsin@thejoeun.com', now(), now());
insert into member(`id`, `name`, `email`, `create_at`, `update_at`) values (4, '강감찬', 'kangkamchan@thejoeun.com', now(), now());
insert into member(`id`, `name`, `email`, `create_at`, `update_at`) values (5, '유관순', 'ryukwansun@thejoeun.com', now(), now());
package com.example.practice5.jpa.repository;
import com.example.practice5.jpa.model.Member;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface MemberRepository extends JpaRepository<Member, Long> {
}
package com.example.practice5.jpa;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@SpringBootApplication
@EnableJpaRepositories
public class JpaTestApplication {
public static void main(String[] args) {
SpringApplication.run(JpaTestApplication.class, args);
}
}
package com.example.practice5.jpa.repository;
import com.example.practice5.jpa.model.Member;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest
class MemberRepositoryTest {
@Autowired
private MemberRepository memberRepository;
@Test
void crud() {
// select 문
System.out.println("-----------------------------------------------------");
List<Member> memberList = memberRepository.findAll();
for(Member member : memberList) {
System.out.println(member.toString());
}
}
}
// System.out.println(member.toString());
// }
// jdk 1.8 에서 사용된 stream 기술을 이용한 print 찍는 방법
memberList.forEach(System.out::println);
console 결과 (일부)
console 결과 (전체)
> Task :compileJava UP-TO-DATE
> Task :processResources
> Task :classes
> Task :compileTestJava
> Task :processTestResources NO-SOURCE
> Task :testClasses
> Task :test
17:39:58.078 [Test worker] INFO org.springframework.test.context.support.AnnotationConfigContextLoaderUtils -- Could not detect default configuration classes for test class [com.example.practice5.jpa.repository.MemberRepositoryTest]: MemberRepositoryTest does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
17:39:58.199 [Test worker] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper -- Found @SpringBootConfiguration com.example.practice5.jpa.JpaTestApplication for test class com.example.practice5.jpa.repository.MemberRepositoryTest
_ _ ___ _ _ _ __ __ _ ___ _ _ _ __
| | | |/ _ \| | | | '_ \ / _` |/ _ \ | | | '_ \
| |_| | (_) | |_| | | | | (_| | __/ |_| | | | |
\__, |\___/ \__,_|_| |_|\__, |\___|\__,_|_| |_|
|___/ |___/
2023-07-07T17:39:58.809+09:00 INFO 7084 --- [ Test worker] c.e.p.j.repository.MemberRepositoryTest : Starting MemberRepositoryTest using Java 17.0.7 with PID 7084 (started by tj in C:\Users\tj\Documents\rest-api-practice)
2023-07-07T17:39:58.812+09:00 INFO 7084 --- [ Test worker] c.e.p.j.repository.MemberRepositoryTest : No active profile set, falling back to 1 default profile: "default"
2023-07-07T17:39:59.508+09:00 INFO 7084 --- [ Test worker] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2023-07-07T17:39:59.574+09:00 INFO 7084 --- [ Test worker] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 56 ms. Found 1 JPA repository interfaces.
2023-07-07T17:40:00.355+09:00 INFO 7084 --- [ Test worker] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2023-07-07T17:40:00.592+09:00 INFO 7084 --- [ Test worker] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Added connection conn0: url=jdbc:h2:mem:testdb user=SA
2023-07-07T17:40:00.595+09:00 INFO 7084 --- [ Test worker] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2023-07-07T17:40:00.698+09:00 INFO 7084 --- [ Test worker] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2023-07-07T17:40:00.867+09:00 INFO 7084 --- [ Test worker] org.hibernate.Version : HHH000412: Hibernate ORM core version 6.2.5.Final
2023-07-07T17:40:00.870+09:00 INFO 7084 --- [ Test worker] org.hibernate.cfg.Environment : HHH000406: Using bytecode reflection optimizer
2023-07-07T17:40:01.099+09:00 INFO 7084 --- [ Test worker] o.h.b.i.BytecodeProviderInitiator : HHH000021: Bytecode provider name : bytebuddy
2023-07-07T17:40:01.293+09:00 INFO 7084 --- [ Test worker] o.s.o.j.p.SpringPersistenceUnitInfo : No LoadTimeWeaver setup: ignoring JPA class transformer
2023-07-07T17:40:01.591+09:00 INFO 7084 --- [ Test worker] o.h.b.i.BytecodeProviderInitiator : HHH000021: Bytecode provider name : bytebuddy
2023-07-07T17:40:02.317+09:00 INFO 7084 --- [ Test worker] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
Hibernate:
drop table if exists member cascade
Hibernate:
create table member (
create_at timestamp(6),
id bigint generated by default as identity,
update_at timestamp(6),
email varchar(255),
name varchar(255),
primary key (id)
)
2023-07-07T17:40:02.367+09:00 INFO 7084 --- [ Test worker] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2023-07-07T17:40:02.862+09:00 WARN 7084 --- [ Test worker] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2023-07-07T17:40:03.272+09:00 INFO 7084 --- [ Test worker] o.s.b.a.h2.H2ConsoleAutoConfiguration : H2 console available at '/h2-console'. Database available at 'jdbc:h2:mem:testdb'
2023-07-07T17:40:03.347+09:00 INFO 7084 --- [ Test worker] c.e.p.j.repository.MemberRepositoryTest : Started MemberRepositoryTest in 4.944 seconds (process running for 6.756)
-----------------------------------------------------
Hibernate:
select
m1_0.id,
m1_0.create_at,
m1_0.email,
m1_0.name,
m1_0.update_at
from
member m1_0
Member(id=1, name=홍길동, email=mars@thejoeun.com, createAt=2023-07-07T17:40:03.294092, updateAt=2023-07-07T17:40:03.294092)
Member(id=2, name=박남순, email=namsun@thejoeun.com, createAt=2023-07-07T17:40:03.296087, updateAt=2023-07-07T17:40:03.296087)
Member(id=3, name=이순신, email=leesunsin@thejoeun.com, createAt=2023-07-07T17:40:03.296087, updateAt=2023-07-07T17:40:03.296087)
Member(id=4, name=강감찬, email=kangkamchan@thejoeun.com, createAt=2023-07-07T17:40:03.297085, updateAt=2023-07-07T17:40:03.297085)
Member(id=5, name=유관순, email=ryukwansun@thejoeun.com, createAt=2023-07-07T17:40:03.298081, updateAt=2023-07-07T17:40:03.298081)
Java HotSpot(TM) 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended
2023-07-07T17:40:04.196+09:00 INFO 7084 --- [ionShutdownHook] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
Hibernate:
drop table if exists member cascade
2023-07-07T17:40:04.200+09:00 WARN 7084 --- [ionShutdownHook] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 90121, SQLState: 90121
2023-07-07T17:40:04.201+09:00 ERROR 7084 --- [ionShutdownHook] o.h.engine.jdbc.spi.SqlExceptionHelper : Database is already closed (to disable automatic closing at VM shutdown, add ";DB_CLOSE_ON_EXIT=FALSE" to the db URL) [90121-214]
2023-07-07T17:40:04.202+09:00 WARN 7084 --- [ionShutdownHook] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 90121, SQLState: 90121
2023-07-07T17:40:04.202+09:00 ERROR 7084 --- [ionShutdownHook] o.h.engine.jdbc.spi.SqlExceptionHelper : Database is already closed (to disable automatic closing at VM shutdown, add ";DB_CLOSE_ON_EXIT=FALSE" to the db URL) [90121-214]
2023-07-07T17:40:04.203+09:00 WARN 7084 --- [ionShutdownHook] o.s.b.f.support.DisposableBeanAdapter : Invocation of destroy method failed on bean with name 'entityManagerFactory': org.hibernate.exception.JDBCConnectionException: Unable to release JDBC Connection used for DDL execution [Database is already closed (to disable automatic closing at VM shutdown, add ";DB_CLOSE_ON_EXIT=FALSE" to the db URL) [90121-214]] [n/a]
2023-07-07T17:40:04.203+09:00 INFO 7084 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated...
2023-07-07T17:40:04.205+09:00 INFO 7084 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.
BUILD SUCCESSFUL in 9s
4 actionable tasks: 3 executed, 1 up-to-date
오후 5:40:04: Execution finished ':test --tests "com.example.practice5.jpa.repository.MemberRepositoryTest"'.
'Spring' 카테고리의 다른 글
[스프링] data.sql / import.sql / schema.sql 차이 (0) | 2023.07.10 |
---|---|
[스프링] JPA - CRUD (0) | 2023.07.07 |
[스프링] intellij - h2 database (0) | 2023.07.07 |
[스프링] Lombok - @Data (0) | 2023.07.07 |
[스프링] Lombok - @Builder (0) | 2023.07.07 |