CascadeType.PERSIST (级联新建)
CascadeType.REMOVE (级联删除)
CascadeType.REFRESH (级联刷新)
CascadeType.MERGE (级联更新)中选择一个或多个。
CascadeType.ALL
实例:
主题 表结构
@Entity
@Table(name="forum_theme")
public class ForumTheme extends BaseEntity {
/*论坛分类*/
@ManyToOne
private ForumCategory forumCategory;
/*回复*/
@OneToMany(mappedBy="forumTheme",cascade = CascadeType.ALL)
List<ForumReply> forumReplys;
回复 表结构
@Entity
@Table(name="forum_reply")
public class ForumReply extends BaseEntity {
/*回复的主题*/
@ManyToOne
private ForumTheme forumTheme;
/*回复内容,为大字段代码*/
@Lob
@Basic(fetch=FetchType.LAZY)
protected byte[] repcontent;
public void setRepcontent(byte[] repcontent) {
this.repcontent = repcontent;
}
public String getRepcontent() {
try {
if (repcontent==null) return "";
return new String(repcontent, "UTF-8");
} catch (UnsupportedEncodingException e) {
return "";
}
}
public void setRepcontent(String repcontent) {
try {
this.repcontent = repcontent.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
this.repcontent = null;
}
}
mappedBy="forumTheme",
就相当于inverse=true,(mappedBy指定的是不需要维护关系的一端)。
这样删除主题的话,主题下的回复就会同样删光了。