2 Replies Latest reply on May 20, 2005 5:29 AM by Jan Bares

    Cascading persist/remove

    Jan Bares Newbie

      Hi,

      suppose this parent/child relation:


      @Entity
      class Parent
       {
       @Id(generate = GeneratorType.AUTO)
       private Long id;
      
       @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL)
       private Set<Children> children = new HashSet<Childer>();
       }
      
      @Entity
      class Child
       {
       @Id(generate = GeneratorType.AUTO)
       private Long id;
      
       @ManyToOne(optional=false)
       private Parent parent;
       }
      


      This code fails to merge(parent). Error: not-null property references a null or transient value (on Child.parent):
      Parent parent = new Parent();
      
      Child child = new Child();
      child.setParent(parent);
      
      parent.getChildren().add(child);
      
      manager.merge(parent);
      


      However for optional=true it works fine. Why?

      This works fine:
      Parent parent = new Parent();
      manager.merge(parent);
      
      Child child = new Child();
      child.setParent(parent);
      
      parent.getChildren().add(child);
      
      manager.merge(parent);
      


      Thanks for explanation, Jan