๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Java·๏ปฟServlet·๏ปฟJSP/Library·Framework

[JPA/Hibernate] persistence.xml ์„ค์ • ๋ฐฉ๋ฒ•

by Leica 2020. 3. 19.
๋ฐ˜์‘ํ˜•

[JPA/Hibernate] persistence.xml ์„ค์ • ๋ฐฉ๋ฒ•

JPA 2.1
hibernate-entitymanager 4.3.10.Final

JPA๋Š” persistence.xml์„ ์‚ฌ์šฉํ•ด์„œ ํ•„์š”ํ•œ ์„ค์ • ์ •๋ณด๋ฅผ ๊ด€๋ฆฌํ•œ๋‹ค.

 

ํด๋ž˜์Šค ํŒจ์Šค์˜ META-INF/persistence.xml ์— ์ƒ์„ฑํ•˜๋ฉด ๋ณ„๋„์˜ ์„ค์ • ์—†์ด JPA๊ฐ€ ์ธ์‹ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

ํ…œํ”Œ๋ฆฟ

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
             version="2.1">
    <persistence-unit name="simple-jpa-application">
        <properties>
            <!-- ์†์„ฑ ์„ค์ • -->
        </properties>
    </persistence-unit>
</persistence>

persistence.xml์€ <persistence>๋กœ ์‹œ์ž‘ํ•˜๋ฉฐ xmlns์— ๋„ค์ž„ ์ŠคํŽ˜์ด์Šค, version์— ์‚ฌ์šฉํ•  JPA ๋ฒ„์ „์„ ์ง€์ •ํ•œ๋‹ค.

<persistence-unit>์˜ name์— unit์˜ ๊ณ ์œ ํ•œ ์ด๋ฆ„์„ ์„ค์ •ํ•œ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ์—ฐ๊ฒฐํ•  ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋‹น ํ•˜๋‚˜์˜ persistence unit์„ ๋“ฑ๋กํ•œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  <properties> ์•„๋ž˜์— JPA ํ‘œ์ค€ ์†์„ฑ๊ณผ ํ•˜์ด๋ฒ„๋„ค์ดํŠธ ์†์„ฑ์„ ์„ค์ •ํ•œ๋‹ค.

 

ํ•„์ˆ˜ ์†์„ฑ

์ผ๋ฐ˜์ ์œผ๋กœ JPA๋ฅผ ์‚ฌ์šฉํ• ๋•Œ ์„ค์ •ํ•ด์•ผํ•  ํ•„์ˆ˜ ์†์„ฑ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

 

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
             version="2.1">
    <persistence-unit name="simple-jpa-application">
        <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"/>
        </properties>
    </persistence-unit>
</persistence>

 

name์ด javax.persistence๋กœ ์‹œ์ž‘ํ•˜๋Š” ์†์„ฑ์€ JPA ํ‘œ์ค€ ์†์„ฑ์ด๋‹ค.

hibernate๋กœ ์‹œ์ž‘ํ•˜๋Š” ์†์„ฑ์€ ํ•˜์ด๋ฒ„๋„ค์ดํŠธ ์ „์šฉ ์†์„ฑ์ด๋‹ค.

javax.persistence.jdbc.driver, user, password, url์— ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๊ฒฐ ์ •๋ณด๋ฅผ ์„ค์ •ํ•˜๊ณ  hirbernate.dialect์— ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค dialect๋ฅผ ์„ค์ •ํ•œ๋‹ค.

์œ„์—์„œ ์„ค์ •ํ•œ ๊ฐ’์ธ org.hibernate.dialect.H2Dialect๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค dialect๋ฅผ H2๋กœ ์„ค์ •ํ•œ ๊ฒƒ์ด๋‹ค.

ํ•˜์ด๋ฒ„๋„ค์ดํŠธ๋Š” ๋‹ค์–‘ํ•œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ dialect ํด๋ž˜์Šค๋ฅผ ์ œ๊ณตํ•˜๋ฏ€๋กœ ์‚ฌ์šฉํ•˜๋Š” DB์˜ dialect ํด๋ž˜์Šค๋ฅผ ์„ค์ •ํ•œ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด Oracle 10g๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด org.hibernate.dialect.Oracle10gDialect๋กœ ์„ค์ •ํ•˜๋ฉด ๋œ๋‹ค.

 

์˜ต์…˜

<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
             version="2.1">
    <persistence-unit name="simple-jpa-application">
        <properties>
            <!-- ํ•„์ˆ˜ ์†์„ฑ -->
            ...


            <!-- ์˜ต์…˜ -->
            <!-- ์ฝ˜์†”์— ํ•˜์ด๋ฒ„๋„ค์ดํŠธ๊ฐ€ ์‹คํ–‰ํ•˜๋Š” SQL๋ฌธ ์ถœ๋ ฅ -->
            <property name="hibernate.show_sql" value="true"/>
            <!-- SQL ์ถœ๋ ฅ ์‹œ ๋ณด๊ธฐ ์‰ฝ๊ฒŒ ์ •๋ ฌ -->
            <property name="hibernate.format_sql" value="true"/>
            <!-- ์ฟผ๋ฆฌ ์ถœ๋ ฅ ์‹œ ์ฃผ์„(comments)๋„ ํ•จ๊ป˜ ์ถœ๋ ฅ -->
            <property name="hibernate.use_sql_comments" value="true"/>
            <!-- JPA ํ‘œ์ค€์— ๋งž์ถ˜ ์ƒˆ๋กœ์šด ํ‚ค ์ƒ์„ฑ ์ „๋žต ์‚ฌ์šฉ -->
            <property name="hibernate.id.new_generator_mappings" value="true"/>
            <!-- ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์‹คํ–‰ ์‹œ์ ์— ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํ…Œ์ด๋ธ” ์ž๋™ ์ƒ์„ฑ -->
            <property name="hibernate.hbm2ddl.auto" value="create"/>
            <!-- ์ด๋ฆ„ ๋งคํ•‘ ์ „๋žต ์„ค์ • - ์ž๋ฐ”์˜ ์นด๋ฉœ ํ‘œ๊ธฐ๋ฒ•์„ ํ…Œ์ด๋ธ”์˜ ์–ธ๋”์Šค์ฝ”์–ด ํ‘œ๊ธฐ๋ฒ•์œผ๋กœ ๋งคํ•‘
             ex) lastModifiedDate -> last_modified_date -->
            <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/>
        </properties>
    </persistence-unit>
</persistence>

 

ํ‘œ - hibernate.hbm2ddl.auto ์†์„ฑ

์˜ต์…˜ ์„ค๋ช…
create DROP + CREATE
๊ธฐ์กด ํ…Œ์ด๋ธ”์„ ์‚ญ์ œํ•˜๊ณ  ์ƒˆ๋กœ ์ƒ์„ฑํ•œ๋‹ค.
create-drop DROP + CREATE + DROP
create ์†์„ฑ์— ์ถ”๊ฐ€๋กœ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์ข…๋ฃŒํ•  ๋•Œ ์ƒ์„ฑํ•œ DDL์„ ์ œ๊ฑฐํ•œ๋‹ค.
update ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํ…Œ์ด๋ธ”๊ณผ ์—”ํ‹ฐํ‹ฐ ๋งคํ•‘์ •๋ณด๋ฅผ ๋น„๊ตํ•ด์„œ ๋ณ€๊ฒฝ ์‚ฌํ•ญ๋งŒ ์ˆ˜์ •ํ•œ๋‹ค.
validate ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํ…Œ์ด๋ธ”๊ณผ ์—”ํ‹ฐํ‹ฐ ๋งคํ•‘์ •๋ณด๋ฅผ ๋น„๊ตํ•ด์„œ ์ฐจ์ด๊ฐ€ ์žˆ์œผ๋ฉด ๊ฒฝ๊ณ ๋ฅผ ๋‚จ๊ธฐ๊ณ  ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์‹คํ–‰ํ•˜์ง€ ์•Š๋Š”๋‹ค.
์ด ์˜ต์…˜์€ DDL์„ ์ˆ˜์ •ํ•˜์ง€ ์•Š๋Š”๋‹ค.
none ์Šคํ‚ค๋งˆ ์ž๋™ ์ƒ์„ฑ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค.
hibernate.hbm2ddl.auto ์†์„ฑ์„ ์‚ญ์ œํ•œ ๊ฒƒ๊ณผ ๋™์ผํ•˜๋‹ค.
๐Ÿ“ ์ถ”์ฒœ ์ „๋žต

๊ฐœ๋ฐœ ์ดˆ๊ธฐ ๋‹จ๊ณ„ : create ๋˜๋Š” update
ํ…Œ์ŠคํŠธ ์„œ๋ฒ„ : update ๋˜๋Š” validate
์šด์˜ ์„œ๋ฒ„ : validate ๋˜๋Š” none

 

Entity Class(์—”ํ‹ฐํ‹ฐ ํด๋ž˜์Šค) ์„ค์ •

JPA ๊ตฌํ˜„์ฒด๋“ค์€ ๋ณดํ†ต ์—”ํ‹ฐํ‹ฐ ํด๋ž˜์Šค๋ฅผ ์ž๋™์œผ๋กœ ์ธ์‹ํ•˜์ง€๋งŒ ํ™˜๊ฒฝ์— ๋”ฐ๋ผ ์ธ์‹ํ•˜์ง€ ๋ชปํ•  ๋•Œ๋„ ์žˆ๋‹ค. ๊ทธ๋•Œ๋Š” <class>์— ์—”ํ‹ฐํ‹ฐ ํด๋ž˜์Šค๋ฅผ ์„ค์ •ํ•ด์ค€๋‹ค.

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
             version="2.1">
    <persistence-unit name="simple-jpa-application">
        <!-- ์—”ํ‹ฐํ‹ฐ ํด๋ž˜์Šค -->
        <class>com.atoz_develop.Member</class>
        
        <properties>
            <!-- ํ•„์ˆ˜ ์†์„ฑ -->
            ...

 

References

๐Ÿ“– ์ž๋ฐ” ORM ํ‘œ์ค€ JPA ํ”„๋กœ๊ทธ๋ž˜๋ฐ(์—์ด์ฝ˜, 2015, ๊น€์˜ํ•œ)

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€