Hibernate级联操作请教 @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)@JoinColumn(name = "nodeId")private TreeNode root = null;有什么办法设置不让级联删除和更新,但是要级联添加和查询 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 cascade = CascadeType.ALL,把这句改成cascade = CascadeType.ADD,好像是ADD,你查查 应该是cascade = CascadeType.PERSIST增加cascade = CascadeType.ALL的话删除和更新及刷新还有增加都会级联操作的。 楼上正解 =all的时候自然是所有的都有有啦 这些都是小细节 注意点就好 回复3,4,5楼,你们所说的是不行的package com.jianglijun.webtemplate.entity;import java.io.Serializable;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.JoinColumn;import javax.persistence.OneToOne;import com.jianglijun.webtemplate.enums.MenuState;/** * 后台管理的菜单 * * @author jlj */@SuppressWarnings("serial")@Entitypublic class Menu implements Serializable { public Menu() { } public Menu(String title, TreeNode treeNode) { this.title = title; this.root = treeNode; } @Id @GeneratedValue(strategy = GenerationType.AUTO) private int id; private String title; private String iconCls = "settings"; private boolean autoScroll = true; private boolean border = false; private String xtype = "treepanel"; @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER) @JoinColumn(name = "nodeId") private TreeNode root = null; private MenuState menuState = MenuState.Enable; public MenuState getMenuState() { return menuState; } public void setMenuState(MenuState menuState) { this.menuState = menuState; } public int getId() { return id; } public String getTitle() { return title; } public String getIconCls() { return iconCls; } public boolean isAutoScroll() { return autoScroll; } public boolean isBorder() { return border; } public TreeNode getRoot() { return root; } public void setId(int id) { this.id = id; } public void setTitle(String title) { this.title = title; } public void setIconCls(String iconCls) { this.iconCls = iconCls; } public void setAutoScroll(boolean autoScroll) { this.autoScroll = autoScroll; } public void setBorder(boolean border) { this.border = border; } public void setRoot(TreeNode root) { this.root = root; } public String getXtype() { return xtype; } public void setXtype(String xtype) { this.xtype = xtype; }}package com.jianglijun.webtemplate.entity;import java.io.Serializable;import java.util.ArrayList;import java.util.List;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.JoinColumn;import javax.persistence.ManyToOne;import javax.persistence.OneToMany;/** * 节点 * * @author jlj * */@SuppressWarnings("serial")@Entitypublic class TreeNode implements Serializable { public TreeNode() { } public TreeNode(String text, String icon) { this.text = text; this.icon = icon; } public TreeNode(String text, String icon, String href) { this.text = text; this.icon = icon; this.href = href; } @Id @GeneratedValue(strategy = GenerationType.AUTO) private int id; private String text; private String href = "javascript:void(0)"; private String icon; private boolean leaf = false;// 是否有子节点 private boolean expanded = true; private boolean singleClickExpand = true; @ManyToOne @JoinColumn(name = "parentId") private TreeNode parentNode;//父节点 @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "parentNode") private List<TreeNode> children = new ArrayList<TreeNode>();//子节点 public int getId() { return id; } public String getText() { return text; } public String getHref() { return href; } public String getIcon() { return icon; } public TreeNode getParentNode() { return parentNode; } public List<TreeNode> getChildren() { return children; } public void setId(int id) { this.id = id; } public void setText(String text) { this.text = text; } public void setHref(String href) { this.href = href; } public void setIcon(String icon) { this.icon = icon; } public void setParentNode(TreeNode parentNode) { this.parentNode = parentNode; } public void setChildren(List<TreeNode> children) { this.children = children; } public boolean isLeaf() { return leaf; } public boolean isExpanded() { return expanded; } public boolean isSingleClickExpand() { return singleClickExpand; } public void setLeaf(boolean leaf) { this.leaf = leaf; } public void setExpanded(boolean expanded) { this.expanded = expanded; } public void setSingleClickExpand(boolean singleClickExpand) { this.singleClickExpand = singleClickExpand; }}要怎么样的Menu里面设置啊? 我在Menu里面管理TreeNode的添加和查询,不管理删除和更新 楼主被你说糊涂了。级联操作原本就是hibernate一个特征。你怎么搞到后面java来了。你在pojo类要设置什么,这里不关,但是你要在影射文件里修改,级联的级别。 使用代码cascade = CascadeType.Add; 既可以了 本人菜鸟,求教如何实现一个电子商城的搜索功能,说下大体思路也行 JavaWED的配置信息问题 web.xml是做什么用的,想听高手详解! 如何使uid始终有效,不过期,!!!!!!!!!!!!!! js验证问题请教,不是单纯的验证 有关JFreeChart -- 柱状图在jsp中调用的方法 门外汉提问 请问那种模式能使我们对数据库的表完全操作,添加字段,删除字段,添加、修改、删除数据页面都能很快完成? github一个仓库里面放多个项目好么? 我直接访问jsp页面报404 求解 关于网站服务器的配置 quartz部署时,报错
把这句改成cascade = CascadeType.ADD,好像是ADD,你查查
cascade = CascadeType.ALL的话删除和更新及刷新还有增加都会级联操作的。
你们所说的是不行的package com.jianglijun.webtemplate.entity;import java.io.Serializable;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.JoinColumn;
import javax.persistence.OneToOne;import com.jianglijun.webtemplate.enums.MenuState;/**
* 后台管理的菜单
*
* @author jlj
*/
@SuppressWarnings("serial")
@Entity
public class Menu implements Serializable { public Menu() {
} public Menu(String title, TreeNode treeNode) {
this.title = title;
this.root = treeNode;
} @Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String title;
private String iconCls = "settings";
private boolean autoScroll = true;
private boolean border = false;
private String xtype = "treepanel";
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "nodeId")
private TreeNode root = null;
private MenuState menuState = MenuState.Enable; public MenuState getMenuState() {
return menuState;
} public void setMenuState(MenuState menuState) {
this.menuState = menuState;
} public int getId() {
return id;
} public String getTitle() {
return title;
} public String getIconCls() {
return iconCls;
} public boolean isAutoScroll() {
return autoScroll;
} public boolean isBorder() {
return border;
} public TreeNode getRoot() {
return root;
} public void setId(int id) {
this.id = id;
} public void setTitle(String title) {
this.title = title;
} public void setIconCls(String iconCls) {
this.iconCls = iconCls;
} public void setAutoScroll(boolean autoScroll) {
this.autoScroll = autoScroll;
} public void setBorder(boolean border) {
this.border = border;
} public void setRoot(TreeNode root) {
this.root = root;
} public String getXtype() {
return xtype;
} public void setXtype(String xtype) {
this.xtype = xtype;
}}
package com.jianglijun.webtemplate.entity;import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;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.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;/**
* 节点
*
* @author jlj
*
*/
@SuppressWarnings("serial")
@Entity
public class TreeNode implements Serializable {
public TreeNode() {
} public TreeNode(String text, String icon) {
this.text = text;
this.icon = icon;
} public TreeNode(String text, String icon, String href) {
this.text = text;
this.icon = icon;
this.href = href;
} @Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String text;
private String href = "javascript:void(0)";
private String icon;
private boolean leaf = false;// 是否有子节点
private boolean expanded = true;
private boolean singleClickExpand = true; @ManyToOne
@JoinColumn(name = "parentId")
private TreeNode parentNode;//父节点
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "parentNode")
private List<TreeNode> children = new ArrayList<TreeNode>();//子节点 public int getId() {
return id;
} public String getText() {
return text;
} public String getHref() {
return href;
} public String getIcon() {
return icon;
} public TreeNode getParentNode() {
return parentNode;
} public List<TreeNode> getChildren() {
return children;
} public void setId(int id) {
this.id = id;
} public void setText(String text) {
this.text = text;
} public void setHref(String href) {
this.href = href;
} public void setIcon(String icon) {
this.icon = icon;
} public void setParentNode(TreeNode parentNode) {
this.parentNode = parentNode;
} public void setChildren(List<TreeNode> children) {
this.children = children;
} public boolean isLeaf() {
return leaf;
} public boolean isExpanded() {
return expanded;
} public boolean isSingleClickExpand() {
return singleClickExpand;
} public void setLeaf(boolean leaf) {
this.leaf = leaf;
} public void setExpanded(boolean expanded) {
this.expanded = expanded;
} public void setSingleClickExpand(boolean singleClickExpand) {
this.singleClickExpand = singleClickExpand;
}}要怎么样的Menu里面设置啊? 我在Menu里面管理TreeNode的添加和查询,不管理删除和更新
级联操作原本就是hibernate一个特征。你怎么搞到后面java来了。
你在pojo类要设置什么,这里不关,但是你要在影射文件里修改,级联的级别。