@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "nodeId")
private TreeNode root = null;有什么办法设置不让级联删除和更新,但是要级联添加和查询

解决方案 »

  1.   

    cascade = CascadeType.ALL,
    把这句改成cascade = CascadeType.ADD,好像是ADD,你查查
      

  2.   

    应该是cascade = CascadeType.PERSIST增加
    cascade = CascadeType.ALL的话删除和更新及刷新还有增加都会级联操作的。
      

  3.   

    楼上正解  =all的时候自然是所有的都有有啦    这些都是小细节  注意点就好
      

  4.   

    回复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")
    @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的添加和查询,不管理删除和更新
      

  5.   

    楼主被你说糊涂了。
    级联操作原本就是hibernate一个特征。你怎么搞到后面java来了。
    你在pojo类要设置什么,这里不关,但是你要在影射文件里修改,级联的级别。
      

  6.   

    使用代码cascade = CascadeType.Add;  既可以了