순수 JPA 써보기

jipyo park·2022년 7월 8일
0

jpa

목록 보기
1/1

의존성 설정

Maven

<dependencies>
	<dependency>
    	<groupId>org.hibernate</groupId>
        	<artifactId>hibernate-entitymanager</artifactId>
            <version>Hibernate 버전</version>
	</dependency>
	<dependency>
		<groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>h2 DB와 동일한 버전</version>
	</dependency>
</dependencies>

persistence.xml 설정

EntityManagerFactory는 persistence.xml에 있는 데이터 기준으로 DB연결 진행
persistence.xml의 위치가 다른 곳에 있으면 참조를 못함
혹시 위치에 제대로 넣었는데도 에러가 나면 javax.xml.bind가 없기 때문이다.
자바 11버전 이상부터 javax.xml.bind라이브러리가 기본 자바에서 빠져있기 때문인데 해결 솔루션은 자바 8로 낮추던가 의존성 추가를 해주어함

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

persistence 설정

<?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="persiste-unit">
        <properties>
            <!-- JDBC Driver -->
            <property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
            <!-- DB name -->
            <property name="javax.persistence.jdbc.user" value="sa"/>
            <!-- DB password -->
            <property name="javax.persistence.jdbc.password" value=""/>
            <!-- DB URL -->
            <property name="javax.persistence.jdbc.url" value="jdbc:h2:c:/dev/java/!h2_db/jpa_study"/>
            <!-- DB SQL Dialect -->
            <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
        </properties>
    </persistence-unit>
</persistence>

main code

    public static void main(String[] args) {
    	// EntityManagerFactory 생성
        /* persistence.xml에 persistence-unit에 설정한 이름으로 값을 주어야함 */
        EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("persiste-unit");

		// EntityManager 생성
        EntityManager entityManager = entityManagerFactory.createEntityManager();
		// Transaction 생성
        EntityTransaction transaction = entityManager.getTransaction();
        // Transaction 시작
        transaction.begin();

        try {
            Member member = new Member();
            member.setId(2L);
            member.setName("jipyo");

			// member 저장
            entityManager.persist(member);
			
            transaction.commit();
        } catch (Exception e) {
            transaction.rollback();
        }
        finally {
            entityManager.close();
        }

        entityManagerFactory.close();
    }

 

참고

3개의 댓글

comment-user-thumbnail
2023년 11월 29일

Meaningful connections can open up opportunities and contribute to the growth of your personal brand. Regularly monitor your online https://avarup.com/ presence by conducting a search for your name on search engines.

답글 달기
comment-user-thumbnail
2023년 11월 29일

thanks

답글 달기
comment-user-thumbnail
2023년 11월 29일

Having security guards on-site can reduce the liability of businesses in the event of security-related incidents. The proactive measures taken by security personnel demonstrate a commitment to safety california security services which can positively influence insurance premiums and legal considerations.

답글 달기