persistence.xml 작성, 데이터베이스 연동 및 옵션설정
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" 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_1.xsd">
<!-- JPA 메인환결설정파일 -->
<persistence-unit name="JPA">
<class>com.himedia.domain.Board</class>
<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="1234" />
<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="false" />
<property name="hibernate.id.new_generator_mappings" value="true" />
<property name="hibernate.hbm2ddl.auto" value="create" />
</properties>
</persistence-unit>
</persistence>
@Entity, 테이블 설정 class 작성
package com.himedia.domain;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.*;
@Entity //Entity가 설정된 class, 클래스이름과 동일한 테이블과 기본매핑됨.
@Table(name = "Board2") //클래스이름과 테이블이 다를경우 이름설정, 동일할경우 생략
public class Board implements Serializable {
@Id //필수, 테이블 기본키 매핑
@GeneratedValue //@Id가 선언된 필드에 기본키값을 자동으로 할당
private Long seq;
private String title;
private String writer;
private String content;
@Temporal(TemporalType.DATE)
private Date createDate;
private Long cnt;
// private static final long serialVersionUID = 1L;
public Long getSeq() {
return seq;
}
public void setSeq(Long seq) {
this.seq = seq;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getWriter() {
return writer;
}
public void setWriter(String writer) {
this.writer = writer;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
public Long getCnt() {
return cnt;
}
public void setCnt(Long cnt) {
this.cnt = cnt;
}
@Override
public String toString() {
return "Board [seq=" + seq + ", title=" + title + ", writer=" + writer + ", content=" + content
+ ", createDate=" + createDate + ", cnt=" + cnt + "]";
}
public Board() {
super();
}
}
테이블 생성 및 글 등록
package com.himedia;
import java.util.Date;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import com.himedia.domain.Board;
public class JPAClient {
public static void main(String[] args) {
// EntityManager 생성
EntityManagerFactory emf = Persistence.createEntityManagerFactory("JPA");
EntityManager em = emf.createEntityManager();
// Transaction 생성
EntityTransaction tx = em.getTransaction();
try {
// Transaction 시작
tx.begin();
// 글 등록
Board board = new Board();
board.setTitle("JPA 제목");
board.setWriter("관리자");
board.setContent("JPA 글등록");
board.setCreateDate(new Date());
board.setCnt(0L);
// 글 등록
em.persist(board);
tx.commit();
tx.rollback();
} catch (Exception e) {
e.printStackTrace();
} finally {
em.close();
emf.close();
}
}
}
완료
'JAVA > JPA' 카테고리의 다른 글
Annotation - @Id (0) | 2023.06.25 |
---|---|
Annotation - @Table (0) | 2023.06.25 |
Annotation - @Entity (0) | 2023.06.25 |
hibernate.dialect, hiberbate란? (0) | 2023.05.03 |
JPA 테이블 CRUD중 에러, java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException (0) | 2023.05.03 |