yes i use bidirectional assosiations.
a bidirectional many2many from contact to receiverlist. and we would like to get, that if i delete a contact, this should be deleted in all receiverlists. but not inverse. logicaly:-) do you need some more infos?
here some code:
contact.hbm.xml:
<class name="contact.Contact" table="contact">
<id name="contact_id" type="int" unsaved-value="null" >
<column name="contact_id" sql-type="integer" not-null="true"/>
<generator class="identity"/>
</id>
...
<many-to-one name="user" column="u_id" class="user.User"/>
<set name="receiverlists" table="consists_of" cascade="save-update" inverse="true">
<key column="contact_id"/>
<many-to-many class="receiverlist.Receiverlist" column="receiverlist_id"/>
</set>
</class>
receiverlist.hbm.xml
<class name="receiverlist.Receiverlist" table="receiverlist">
<id name="receiverlist_id" type="int" unsaved-value="null" >
<column name="receiverlist_id" sql-type="integer" not-null="true"/>
<generator class="identity"/>
</id>
...
<many-to-one name="user" column="u_id" class="user.User"/>
<set name="contacts" table="consists_of" cascade="save-update" >
<key column="receiverlist_id"/>
<many-to-many class="contact.Contact" column="contact_id" />
</set>
</class>
the file where we would like to delete a contact :...and i'm sure, its not that f. complicated....:-)
with this file we have to delete the deleted contact in the receiverlists. but i think that should be done automatically by hibernate...
public class DeleteContactAction extends Action{
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res) throws Exception{
List temp = Collections.synchronizedList(new ArrayList());
DynaActionForm df = (DynaActionForm)form;
String[] contactId = (String[])df.get("deleteContact");
HttpSession session = req.getSession(true);
User user = (User)session.getAttribute("user");
for(int i=0; i < contactId.length; i++){
Set contactSet = user.getContacts();
Iterator contactIter = contactSet.iterator();
for(int m=0; m < contactSet.size(); m++){
Contact contact = (Contact)contactIter.next();
if(contact.getContactId() == Integer.parseInt(contactId[i])){
Deleter.delete(contact);
temp.add(contact);
}
}
}
Iterator tempIter = temp.iterator();
for(int n = 0; n < temp.size(); n++){
Contact c = (Contact)tempIter.next();
user.getContacts().remove(c);
Set receiverlistSet = user.getReceiverlists();
Iterator receiverlistIter = receiverlistSet.iterator();
for(int r=0; r < receiverlistSet.size(); r++){
Receiverlist receiverlist = (Receiverlist)receiverlistIter.next();
receiverlist.getContacts().remove(c);
}
}
return mapping.findForward("contactDeleted");
}
}
|