본문 바로가기
TIL/BackEnd

JPA - Oracle 데이터베이스 생성

by sun_HY 2023. 5. 8.

JPA 이용 기본적인 DB 생성 확인

<!-- 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">
	<persistence-unit name="JPAProject">

		<properties>

			<!-- 필수 속성 -->
			<property name="javax.persistence.jdbc.driver" value="oracle.jdbc.driver.OracleDriver" />
			<!-- DB id/name/url 수정 -->
			<property name="javax.persistence.jdbc.user" value="c##kb" />
			<property name="javax.persistence.jdbc.password" value="1234" />
			<property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:@localhost:1521:orcl" />
			<!-- JPA dialect -->
			<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />


			<!-- 옵션 -->
			<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" /> <!--create | create-drop | update | none -->
			<!-- create: drop 시도 후 create -->
		</properties>

	</persistence-unit>
</persistence>
// MainApp.java

package exam;

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class MainApp {

	public static void main(String[] args) {
		System.out.println("** JPA 시작");
		
		// 인수 unitName: 대소문자 구분
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("JPAProject");
	}

}
// Customer.java

package exam;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity	// 선언한 것들을 column으로 관리하고 싶다!
public class Customer {
	
	@Id	// PK
	private Long id;	// id는 숫자형 권장
	private String userName;	// 기본 null
	private int age;	// 기본 not null
}

글자 길이 등은 임의로 부여 (추가 설정 가능)

 

 

설정 추가

package exam;

import java.time.LocalDateTime;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

@Entity	// 선언한 것들을 column으로 관리하고 싶다!
public class Customer {
	
	@Id	// PK
	private Long id;	// id는 숫자형 권장
	
	@Column(nullable = false, length = 100, name = "user_name")
	private String userName;	// 기본 null
	
	@Column(nullable = true)
	private int age;	// 기본 not null
	
	// LocalDateTime: java 1.8에 추가된 날짜 타입
	@CreationTimestamp
	private LocalDateTime insertDate;	// 등록일
	
	@UpdateTimestamp
	private LocalDateTime updateDate;	// 수정일
	
	@Temporal(TemporalType.DATE)
	private Date birthday;
}

 

Oracle에서 확인

728x90

'TIL > BackEnd' 카테고리의 다른 글

JPA (Java Persistence API)  (0) 2023.05.10
Transaction  (0) 2023.05.04
[Spring] 예외처리: @ExceptionHandler, SimpleMappingExceptionResolver  (0) 2023.04.27
@Autowired와 @Value를 통한 주입의 차이  (0) 2023.04.26
[Spring] IoC & DI  (0) 2023.04.25