본문 바로가기

728x90

JPA

(7)
[Spring Security] Spring Boot에서 Spring Security 설정하기 (Database) # 구현 과정 1) Database를 사용하여 Role에 따라 Access Control 하도록 한다. - User JPA Entity, Role JPA Entity 가 필요하다. (N:M 매핑 관계로 함) - User와 Role entity와 관계된 Spring Data JPA repository를 만든다. 2) UserDetailsService와 UserDetails를 만든다. - UserDetailsService 인터페이스에서 정의된, loadUserByUsername()를 구현한다. - UserDetails 인터페이스에 User Entity, Role Entity를 넣어주는 것이 UserDetailsService의 loadUserByUsername 메서드이다. 3) Spring Security 설..
[JPA] JPA / Hibernate Cascade Types 요약 JPA Cascade Type ALL PERSIST MERGE REMOVE REFRESH DETACH CascadeType.ALL 모든 Cascade 적용 CascadeType.PERSIST 엔티티를 영속화할 때, 연관된 엔티티도 함께 유지 CascadeType.MERGE 엔티티 상태를 병합 할 때, 이 필드에 보유 된 엔티티도 병합됨. CascadeType.REFRESH 엔티티를 새로 고칠 때, 이 필드에 보유 된 엔티티도 새로 고침. CascadeType.REMOVE 엔티티를 삭제할 때, 이 필드에 보유 된 엔티티도 삭제됨 CascadeType.DETACH 부모 엔티티가 detach()를 수행하게 되면, 연관된 엔티티도 detach() 상태가 되어 변경사항이 반영되지 않는다.
[Spring Boot] Spring Data JPA의 사용법과 사용이유, 예시 Spring Data JPA 1. Creating DAO 문제 다른 entity 를 만들때 유사한 코드의 반복. DAO 패턴이 같다는 점. 코드의 반복 entity type , primary key만 바꿔주면 다른 entity 도 다를 것이 없다. 2. Spring Data JPA 장점 틀에 박혀있는 DAO code를 최소화, DAO를 실제로 만들어줄 필요가 없다 제공되는 인터페이스 Spring DATA JPA 는 repository interfaces 를 제공한다. 1) CrudRepository 2) PagingAndSortingRepository 3) JpaRepository 4. Query Method id를 기반으로 해서 조회하기 보다는, 검색을 통해서 조회할 경우가 있다. 그 부분에 대해서는 ..
[Spring Boot] Spring에서의 Hibernate 사용법 1. Spring Framework에서 Hibernate 사용 1) Maven Dependency 추가 2) Spring Bean 설정 3) Entity Bean (with annotation) 4) DAO Layer ( Spring JDBC => Hibernate) 1) Maven Dependency 추가 org.springframework spring-orm ${org.springframework-version} org.hibernate hibernate-core 5.4.28.Final 2) hibernate configuartion (dao-context.xml) - hiberante.cfg.xml 파일을 실제로 생성할 필요가 없고, dao-context.xml에 정보를 넣어주면된다. - Confi..
[Spring Boot] Hibernate 에서 Entity Relationships 1. Entity Relationships 4가지 타입 @OneToOne @OneToMany, @ManyToOne @ManyToMany relationship의 방향성이 있다. - bidirectional (양방향) : owning side(referenece를 가지고 있는 side) and inverse side - unidirectional (일방향) : owning side only 2. Entity Relation Attributes Cascading updates/deletes - CascadeType : ALL, PERSIST, MERGE, REMOVE, REFRESH - default 값 : no operation @OneToMany(cascade = CascadeType.ALL, fetch..
[Spring boot] JPA vs Hibernate # Hibernate는 JPA의 구현체이다 - JPA는 스팩이나 인터페이스를 뜻한다. - Hibernate는 JPA의 구현체라고 보면 된다. => JPA만 가지고는 할 수 없고, 구현체가 필요하다. => JPA를 사용하기 위해 굳이 Hibernate를 사용할 필요는 없다. ex) Hibernate, EclipseLink, DataNucleus ... - 밑에 예시와 같이 인터페이스를 직접 구현한 라이브러리이다. List list = new ArrayList(); // | | // Interface Implementation - Java-persistence-api javax.persistence javax.persistence-api 2.2 - Hibernate-core org.hibernate hibe..
[Spring Boot] Hibernate 의 동작 원리 및 특징과 사용 예시 helloHibernate Hibernate 연습 Benefits of Hibernate 1) Hiberante provides the Object-to-Relational Mapping(ORM) 2) Hibernate handles all of the low-level SQL JDBC 코드의 양을 최소화 해준다. Application Architecture SessionFactory를 사용해서 session을 만들어 DB와 커뮤니케이션 한다. hibernate.cfg.xml *.hbm.xml class mappings : 객체와 테이블 사이에 매핑을 하는 부분 1) Configuration Hibernate Config (설정정보) : hibernate.cfg.xml 을 읽음 Hibernate Mapp..

728x90