---
# System prepended metadata

title: 中繼器 V2
tags: [CTBC-Lab2]

---

# 中繼器 V2
###### tags: `CTBC-Lab2`

* 宏毅哥的解法
```java
public CourseCategoryBean getCourseCategoryBean() {
    if(Objects.isNull(courseCategoryBean)) {
        return null;
    }
    CourseCategoryBean r = new CourseCategoryBean();
    r.setCategoryId(courseCategoryBean.getCategoryId());
    r.setCategoryName(courseCategoryBean.getCategoryName());
    r.setDetail(courseCategoryBean.getDetail());
    return r;
}
```

* 我的bean程式碼
* 子類別-學生
```java
package com.ctbcbank.boot.Lab4.bean;

import java.sql.Clob;
import java.util.ArrayList;
import java.util.List;
import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;

import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;

import lombok.Data;
import lombok.NoArgsConstructor;


@Entity
@Data
@NoArgsConstructor
public class StudentBean {
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private long studentId;
	
	private String studentName;
	private int studentScore;
	@Lob
	private String studentCode;
	
	// 新增時間
	private Date createDate;
	@PrePersist
	public void onCreate() {
		this.createDate = new Date();}
	// 修改時間
	private Date updatedAt;
	@PreUpdate
	private void onUpdate() {this.updatedAt = new Date();}
	
	// 
	// V1
//	@ManyToOne(cascade = CascadeType.REMOVE)
//	@JoinColumn(name = "gradeId")
	// V2
//	@ManyToOne(cascade = CascadeType.REMOVE)
//	@JoinColumn(name = "gradeId", referencedColumnName = "gradeId", nullable = false)
//	@OnDelete(action = OnDeleteAction.NO_ACTION)
	// V3
	// 子類別annotation
	@ManyToOne
	@JoinColumn(name = "gradeId", nullable = false)
	private GradeBean gradeBean;

}

```

* 父類別-年級
```java
package com.ctbcbank.boot.Lab4.bean;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Generated;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;

import com.fasterxml.jackson.annotation.JsonIgnore;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;


@Data
@Entity
@NoArgsConstructor
public class GradeBean {
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private long gradeId;
	
	private String gradeName; // 年級名稱
	private int gradeHowMany; // 年級人數
	private float gradeAverage; // 年級平均
	
	// 新增時間 
	private Date createDate;
	@PrePersist
	public void onCreate() {
		this.createDate = new Date();}
	// 修改時間
	private Date updatedAt;
	@PreUpdate
	private void onUpdate() {this.updatedAt = new Date();}
	
	// V1
//	@OneToMany(fetch = FetchType.EAGER,mappedBy = "gradeBean")
	// V2
	// 父類別annotation
	@OneToMany(mappedBy = "gradeBean", cascade = CascadeType.REMOVE)
	@JsonIgnore
	@ToString.Exclude 
	private List<StudentBean> studentsList = new ArrayList<>();
	

}

```