6 Replies Latest reply on Dec 2, 2010 12:21 PM by Wahid Atif

    Persistence: @ManyToOne - how to set null referencing rows

    first last Newbie

      Hey all,


      I've been scratching my head about this, tried the Hibernate forum and found no help.
      My hibernate hbm2ddl creates two tables corresponding to two Entities: Person and House.


      @Entity
      public class Person {
         
         private House house;
      
         @ManyToOne
         public House getHouse() { return this.house; }
         public void setHouse(House house) { this.house = house; }
      }



      Each Person CAN have a house, a House itself is oblivious to Persons, its a uni-directional relationship.


      The above creates the following field, key and constraint in the person table:



      house_id bigint(20) DEFAULT NULL,
      KEY FK7877DFEB59528447 (house_id)
      CONSTRAINT FK7877DFEB59528447 FOREIGN KEY (house_id) REFERENCES house (house)
      



      (removed some quotes from this)


      My wish:


      I need to able to delete a House, affecting the Person table is such a way that whatever Person pointed to that house will now have null instead.


      I understand this has to do with on delete set null that SQL has.


      Can I achieve this with JPA or Hibernate ?


      If not, should I hard-code it into the DB - If that is the case, how can this be done programaticaly through Seam... ? Something like a statement that says: if this constraint doesn't exist then create it.


      Thanks for any info.