1:student 学生类
2:studentLog 学生日志类
3:studentExtend 学生扩展类
对于普通的业务需求,可能只需要student、studentLog就可以了,但此处模仿多个类来做映射:
@Entity@Table(name="student")public class Student implements Serializable { private Integer id; private String username; private StudentLog studentLog; private StudentExtend studentExtend; @Id @GeneratedValue(strategy=GenerationType.AUTO) public Integer getId() { return id; } /fetch=FetchType.LAZY 设置懒加载 //cascade={CascadeType.REMOVE} 级联删除(注意不是在Studentlog、studentExtend设置) //mappedBy="student" @OneToOne(fetch=FetchType.LAZY,cascade={CascadeType.REMOVE},mappedBy="student") public StudentExtend getStudentExtend() { return studentExtend; } @OneToOne(fetch=FetchType.LAZY,cascade={CascadeType.REMOVE},mappedBy="student") public StudentLog getStudentLog() { return studentLog; } @Column(name="username",unique=true,nullable=false) public String getUsername() { return username; } //tXxx略
@Entity@Table(name="student_log")public class StudentLog implements Serializable { private Integer id; private Student student; private String message; @Id @GeneratedValue(strategy=GenerationType.AUTO) public Integer getId() { return id; } @Column(name="message",unique=false,nullable=false) public String getMessage() { return message; } @OneToOne(fetch=FetchType.LAZY) @JoinColumn(name="student_id") public Student getStudent() { return student; } //etXxx略
@Entity@Table(name="student_extend")public class StudentExtend implements Serializable { private Integer id; private Student student; private String extendMessage; @Id @GeneratedValue(strategy=GenerationType.AUTO) public Integer getId() { return id; } @Column(name="extend_message",unique=false,nullable=false) public String getExtendMessage() { return extendMessage; } @OneToOne(fetch=FetchType.LAZY) @JoinColumn(name="student_id") public Student getStudent() { return student; }//tXxx略
然后直接调用session.delete删除student(主表)记录时,studentLog、studentExtend等从表也会被删除
studentService.deleteById(1);