1 Reply Latest reply on Oct 9, 2007 8:22 AM by pvelarde

    NULL FOREIGN KEY

    pvelarde

      Hi all,

      I've got a master-detail relation mapped with these two simple EJB3.0 beans below; when I modify master record, the detail records lose the join with master so masterid is set to null in detail table.

      I don't want any cascade efect so CASCADETYPE is not established in master.

      I've read something about this problem in forus but have no solution. Is this an Hibernate bug¿? have any idea ¿?

      thanks for your help.

      MASTER BEAN CODE

      @Entity
      @Table(name = "master")
      @Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
      public class MasterEJB implements Serializable {
      
      ...
      
      protected Collection<DetailEJB> details = new Vector<DetailEJB>();
      
      @OneToMany(targetEntity = DetailEJB.class)
      @JoinColumn(name="master_id")
      public Collection<DetailEJB> getDetails() {
       return details;
      }
      
      public void setDetails(Collection<DetailEJB> details) {
       this.details = details;
      }
      
      ...
      


      DETAIL BEAN CODE

      @Entity
      @Table(name = "detail")
      @Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
      public class DetailEJB implements Serializable {
      
      ...
      
      @ManyToOne(optional=false)
      @JoinColumn(name="master_id", nullable=false)
      public Integer getMasterID() {
       return masterid;
      }
      
      public void setMasterID(Integer masterid) {
       this.masterid = masterid;
      }
      
      ...
      


      Each bean has its own table on a MySQL DBMS, here are master and detail tables definitions:

      CREATE TABLE `master` (
       `id` int(11) NOT NULL auto_increment,
       PRIMARY KEY (`id`)
      ) ENGINE=InnoDB
      
      CREATE TABLE `detail` (
       `id` int(11) NOT NULL auto_increment,
       `master_id` int(11) default '0',
       PRIMARY KEY (`id`),
       KEY `FK30E0AFBF496DF04C` (`master_id`)
      ) ENGINE=InnoDB
      
      




        • 1. Re: NULL FOREIGN KEY
          pvelarde

          sorry, I forgot when I modify a master object the details are not included becouse details are historic records. Any way, if I established no CASCADE-TYPE, master modifications wouldn't modify old details, is this true¿?