๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๊ณต๋ถ€/ORM,JPA

[JPA] JPA ์‹œ์ž‘, ํ”„๋กœ์ ํŠธ ์„ค์ • ๋ฐ H2,JPA ์„ค์ •

๐Ÿ“Œ ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ

> H2 ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์„ค์น˜

H2 Database Engine

 

H2 Database Engine

H2 Database Engine Welcome to H2, the Java SQL database. The main features of H2 are: Very fast, open source, JDBC API Embedded and server modes; in-memory databases Browser based Console application Small footprint: around 2 MB jar file size     Suppor

www.h2database.com

 

์„ค์น˜๊ฐ€ ์‹คํŒจํ•˜๋ฉด 8082 ํฌํŠธ๊ฐ€ ์‚ฌ์šฉ์ค‘์ธ์ง€ ํ™•์ธํ•ด ๋ณด์ž. ๋‚ด ๊ฒฝ์šฐ์—๋Š” 8082 ํฌํŠธ๊ฐ€ ์‚ฌ์šฉ์ค‘์ด์—ฌ์„œ ์„ค์น˜๊ฐ€ ์•ˆ๋๋‹ค.

// ํฌํŠธ๊ฐ€ ํ™•์ธ
netstat -na | findstr ํฌํŠธ๋ฒˆํ˜ธ 

//ํฌํŠธ์˜ PID ํ™•์ธ
netstat -nao | findstr ํฌํŠธ

// ํ”„๋กœ์„ธ์Šค ID ์ฐพ๊ธฐ 
tasklist | findstr PID๋ฒˆํ˜ธ

// ํ”„๋กœ์„ธ์Šค ์ฃฝ์ด๊ธฐ 
taskkill /f /pid PID๋ฒˆํ˜ธ

 

1. Embedded๋กœ ์—ฐ๊ฒฐํ•ด dbํŒŒ์ผ์„ ๋งŒ๋“ ๋‹ค. ์‚ฌ์šฉ์ž ๊ณ„์ •๋””๋ ‰ํ† ๋ฆฌ์— test.mv.db ํŒŒ์ผ์ด ์ƒ์„ฑ ๋œ ๊ฒƒ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

2.  test.mv.db ํŒŒ์ผ์ด ์ƒ์„ฑ  ํ›„, TCP ์†Œ์ผ“์„ ์ ‘์†ํ•œ๋‹ค. 

> ์ง์ ‘ TCP ์†Œ์ผ“์œผ๋กœ ์—ฐ๊ฒฐํ•˜์ง€ ์•Š๋Š” ์ด์œ ๋Š”, h2 1.4.198๋ฒ„์ „ ์ดํ›„ ๋ณด์•ˆ ๋ฌธ์ œ๋กœ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๊ฐ€ ์ž๋™์œผ๋กœ ์ƒ์„ฑ๋˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

> ์ง์ ‘ TCP ์†Œ์ผ“์œผ๋กœ ์—ฐ๊ฒฐ์„ ์‹œ๋„ํ•˜๋ฉด Database not found, either pre-create it or allow remote  ~ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉฐ ์—ฐ๊ฒฐ์ด ๋˜์ง€ ์•Š๋Š”๋‹ค.

 

> JPA ์„ค์ •ํ•˜๊ธฐ(Maven ๊ธฐ์ค€)

- resources ์•„๋ž˜ META-INF์— persistence.xml ์„ธํŒ…

- JPA๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์•ก์„ธ์Šค๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ์ง€์†์„ฑ์„ ์ œ๊ณตํ•œ๋‹ค. persistence.xml ์— ์•ก์„ธ์Šคํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๊ธฐ์ˆ ํ•œ๋‹ค.

 

 

<?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"> 
 <properties> 
 <!-- ํ•„์ˆ˜ ์†์„ฑ --> 
 <property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/> 
 <property name="javax.persistence.jdbc.user" value="sa"/> 
 <property name="javax.persistence.jdbc.password" value=""/> 
 <property name="javax.persistence.jdbc.url" value="jdbc:h2:tcp://localhost/~/test"/> 
 <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/> 
 
 <!-- ์˜ต์…˜ --> 
 <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>

 

๐Ÿ“Œ Test

 

1. ํ…Œ์ŠคํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด, h2 database์— Member table์„ createํ•œ๋‹ค.

 

2. Java Class์—์„œ entity๋ฅผ ์ •์˜ํ•œ๋‹ค.

@Entity
public class Member {

    @Id
    private Long id;
    private String name;
    
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

 

3.  persist๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•œ๋‹ค.

public class JpaMain {
    public static void main(String[] args) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("hello"); //๋กœ๋”ฉ ์‹œ์ ์— ํ•œ๋ฒˆ์— ๋งŒ๋“ค์–ด์•ผํ•œ๋‹ค.

        EntityManager em = emf.createEntityManager(); //ํŠธ๋ Œ์ ์…˜ ๋‹จ์œ„ ๋งˆ๋‹ค ๋งŒ๋“ค์–ด์•ผํ•œ๋‹ค.
        EntityTransaction tx = em.getTransaction(); //jpa๋Š” ํŠธ๋ Œ์ ์…˜ ๋‹จ์œ„ ์•ˆ์—์„œ ์ˆ˜ํ–‰๋˜์•ผํ•œ๋‹ค.
        tx.begin();

        try {
            Member member = new Member();
            member.setId(1L);
            member.setName("HI");
            em.persist(member);

            tx.commit();
        }catch (Exception e){
            tx.rollback();
        }finally {
            em.close();
        }
        emf.close();

    }
}

 

4. ๊ฒฐ๊ณผ ํ™•์ธ

 

 

[์ฐธ๊ณ ]

- ํ…Œ์ŠคํŠธ ๋„์ค‘, persistence.xml์—์„œ ์„ค์ • ์ •๋ณด๋ฅผ ์กฐํšŒ ํ›„ persistenceUnitName์œผ๋กœ EntityMangerFatory ์ƒ์„ฑ ์‹œ  ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค. 

 

Java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException… ๊ทธ๋ฆฌ๊ณ  java.lang.ClassNotFoundException: javax.xml.bind.JAXBException…

 

[์›์ธJ

- AXB API๋Š” ์ž๋ฐ” 9๋ฒ„์ „๋ถ€ํ„ฐ๋Š” ๋” ์ด์ƒ ํฌํ•จ๋˜์ง€ ์•Š๋Š”๋‹ค. ์ž๋ฐ” 11๋ถ€ํ„ฐ๋Š” JDK์—์„œ ์™„์ „ํžˆ ์‚ญ์ œ๋Œ

 

[ํ•ด๊ฒฐ๋ฐฉ๋ฒ•]

- ํ”„๋กœ์ ํŠธ์— JAXB ๊ด€๋ จ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ํฌํ•จ

 

- maven

<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.3.0</version>
</dependency>
 

 

-gradle

compile group: 'javax.xml.bind', name: 'jaxb-api', version: '2.3.0'

 

โœ ์ฃผ์˜!

  - ์—”ํ‹ฐํ‹ฐ ๋งค๋‹ˆ์ € ํŒฉํ† ๋ฆฌ๋Š” ํ•˜๋‚˜๋งŒ ์ƒ์„ฑํ•ด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ „์ฒด์—์„œ ๊ณต์œ ํ•ด์•ผํ•œ๋‹ค

  - ์—”ํ‹ฐํ‹ฐ ๋งค๋‹ˆ์ €๋Š” ์“ฐ๋ ˆ๋“œ๊ฐ„์— ๊ณต์œ ํ•˜๋ฉด ์•ˆ๋œ๋‹ค.

  - JPA์˜ ๋ชจ๋“  ๋ฐ์ดํ„ฐ ๋ณ€๊ฒฝ์€ ํŠธ๋žœ์žญ์…˜ ์•ˆ์—์„œ ์‹คํ–‰ํ•ด์•ผํ•œ๋‹ค.

 

 

 

REFERENCE


https://www.inflearn.com/course/ORM-JPA-Basic/dashboard

 

์ž๋ฐ” ORM ํ‘œ์ค€ JPA ํ”„๋กœ๊ทธ๋ž˜๋ฐ - ๊ธฐ๋ณธํŽธ - ์ธํ”„๋Ÿฐ | ๊ฐ•์˜

JPA๋ฅผ ์ฒ˜์Œ ์ ‘ํ•˜๊ฑฐ๋‚˜, ์‹ค๋ฌด์—์„œ JPA๋ฅผ ์‚ฌ์šฉํ•˜์ง€๋งŒ ๊ธฐ๋ณธ ์ด๋ก ์ด ๋ถ€์กฑํ•˜์‹  ๋ถ„๋“ค์ด JPA์˜ ๊ธฐ๋ณธ ์ด๋ก ์„ ํƒ„ํƒ„ํ•˜๊ฒŒ ํ•™์Šตํ•ด์„œ ์ดˆ๋ณด์ž๋„ ์‹ค๋ฌด์—์„œ ์ž์‹ ์žˆ๊ฒŒ JPA๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค., ๋ณธ ๊ฐ•์˜๋Š” ์ž๋ฐ” ๋ฐฑ์—”

www.inflearn.com

 

300x250

'๊ณต๋ถ€ > ORM,JPA' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[JPA] JPA๋ž€?  (0) 2021.11.22