Search

[springboot] JPA - 1. JPA μ†Œκ°œ 및 μ‹œμž‘

νƒ€μž…
μŠ€ν„°λ””
νƒœκ·Έ
springboot
μƒνƒœ
Published
생성일
2022/08/01 13:23
μ΅œμ’… νŽΈμ§‘ μΌμ‹œ
2023/02/27 03:17
μƒμœ„ ν•­λͺ©
1 more property

JPA μ†Œκ°œ

JPA μ—μ„œ μ˜΅μ…˜μœΌλ‘œ μ§€μ—°λ‘œλ”©μœΌλ‘œ 할지, μ¦‰μ‹œλ‘œλ”©μœΌλ‘œ 할지 μ„€μ •ν•  수 μžˆλ‹€.
쿼리 μ΅œμ ν™”λ₯Ό κ³ λ―Όν•˜λ©΄μ„œ μ§€μ—°μœΌλ‘œ ν• μ‹œ, μ¦‰μ‹œλ‘œ ν• μ‹œ μ •ν•œλ‹€.

ν”„λ‘œμ νŠΈ μ„ΈνŒ…

spring initializer
Β Spring Web, MySQL Driver, JPA λ””νŽœλ˜μ‹œ μΆ”κ°€
application.yml
spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/hello-jpa?serverTimezone=Asia/Seoul username: root password: <password> jpa: show-sql: true hibernate: ddl-auto: none
YAML
볡사
resources/META-INF/persistence.xml
persistence.xml νŒŒμΌμ€ JPA λ₯Ό μ„€μ •ν•˜λŠ” νŒŒμΌμ΄λ‹€.
파일 κ²½λ‘œκΉŒμ§€ λ™μΌν•˜κ²Œ λ§žμΆ°μ€˜μ•Ό ν•œλ‹€.
<?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="hello"> <class>jpabasic.hellojpa.Member</class> <properties> <!-- ν•„μˆ˜ 속성 --> <property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/> <property name="javax.persistence.jdbc.user" value="root"/> <property name="javax.persistence.jdbc.password" value="<password>"/> <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/hello-jpa?serverTimezone=Asia/Seoul"/> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/> <!-- μ˜΅μ…˜ --> <property name="hibernate.show_sql" value="true"/> <property name="hibernate.format_sql" value="true"/> <property name="hibernate.use_sql_comments" value="true"/> <!--<property name="hibernate.hbm2ddl.auto" value="create" />--> </properties> </persistence-unit> </persistence>
XML
볡사
Β org.hibernate.dialect.MySQL5Dialect
: hibernate 에 μ˜ν•΄ JPA κ°€ MySQL 쿼리문으둜 λ³€ν™˜λœλ‹€.
Β <class>jpabasic.hellojpa.Member</class>
: Member μ—”ν‹°ν‹°λ₯Ό 인식
 사싀 Springboot λ₯Ό μ‚¬μš©ν•œλ‹€λ©΄ persistence.xml μ„€μ •νŒŒμΌ 없이 application.yml μ—μ„œ 섀정을 해쀄 수 μžˆλ‹€. persistence.xml νŒŒμΌμ€ μˆœμˆ˜ν•œ JPA 와 Hibernate λ₯Ό μ‚¬μš©ν•  λ•Œ ν•„μš”ν•œ νŒŒμΌμ΄λ‹€.
HelloJpaApplicaton.java
@SpringBootApplication public class HelloJpaApplication { public static void main(String[] args) { SpringApplication.run(HelloJpaApplication.class, args); EntityManagerFactory emf = Persistence.createEntityManagerFactory("hello"); EntityManager em = emf.createEntityManager(); // em : DB connection EntityTransaction tx = em.getTransaction(); tx.begin(); // νŠΈλžœμž­μ…˜ μ‹œμž‘ try { Member member = new Member(); member.setId(1L); member.setName("HelloA"); // INSERT em.persist(member); // SELECT Member findMember = em.find(Member.class, 1L); // findMember : JPA λ₯Ό ν†΅ν•΄μ„œ κ°€μ Έμ˜¨ κ°μ²΄λŠ” DB와 μ—°κ²°λΌμžˆλ‹€κ³  보면 됨 System.out.println("findMember.id = " + findMember.getId()); System.out.println("findMember.name = " + findMember.getName()); // JPQL 을 μ΄μš©ν•œ μ—¬λŸ¬ 개 SELECT List<Member> result = em.createQuery("select m from Member as m", Member.class) .setFirstResult(5) .setMaxResults(8) .getResultList(); for (Member mem : result) { System.out.println("member name = " + mem.getName()); } // UPDATE findMember.setName("HelloB"); // μžλ°” 객체 멀버λ₯Ό λ°”κΎΈλŠ” κ²ƒμ²˜λŸΌ λ³΄μ΄μ§€λ§Œ DB에도 반영이 됨 // DELETE //em.remove(findMember); tx.commit(); // νŠΈλžœμž­μ…˜ 컀밋 } catch (Exception e) { tx.rollback(); // νŠΈλžœμž­μ…˜ λ‘€ } finally { em.close(); } emf.close(); } }
Java
볡사
Β EntityManagerFactory 생성 β†’ EntityManager 생성 β†’ νŠΈλžœμž­μ…˜ 처리&μ™„λ£Œ β†’ EntityManager μ’…λ£Œ β†’ EntityManagerFactory μ’…λ£Œ Β μ½˜μ†” κ²°κ³Ό
1.
INSERT : persist()
2.
SELECT : find()
3.
UPDATE : setXX()
4.
DELETE : remove()