2019. 3. 16. 21:47ㆍ[정리] 기능별 개념 정리/JPA
persistence.xml
JPA의 설정을 관리하는 파일
Spring boot에서는 application.properties 또는 application.yml 으로 관리한다.
H2와 JPA를 연결하는 예시 |
javax.persistence.jdb.driver = org.h2.Driver javax.persistence.jdb.url= jdbc:h2:tcp://localhost/~/test javax.persistence.jdb.user= javax.persistence.jdb.password = hibernate.dialect = org.hibernate.dialect.H2Dialect hibernate.show_sql = true hibernate.format_sql = true hibernate.use_sql_comments = true hibernate.id.new_generator_mappings = true hibernate.hbm2ddl.auto = create-drop // 데이터 베이스 DDL 자동 생성하기 * 테스트할 때만 사용할 것 |
@Entity public class Member { @Id private long id; private String name; private String age; // ... getter setter는 생략 } |
Create |
public class Main{ public static void main(String[] args){ EntityManagerFactory emf = Persistence.createEntityManagerFactory("persistent.xml에 등록된 name"); EntityManager em = emf.createEntityManager(); EntityTransaction tx = em.getTransaction(); tx.begin(); Member member = new Member(); member.setId(1L); member.setName("kok202"); member.setAge(99); em.persist(); tx.commit(); em.close(); emf.close(); } } |
Read |
public class Main{ public static void main(String[] args){ EntityManagerFactory emf = Persistence.createEntityManagerFactory("persistent.xml에 등록된 name"); EntityManager em = emf.createEntityManager(); EntityTransaction tx = em.getTransaction(); tx.begin(); Member member = em.find(Member.class, 1L); tx.commit(); em.close(); emf.close(); } } |
Update |
public class Main{ public static void main(String[] args){ EntityManagerFactory emf = Persistence.createEntityManagerFactory("persistent.xml에 등록된 name"); EntityManager em = emf.createEntityManager(); EntityTransaction tx = em.getTransaction(); tx.begin(); Member member = em.find(Member.class, 1L); member.setName("kok303"); tx.commit(); em.close(); emf.close(); } } |
Delete |
public class Main{ public static void main(String[] args){ EntityManagerFactory emf = Persistence.createEntityManagerFactory("persistent.xml에 등록된 name"); EntityManager em = emf.createEntityManager(); EntityTransaction tx = em.getTransaction(); tx.begin(); Member member = em.find(Member.class, 1L); em.remove(member); tx.commit(); em.close(); emf.close(); } } |
'[정리] 기능별 개념 정리 > JPA' 카테고리의 다른 글
JPA 양방향 맵핑, Test 코드 작성시 유의사항 (0) | 2019.04.25 |
---|---|
JPA - Mapped By 의 필요성, '관계의 주인' 의미 (2) | 2019.04.25 |
JPA (기초 : 영속성 컨텍스트) (0) | 2019.03.17 |
JPA (기초 : 엔티티 Annotation) (0) | 2019.03.16 |
JPA (기초 : ORM 의 역사) (0) | 2019.03.16 |