데이터베이스 7

[DB / Error] @DataJpaTest 사용 시 org.springframework.beans.factory.UnsatisfiedDependencyException 에러

SpringBoot + Jpa + In-memory h2 환경에서 간단한 레포지토리 테스트를 수행하던 중 Bean 의존성 문제가 발생했다.에러더보기org.springframework.beans.factory.UnsatisfiedDependencyException:Error creating bean with name 'QueryDslEventRepositoryTest':Unsatisfied dependency expressed through field 'jf':No qualifying bean of type 'cohttp://m.querydsl.jpa.impl.JPAQueryFactory' available:expected at least 1 bean which qualifies as autowire ca..

데이터베이스 2024.11.26

[DB / 테스트] @DataJpaTest

@DataJpaTest는 JPA, 데이터베이스 관련 설정만 최소한으로 로드하여 레포지토리/엔티티를 테스트하는 어노테이션이다.  임베디드 데이터베이스를 자동 구성해주기 때문에 외부 데이터베이스를 사용하지 않아도 된다.Application Context에 JPA, 데이터베이스 관련 클래스들을 자동으로 로드해준다.테스트가 끝날 때마다 데이터베이스 상태가 자동으로 rollback된다. 위 내용에 대해 더 자세히 알아보자!1. 기본적으로 In-memory Embedded Database H2를 사용@DataJpaTest는 테스트 실행 시 In-memory Embedded Database H2를 기본적으로 사용한다. 임베디드 데이터베이스는 애플리케이션 외부의 데이터베이스(MySQL 등) 없이 테스트 실행 중 메모리..

데이터베이스 2024.11.26

[DB / H2] spring.jpa.properties.hibernate.globally_quoted_identifiers=true 설정

설명Spring Boot에서 embadded database(h2 등)을 쓸 때 ddl-auto를 사용하면 hibernate가 자동으로 쿼리를 만들어준다. 이때 설정으로 spring.jpa.properties.hibernate.globally_quoted_identifiers=true를 켜두면 hibernate가 ddl, dml을 생성할 때 테이블명/컬럼명을 백틱(`) 또는 쌍따옴표(")로 감싸서 생성한다. (dbms에 따라 사용되는 기호는 다름) ddl은 ddl-auto 옵션(update, create 등)에 따라 애플리케이션이 처음 실행될 때 한 번만 실행된다. hibernate가 스키마를 생성하거나 수정할 때 사용되는 명령어이다. dml은 entity mananger의 영속성 컨텍스트에서 엔티티의 ..

데이터베이스 2024.11.26

[DB / Error] Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Syntax error in SQL statement "\000d\000a

Spring Boot + JPA + In-memory h2 환경에서 레포지토리 테스트 중 다음과 같은 에러 발생에러더보기더보기Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Syntax error in SQL statement "\000d\000a    create table [*]user (\000d\000a        user_id bigint not null auto_increment,\000d\000a        birth date,\000d\000a        created_date datetime(6),\000d\000a        email varchar(255),\000d\000a        gender enum ('M','W'),\00..

데이터베이스 2024.11.25

[DB] Error / engine[*]=InnoDB"; expected "identifier";]

Spring Boot 환경에서 In-memory h2를 사용하던 중 에러가 발생했다.에러더보기    create table `bookmark` (         `bookmark_id` integer not null auto_increment,         `created_date` varchar(255) not null,         `last_modified_date` varchar(255) not null,         `name` varchar(255),         `user_id` bigint,         primary key (`bookmark_id`)     ) engine=InnoDB     create table `bookmark` (         `bookmark_id` ..

데이터베이스 2024.11.24

[DB / MySQL] Index의 동작 원리

인덱스데이터베이스 개체(테이블, 인덱스, 뷰, 스토어드 프로시저, 트리거) 중 하나데이터를 조회할 때 결과를 빠르게 찾는 데 쓰인다.인덱스 없이 특정 데이터를 검색할 경우, 테이블 전체를 검사해야 한다.-- customer 테이블의 customer_name 열에 인덱스를 지정CREATE INDEX idx_customer_name ON customer(customer_name);-- 인덱스를 통해 결과를 찾음SELECT * FROM customer WHERE customer_name = "Kim"; 인덱스의 동작 원리* MySQL Database의 B-Tree 구조를 기준으로 설명IndexClustered Index : MySQL이 자동으로 설정하는 index최대 효율을 위해 중복이 최대한 발생하지 않는 ..

데이터베이스 2024.07.11

[DB / 트랜잭션] Transaction /Spring @Transactional 사용법

이제야 정리하는 트랜잭션! Java Spring에서 트랜잭션을 어떻게 사용하는지 정리해보았다.  글을 쓰면서 @Transaction의 사용 위치, 격리성 문제 등 더 알아가야 할 내용이 많이 보여 다른 포스팅으로 돌아와야할 것 같다. 사실 테스트 코드에 트랜잭션을 적용해보고싶어서 방법을 찾던 와중에 이에 대한 많은 고민과 의견들이 있길래 흥미롭게 이것 저것 찾아보고 정리해보려고 했는데, 이해가 잘 가지 않아 힘들었다. 트랜잭션 자체에 대해서 아무것도 모르니 일단 기초부터 공부해보자!!트랜잭션애플리케이션에서 어떤 기능이 동작할 때 여러 개의 쿼리가 실행될 수 있다. 이 경우 모든 쿼리가 다 에러 없이 실행되어야 그 기능이 정상동작했다고 말할 수 있다. 트랜잭션은 트랜잭션 범위에서 실행된 모든 쿼리가 다 에..

데이터베이스 2024.05.16