spring boot

테이블 생성 및 JPA 테스트

alarim 2023. 5. 9. 18:25

 

 

 

https://docs.jboss.org/hibernate/orm/5.0/javadocs/org/hibernate/boot/model/naming/ImplicitNamingStrategyComponentPathImpl.html

 

ImplicitNamingStrategyComponentPathImpl (Hibernate JavaDocs)

All Implemented Interfaces: Serializable, ImplicitNamingStrategy public class ImplicitNamingStrategyComponentPathImpl extends ImplicitNamingStrategyJpaCompliantImpl An ImplicitNamingStrategy implementation which uses full composite paths extracted from Att

docs.jboss.org

 

package com.tenco.blog.model;

import java.sql.Timestamp;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class User {
	
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private int id; 
	private String username; 
	private String password; 
	private String email; 
	private Timestamp createdDate;
	
}
​

 

package com.tenco.blog.model;

import java.sql.Timestamp;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Board {
	
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private int id;
	private String title; 
	private String content; 
	//private User user;
	private Timestamp createdDate;
}

 

package com.tenco.blog.model;

import java.sql.Timestamp;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Reply {
	
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private int id; 
	private String content; 
	// 어느 게시글에 작성된 리플
	// 누가 작성한 리블인지 
	
	private Timestamp createdDate;
	
	
}

 

JPA 활용해서 테이블 컬럼에 제약 설정 및 외래키 설정하기

package com.tenco.blog.model;

import java.sql.Timestamp;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.CreationTimestamp;

@Entity
public class User {
	
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private int id;
	@Column(nullable = false, length = 30)
	private String username; 
	
	@Column(nullable = false, length = 100)
	private String password; 
	
	@Column(nullable = false, length = 50)
	private String email; 
	
	@ColumnDefault("'user'") // 문자열 타입 이라고 명시는 ''(홑따옴표) 
	private String role; // user, admin, manager 
	
	@CreationTimestamp // 자동 시간 입력 == now() 
	private Timestamp createdDate;
	
}