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()