JPA란?
2024. 12. 1. 20:21ㆍSpring Boot
Java Persistence API는 자바에서 ORM 기술 표준으로 사용되는 인터페이스 모음이다.
주로 사용할 때의 구현체는 Hibernate가 있다.
기본적으로 DB와 자바간의 통신은 JDBC를 이용해 이루어진다
하지만 그냥 JDBC를 사용하면 불편하므로 객체와 DB의 매핑 ORM이라는 개념이 생겨났고
이를 JPA를 통해 구현한다.
JPA는 기본적으로 EntityManager을 통해 작동하는데
이는 EntityManagerFactory를 통해 만들어지고
이 EntityManagerFactory는 TransactionManager에 등록되어 있다.
EntityManager는 데이터 베이스 연산과 영속성 관리를 담당하는데

기본적으로 Entity가 만들어지면 비영속 상태이다.
이 상태에서 EntityManager을 통해 Persit가 진행되면 영속성 컨텍스트가 된다.
이때 부터는 Entity는 1차 캐시 동일성 보장 변경 감지등의 기능이 작동한다.
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import java.util.*;
@Configuration
@EnableJpaRepositories(
basePackages = "com.example.demo.imageHandling",
entityManagerFactoryRef = "imageManagerFactory",
transactionManagerRef = "imageTransactionManager"
)
@PropertySource(value = {"classpath:imageDB.properties"})
public class imageDBConfig {
@Value("${image_url}")
private String url;
@Value("${image_name}")
private String name;
@Value("${image_password}")
private String password;
@Primary
@Bean
public DataSource imageSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
dataSource.setUrl(url);
dataSource.setUsername(name);
dataSource.setPassword(password);
return dataSource;
}
@Primary
@Bean
public LocalContainerEntityManagerFactoryBean imageManagerFactory() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(imageSource());
em.setPackagesToScan(new String[] {"com.example.demo.imageHandling"});
HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
// adapter.setShowSql(false);
adapter.setGenerateDdl(true);
em.setJpaVendorAdapter(adapter);
HashMap<String, Object> prop = new HashMap<>();
prop.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
prop.put("hibernate.hbm2ddl.auto", "update");
//prop.put("hibernate.format_sql", true);
em.setJpaPropertyMap(prop);
return em;
}
@Primary
@Bean
public PlatformTransactionManager imageTransactionManager() {
JpaTransactionManager manager = new JpaTransactionManager();
manager.setEntityManagerFactory(imageManagerFactory().getObject());
return manager;
}
}
'Spring Boot' 카테고리의 다른 글
| Spring Boot Exception Handling (0) | 2024.12.17 |
|---|---|
| Spring Data DTO (0) | 2024.12.01 |
| JPA Paging (0) | 2024.11.28 |
| JPQL (0) | 2024.11.28 |
| Spring Cloud Eureka (0) | 2024.10.29 |