CascadeType.REMOVE 级联删除操作,删除当前实体时,与它有映射关系的实体也会跟着被删除。
CascadeType.MERGE 级联更新(合并)操作,当Student中的数据改变,会相应地更新Course中的数据。CascadeType.DETACH 级联脱管/游离操作,如果你要删除一个实体,但是它有外键无法删除,你就需要这个级联权限了。它会撤销所有相关的外键关联。CascadeType.REFRESH 级联刷新操作,假设场景 有一个订单,订单里面关联了许多商品,这个订单可以被很多人操作,那么这个时候A对此订单和关联的商品进行了修改,与此同时,B也进行了相同的操作,但是B先一步比A保存了数据,那么当A保存数据的时候,就需要先刷新订单信息及关联的商品信息后,再将订单及商品保存。CascadeType.ALL 拥有以上所有级联操作权限。一(被拥有方)mappedBy,mappedBy = "‘多’的实体类的属性字段",mappedBy 为被拥有方多(拥有方) @JoinColumn----------------------------------------------------------------------- 一对一people 用户表(id,name,sex,birthday,address_id)address 地址表(id,phone,zipcode,addr)people 表@Entitypublic class People { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id", nullable = false) private Long id;//id @Column(name = "name", nullable = true, length = 20) private String name;//姓名 @Column(name = "sex", nullable = true, length = 1) private String sex;//性别 @Column(name = "birthday", nullable = true) private Timestamp birthday;//出生日期 @OneToOne(cascade=CascadeType.ALL) //People是关系的维护端,当删除 people,会级联删除 address @JoinColumn(name = "address_id", referencedColumnName = "id") //name:关联id,referencedColumnName:地址表id private Address address;//地址}关联的实体的主键一般是用来做外键的。但如果此时不想主键作为外键,则需要设置referencedColumnName属性。当然这里关联实体(Address)的主键 id 是用来做主键,所以这里第23行的 referencedColumnName = "id" 实际可以省略。address 表@Entitypublic class Address { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id", nullable = false) private Long id;//id @Column(name = "phone", nullable = true, length = 11) private String phone;//手机 @Column(name = "zipcode", nullable = true, length = 6) private String zipcode;//邮政编码 @Column(name = "addr", nullable = true, length = 100) private String addr;//地址 //如果不需要根据Address级联查询People,可以注释掉,mappedBy = "People实体类中address属性"// @OneToOne(mappedBy = "address", cascade = {CascadeType.MERGE, CascadeType.REFRESH}, optional = false)// private People people;}-----------------------------------------------------------------------一对多author 作者表(id,name)article 文章表(id,title,conten,author_id)author 表@Entitypublic class Author { @Id // 主键 @GeneratedValue(strategy = GenerationType.IDENTITY) // 自增长策略 private Long id; //id @NotEmpty(message = "姓名不能为空") @Size(min=2, max=20) @Column(nullable = false, length = 20) private String name;//姓名 //mappedBy = "Article实体类中author属性" @OneToMany(mappedBy = "author",cascade=CascadeType.ALL,fetch=FetchType.LAZY) //级联保存、更新、删除、刷新;延迟加载。当删除用户,会级联删除该用户的所有文章 //拥有mappedBy注解的实体类为关系被维护端 private List<Article> articleList;//文章列表}article 表 @Entity public class Article { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) // 自增长策略 @Column(name = "id", nullable = false) private Long id; @NotEmpty(message = "标题不能为空") @Size(min = 2, max = 50) @Column(nullable = false, length = 50) // 映射为字段,值不能为空 private String title; @Lob // 大对象,映射 MySQL 的 Long Text 类型 @Basic(fetch = FetchType.LAZY) // 懒加载 @NotEmpty(message = "内容不能为空") @Size(min = 2) @Column(nullable = false) // 映射为字段,值不能为空 private String content;//文章全文内容 //可选属性optional=false,表示author不能为空。删除文章,不影响用户 @ManyToOne(cascade={CascadeType.MERGE,CascadeType.REFRESH},optional=false) //author_id关联author的id字段 @JoinColumn(name = "author_id", referencedColumnName = "id") private Author author;//所属作者 }----------------------------------------------------------------------- 多对多(可以随意指定一方为关系维护端 @JoinColumn)user 用户表(id,username,password)user_authority 中间表(user_id,authority_id)authority 权限表(id,authname)User.java @Entity public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @NotEmpty(message = "账号不能为空") @Size(min=3, max=20) @Column(nullable = false, length = 20, unique = true) private String username; // 用户账号,用户登录时的唯一标识 @NotEmpty(message = "密码不能为空") @Size(max=100) @Column(length = 100) private String password; // 登录时密码 @ManyToMany @JoinTable(name = "user_authority",joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "authority_id")) //关系维护端,负责多对多关系的绑定和解除,即实现User.setAuthorityList(authority) // @JoinTable name:关联表的名字,joinColumns:关联User表的id,inverseJoinColumns:关联Authority表的id private List<Authority> authorityList; }Authority.java @Entity public class Authority { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; @Column(nullable = false) private String authname; //权限名 //mappedBy = "User实体类中authorityList属性" @ManyToMany(mappedBy = "authorityList") private List<User> userList; }-------------------------------------------------------------------------------------