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

    NULL FOREIGN KEY

    pedro velarde Newbie

      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