영속성은 한글로보면 뭔소린가 싶다

Spring JPA에서 "영속성(Persistence)"은 데이터를 저장하고 유지하는 성질을 의미한다.
즉, 단어 해석처럼 엔티티를 영구적으로 저장해주는 환경!!을 의미함.
Entity 객체를 영속성 상태로 관리하는 일종의 캐시 역할을 하는 공간으로 여기에 저장된 Entity는 데이터베이스와 자동으로 동기화되며 같은 트랜잭션 내에서는 동일한 객체가 유지된다.
위는 강의에서 나온 정의
쉽게 말하면, "JPA 내부의 저장소" 라고 생각하면 된다.
JPA를 사용하면, 엔티티 객체를 직접 데이터베이스에서 가져오고, 저장하고, 수정하는 것이 아니라 영속성 컨텍스트를 통해 관리된다.
즉, 객체가 메모리에 올라와 있는 상태를 관리하는 공간이다.
트랜잭션이 끝나기 전에 같은 객체를 여러 번 조회해도 DB에 다시 조회하지 않고, 영속성 컨텍스트에서 가져올 수 있음!!
🍋Entity 객체
데이터베이스에서 Entity란 저장할 수 있는 데이터의 집합을 의미한다.
이런 영속성 컨텍스트는 4가지 상태를 가질 수 있다.

1️⃣비영속(new/transient)
2️⃣영속(managed)
3️⃣준영속(detached)
4️⃣삭제(removed)
뭐가 좋을지 고민하다가 극단적 k-게이머 관점으로 풀어봤다.
영속성 컨텍스트 = 길드
객체(Entity) = 유저
원래 길드에 소속되어 있었는데 길드에서 탈퇴함
하지만 캐릭터는 살아있고, 다시 길드에 가입할 수도 있음
✅길드에서 탈퇴했지만, 캐릭터는 그대로 존재하는 상태
❤️ 1차 캐시
🧡 동일성 보장 (Identity)
💛 쓰기 지연 (Write-behind / Delayed Write)
💚 변경 감지 (Dirty Checking)
@Entity
클래스에
@Entity가 있다면 JPA가 관리하는Entity로 만들어진다.

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.2"
xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd">
<persistence-unit name="test"> <!-- createEntityManagerFactory에서 사용할 이름 -->
<class>org.entity.Tutor</class>
<properties>
<property name="jakarta.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/>
<!-- DB 아이디 -->
<property name="jakarta.persistence.jdbc.user" value="root"/>
<!-- DB 비밀번호 -->
<property name="jakarta.persistence.jdbc.password" value="1111"/>
<!-- 스키마 -->
<property name="jakarta.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/sparta"/>
<!-- 스키마 자동생성 속성 -->
<property name="hibernate.hbm2ddl.auto" value="create" />
<!-- Hibernate가 DB에 전송하는 DDL, DML SQL을 콘솔에 출력한다. -->
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.use_sql_comments" value="true"/>
<!-- 한꺼번에 전송될 SQL 수량 설정 -->
<!-- <property name="hibernate.jdbc.batch_size" value="10"/>-->
</properties>
</persistence-unit>
</persistence>
@Entity(name = "Tutor") // 기본 값, name 속성은 생략하면 클래스 이름이 된다.
...
public class Tutor {
@Id 사용), 기본 생성자final, enum, interface, inner 클래스 / 필드에 final 키워드name@Table(name = "tutor")
public class Tutor {
}
namecatalogschemauniqueConstraints@Table(uniqueConstraints = {@UniqueConstraints
(
name = "name_unique",
columnNames= {"name"}
)
}
)
유니크 제약 조건은 이런식으로 사용함
(DDL을 자동으로 생성할 때만 사용되며 Application 로직에는 영향이 없음)