
공용으로 사용될 컬럼을 별도의 클래스로 빼고 상속받아서 사용
@Data
@MappedSuperclass
public class PublicInfoEntity {
@CreationTimestamp
private Date creationDate;
@UpdateTimestamp
private Date updateDate;
}
//단방향으로 작업
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "myemp")
public class EmpEntity extends PublicInfoEntity {
@Id
private String userId;
private String name;
private String addr;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "userPrivateId")
private PrivateInfoEntity infoEntity;
public EmpEntity(String userId, String name, String addr, PrivateInfoEntity infoEntity) {
this.userId = userId;
this.name = name;
this.addr = addr;
this.infoEntity = infoEntity;
}
//경력사항은 한 사람이 여러 개 가질 수 있다.
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "userKey")
private List<HistoryEntity> historyEntityList = new ArrayList<>();
@ManyToOne
@JoinColumn(name = "deptId")
private DeptEntity dept;
public EmpEntity(String userId, String name, String addr, PrivateInfoEntity infoEntity, List<HistoryEntity> historyEntityList) {
this.userId = userId;
this.name = name;
this.addr = addr;
this.infoEntity = infoEntity;
this.historyEntityList = historyEntityList;
}
}
myemp테이블과 1:1로 매핑되는 사적정보를 담고있는 엔티티
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "userInfo")
public class PrivateInfoEntity {
//시퀀스를 PK로 하지 않고 아이디를 PK
@Id
private String userInfoId;
private String info1;
private String info2;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "myhistory")
public class HistoryEntity {
@Id
@GeneratedValue
private Long historyId;
private String company;
private String content;
public HistoryEntity(String company, String content) {
this.company = company;
this.content = content;
}
}
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "mydept")
public class DeptEntity {
@Id
@GeneratedValue
@Column(name = "deptNo")
private Long id;
private String name;
private String mgr;
public DeptEntity(String name, String mgr) {
this.name = name;
this.mgr = mgr;
}
}
@SpringBootTest
@Transactional
@Rollback(value = false)
class EmpEntityTest {
//JPA의 스펙에서 제공하는 기능을 제대로 사용할 수 있도록 제공
//@Autowired와 같은 역할도 수행
//@PersistenceContext는 EntityManager를 빈으로 주입할 때 사용하는 어노테이션
@PersistenceContext
EntityManager entityManager;
@Test
public void test1(){
//사원의 기본정보와 사원의 시크릿정보를 입력받아서 두테이블에 저장
PrivateInfoEntity privateInfo1 =
new PrivateInfoEntity("bts1","태양과 듀엣","솔로");
PrivateInfoEntity privateInfo2 =
new PrivateInfoEntity("bts2","Seven","올림픽곡");
PrivateInfoEntity privateInfo3 =
new PrivateInfoEntity("bts3","춤모야","조교");
PrivateInfoEntity privateInfo4 =
new PrivateInfoEntity("bts4","제대했다","너무해");
PrivateInfoEntity privateInfo5 =
new PrivateInfoEntity("kbr","싱어송라이터","바람바람바람");
EmpEntity emp1 = new EmpEntity("bts1","지민","광주",privateInfo1);
EmpEntity emp2 = new EmpEntity("bts2","정국","부산",privateInfo2);
EmpEntity emp3 = new EmpEntity("bts3","제이홉","광주",privateInfo3);
EmpEntity emp4 = new EmpEntity("bts4","석진","천안",privateInfo4);
EmpEntity emp5 = new EmpEntity("kbr","범룡","청주",privateInfo5);
entityManager.persist(emp1);
entityManager.persist(emp2);
entityManager.persist(emp3);
entityManager.persist(emp4);
entityManager.persist(emp5);
//object references an unsaved transient instance - save the transient instance before flushing 오류 긁어서 구글링하면 해결가능
}
@Test
public void readtest1(){
EmpEntity empEntity = entityManager.find(EmpEntity.class,"bts4");
System.out.println(empEntity);
}
@Test
public void test2(){
//경력사항, emp기본정보, 시크릿정보 모두 저장하기
List<HistoryEntity> historyEntityList = new ArrayList<>();
historyEntityList.add(new HistoryEntity("A사","front react개발"));
historyEntityList.add(new HistoryEntity("B사","Entity개발"));
historyEntityList.add(new HistoryEntity("C사","보안개발"));
EmpEntity emp = new EmpEntity("bts7", "슈가", "대구",
new PrivateInfoEntity("bts7","화양연화","래퍼"),
historyEntityList);
entityManager.persist(emp);
}
@Test
public void readtest2(){
EmpEntity empEntity = entityManager.find(EmpEntity.class,"bts7");
System.out.println(empEntity);
}
@Test
public void test3(){
//부서, 기본정보, 시크릿, 히스토리
DeptEntity dept1 = new DeptEntity("전산실","김서연");
entityManager.persist(dept1);
//경력
List<HistoryEntity> historyEntityList = new ArrayList<>();
historyEntityList.add(new HistoryEntity("A사","front react개발"));
historyEntityList.add(new HistoryEntity("B사","Entity개발"));
historyEntityList.add(new HistoryEntity("C사","보안개발"));
//사원정보, 시크릿, 경력정보, 부서정보
EmpEntity emp = new EmpEntity("bts7", "슈가", "대구",
new PrivateInfoEntity("bts7","화양연화","래퍼"),
historyEntityList,dept1);
entityManager.persist(emp);
}
@Test
public void readtest3(){
EmpEntity empEntity = entityManager.find(EmpEntity.class,"bts7");
System.out.println(empEntity);
}
}
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "category")
public class CategoryEntity {
@Id
@GeneratedValue
private Long categoryId;
private String categoryName;
private String info;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "product")
private List<ProductEntity> productEntityList = new ArrayList<>();
public CategoryEntity(String categoryName, String info) {
this.categoryName = categoryName;
this.info = info;
}
public CategoryEntity(String categoryName, String info, List<ProductEntity> productEntityList) {
this.categoryName = categoryName;
this.info = info;
this.productEntityList = productEntityList;
}
}
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "product")
public class ProductEntity extends PublicInfoEntity {
@Id
@GeneratedValue
private Long productNo;
private String productName;
private String info;
private String image;
private int price;
//CategoryEntity category;
public ProductEntity(String productName, String info, String image, int price) {
this.productName = productName;
this.info = info;
this.image = image;
this.price = price;
}
}
@SpringBootTest
@Transactional
@Rollback(value = false)
class CategoryEntityTest {
@PersistenceContext
EntityManager entityManager;
@Test
public void test(){
List<ProductEntity> productEntityList1 = new ArrayList<>();
productEntityList1.add(new ProductEntity("식탁","갈색","사진1.jpg",3000));
productEntityList1.add(new ProductEntity("수납장","녹색","사진4.jpg",1000));
List<ProductEntity> productEntityList2 = new ArrayList<>();
productEntityList2.add(new ProductEntity("쇼파","아이보리색","사진2.jpg",5000));
productEntityList2.add(new ProductEntity("수납장","녹색","사진4.jpg",1000));
List<ProductEntity> productEntityList3 = new ArrayList<>();
productEntityList3.add(new ProductEntity("침대","폭신함","사진3.jpg",4000));
productEntityList3.add(new ProductEntity("이불","흰색","사진5.jpg",2000));
CategoryEntity category1 = new CategoryEntity("주방가구","식탁과수납장",productEntityList1);
CategoryEntity category2 = new CategoryEntity("거실가구","쇼파와수납장",productEntityList2);
CategoryEntity category3 = new CategoryEntity("침구","침대와협탁",productEntityList3);
entityManager.persist(category1);
entityManager.persist(category2);
entityManager.persist(category3);
}
@Test
public void readtest(){
ProductEntity productEntity = entityManager.find(ProductEntity.class,1L);
System.out.println(productEntity);
CategoryEntity categoryEntity = entityManager.find(CategoryEntity.class,1L);
System.out.println(categoryEntity);
}
}
본 포스팅은 멀티캠퍼스의 멀티잇 백엔드 개발(Java)의 교육을 수강하고 작성되었습니다.