묻고 답해요
141만명의 커뮤니티!! 함께 토론해봐요.
인프런 TOP Writers
-
해결됨실전! 스프링 부트와 JPA 활용1 - 웹 애플리케이션 개발
회원기능 테스트에서 질문드립니다.
dependencies { implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-devtools' compile('com.github.gavlyukovskiy:p6spy-spring-boot-starter:1.5.7') compileOnly 'org.projectlombok:lombok' runtimeOnly 'com.h2database:h2' annotationProcessor 'org.projectlombok:lombok' testImplementation 'org.springframework.boot:spring-boot-starter-test'} spring: datasource: url: jdbc:h2:tcp://localhost/~/Dev/projects/book/db;MVCC=TRUE username: sa password: driver-class-name: org.h2.Driver jpa: hibernate: ddl-auto: create properties: hibernate: show_sql: true format_sql: truelogging: level: org.hibernate.SQL: debug org.hibernate.type: trace package com.ym.book.shop.service;import com.ym.book.shop.domain.entity.Member;import com.ym.book.shop.repository.MemberRepository;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;import org.springframework.transaction.annotation.Transactional;import static org.junit.Assert.*;@RunWith(SpringRunner.class)@SpringBootTest@Transactionalpublic class MemberServiceTest { @Autowired MemberService memberService; @Autowired MemberRepository memberRepository; @Test public void 회원가입()throws Exception{ //given Member member = new Member(); member.setName("Kim"); //when Long saveId = memberService.join(member); //then assertEquals(member, memberRepository.findOne(saveId)); } @Test public void 중복_회원_예외()throws Exception{ //given //when //then }}결과 :2019-10-06 14:52:56.074 INFO 2244 --- [ Test worker] o.h.h.i.QueryTranslatorFactoryInitiator : HHH000397: Using ASTQueryTranslatorFactory 2019-10-06 14:52:56.173 DEBUG 2244 --- [ Test worker] org.hibernate.SQL : select member0_.member_id as member_i1_4_, member0_.city as city2_4_, member0_.street as street3_4_, member0_.zipcode as zipcode4_4_, member0_.name as name5_4_ from member member0_ where member0_.name=? Hibernate: select member0_.member_id as member_i1_4_, member0_.city as city2_4_, member0_.street as street3_4_, member0_.zipcode as zipcode4_4_, member0_.name as name5_4_ from member member0_ where member0_.name=? 2019-10-06 14:52:56.204 DEBUG 2244 --- [ Test worker] org.hibernate.SQL : insert into member (member_id, city, street, zipcode, name) values (null, ?, ?, ?, ?) Hibernate: insert into member (member_id, city, street, zipcode, name) values (null, ?, ?, ?, ?) 2019-10-06 14:52:56.216 INFO 2244 --- [ Test worker] o.s.t.c.transaction.TransactionContext : Rolled back transaction for test: [DefaultTestContext@4d40c3e testClass = MemberServiceTest, testInstance = com.ym.book.shop.service.MemberServiceTest@b322034, testMethod = 회원가입@MemberServiceTest, testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@1ee9b049 testClass = MemberServiceTest, locations = '{}', classes = '{class com.ym.book.shop.ShopApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@275315df, org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@47bf348f, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@7db5eaa6, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@2cda0ac3], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.web.ServletTestExecutionListener.activateListener' -> true, 'org.springframework.test.context.web.ServletTestExecutionListener.populatedRequestContextHolder' -> true, 'org.springframework.test.context.web.ServletTestExecutionListener.resetRequestContextHolder' -> true]] 2019-10-06 14:52:56.223 INFO 2244 --- [ Thread-6] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor' 2019-10-06 14:52:56.223 INFO 2244 --- [ Thread-6] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default' 2019-10-06 14:52:56.226 INFO 2244 --- [ Thread-6] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated... 2019-10-06 14:52:56.231 INFO 2244 --- [ Thread-6] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed. BUILD SUCCESSFUL in 5s 5 actionable tasks: 3 executed, 2 up-to-date 2:52:56 PM: Tasks execution finished ':cleanTest :test --tests "com.ym.book.shop.service.MemberServiceTest.회원가입"'.이렇게 결과가 나옵니다.select, insert가 2번씩 실행이 되는데요혹시 제가 설정이 잘못된 게 있을까요?
-
해결됨실전! 스프링 부트와 JPA 활용1 - 웹 애플리케이션 개발
DB Insert할 때 질문입니다.
회원 서비스 개발에서 validateDuplicateMember를 할 때에 동시에 회원가입이 이루어질 때 방지로 name에 유니크를 걸어 주신다고 했는데 이해가 잘 되지를 않습니다. 보통 이름이 같은 사람도 많은데 name 에 unique를 주어도 되는건지 궁금하고요. 동시에 일어났을 때 방지하는 법에 대해 좀 더 자세히 알고 싶습니다~
-
미해결실전! 스프링 부트와 JPA 활용1 - 웹 애플리케이션 개발
@ManyToOne, @OneToMany연관관계 조회 시 순환참조가 일어나는 부분은 어떻게 해결하여야 하나요?
안녕하세요!다름이아니라 제가 수업을 따라해보던 중 @ManyToOne, @OneToMany으로 연관관계를 맺은 두개의 테이블을 조회 시 아래와 같은 에러를 확인하였습니다.@Entity @Table(name = "MEMBER") @Getter @Setter public class Member { @Id @GeneratedValue @Column(name = "MEMBER_ID") Long id; @Column(name = "NAME") String name; @OneToMany(mappedBy = "member") List<Post> posts = new ArrayList<>(); } @Entity @Table(name = "POST") @Getter @Setter public class Post { @Id @GeneratedValue @Column(name = "POST_ID") Long id; String title; String description; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "MEMBER_ID") Member member; }Could not write JSON: Infinite recursion (StackOverflowError);제 생각에는 한쪽에서 다른쪽 테이블을 조회 시 순환참조가 일어나는것 같다고 생각하는데 이러한 경우에는 어떻게 처리를 하여야 하는지 궁금합니다.찾아보니 @JsonIgnore를 사용하는 방법이 있는것 같기는한데 올바른 방법이 아닌것 같아서 여쭈어봅니다.좋은강의 감사합니다!
-
미해결실전! 스프링 부트와 JPA 활용1 - 웹 애플리케이션 개발
./h2.sh 실행시 퍼미션 에러가 뜨네요..^^ 참고하세요
-bash: ./h2.sh: Permission denied bin 폴더 경로에 가셔서 터미널 여시고 chmod 755 h2.sh <-- 입력하시면 됩니다. 참고로 터미널에서는 h2 DB 종료 단축키는 control + c 입니다.