5 Replies Latest reply on Nov 28, 2005 2:55 PM by iiping

    O/R Mapping Problem Cascades....

      Hello I bumped in to a little problem on O/R Mapping regarding on cascades.


      Here is my little POJO mapped and zipped-out as par archived on a exploded ear...


      Specs i used is

      JBoss4.0.3sp1 on ejb3
      JDK 1.5 series on JRockit 1.5_03
      and MySQL 5.0_13 on ConnectorJ 3.1.11 with MySQLInnoDBDialect

      package advance.semiconductor.schematic.layout.v2.model;

      import javax.persistence.*;
      import java.io.Serializable;

      @Entity
      @Table(name="mm_class")
      public class Classification implements Serializable{

      private long classificationId;
      private Classification parent; // a self relating <--- problem occurs here
      private String description;

      public Classification(){}

      /** accessor method **/

      @Id(generate=GeneratorType.AUTO)
      @Column(name="mm_classification_id")
      public long getClassificationId(){return classificationId;}
      public void setClassificationId(long classificationId){this.classificationId = classificationId;}

      /** man i can't seem to get this right **/
      @ManyToOne(cascade={CascadeType.ALL})
      @JoinColumn(name="mm_parent_classification_id")
      public Classification getParent(){return parent;}
      public void setParent(Classification parent){this.parent = parent;}

      @Column(name="mm_desc")
      public String getDescription(){return description;}
      public void setDescription(String description){this.description = description;}


      }


      Now when I delete some child nodes it doesn't have a problem but when i delete a parent node...

      Tada!! problem occurs... and error are produced.


      WARN [JDBCExceptionReporter] SQL Error: 1451, SQLState: 23000
      ERROR [JDBCExceptionReporter] Cannot delete or update a parent row: a foreign key constraint fails (`semicon_ejb3/mm_class`, CONSTRAINT `FK68A9621916E3F592` FOREIGN KEY (`mm_parent_classification_id`) REFERENCES `mm_class` (`mm_classification_id`))


      and ... a whole bunch of unsynchronized database and object errors..... here ###




      I would like to ask some question on how can I map it with such annotation that when i run hbm2ddl tool it can
      auto-magically create like this script.. below


      create table mm_class(
      mm_classification_id bigint auto_increment not null,
      mm_desc varchar(255),
      mm_parent_classification_id bigint,
      primary key(mm_classification_id),
      constraint `fk1_root_class_id` foreign key (`mm_parent_classification_id`)
      references `mm_class` (`mm_classification_id`)
      on update cascade on delete cascade
      ) type= innodb;