java - Hibernate sets null value for a column, instead of deleting the entire row on saveOrUpdate() -


i have class catastropheperilassoc controls mapping of catastrophe class peril class, table cat_peril_assoc has 3 rows: id(primary key, auto-generated), catastrophe.id(mapped catastrophe), , peril.id(mapped peril)

when add or remove associations, new associations(rows) created in table, fine. however, hibernate not delete old rows, instead sets value of catastrophe.id null. there way me tell hibernate delete these rows instead? note don't want delete actual objects being used elsewhere, want remove them particular table.

looking @ sql log, can confirm setting column value null:

update cat_peril_assoc set catastrophe_type_id=null catastrophe_type_id=? 

hbm file:

<hibernate-mapping package="com.nature">     <class name="com.nature.catastropheperilassoc" table="cat_peril_assoc">         <id name="id" type="string">             <column name="id" length="32" />             <generator class="uuid.hex" />         </id>         <many-to-one name="catastrophe" class="com.nature.catastrophe">                <column name="catastrophe_type_id" />          </many-to-one>          <many-to-one name="peril" class="com.nature.peril">                 <column name="peril_id" />          </many-to-one>      </class> </hibernate-mapping> 

java class:

public class catastropheperilassoc  {      private catastrophe cat;      private peril peril;      public catastrophe getcatastrophe() {         return cat;     }     public void setcatastrophe(catastrophe cat) {         this.cat = cat;     }      public peril getperil() {         return peril;     }     public void setperil(peril peril) {         this.peril = peril;     } } 

code update association:

public void savecatperilassoc(catastrophe cat) {      criteria polquery = getsession(false).createcriteria(cat.getclass());     polquery.add(restrictions.eq("name", cat.getname()).ignorecase());      if (cat.getid() != null) {         polquery.add(restrictions.ne("id", cat.getid()));     }      if (polquery.list().size() > 0) {         //throw error     }      getsession(false).saveorupdate(cat); } 

you need use cascade type called delete-orphan. need configure cascade-orphan on catastrophe , peril classes, if of removes association, remove entry join table.

check section of documentation, explains cascade better.

and 1 question. why use catastropheperilassoc join table? hibernate manages many many relationships, there's no need create entities catastropheperilassoc.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -