You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by xnpeng <xn...@163.com> on 2009/03/17 13:05:15 UTC

how to update/persist associate entity?

class Site{
  String id;
  String name;
  Set<SiteProperty> properties;
  ...setter/getter
}
 
class SiteProperty{
 String typeId;
 String typeName;
 Site site;
 ...
 setter/getter
}
 
SiteController.java
class SiteController extends ActionSupport{
  Site site;
  setter/getter...
  public String update(){
     Session session = SessionFactory.getSession();
     Transaction tx = session.beginTransaction();
     try{
        session.saveOrUpdate(site);
        Set props = site.getProperties();
        Iterator it = props.iterator();
        while(it.hasNext()){
           SiteProperty prop = (SiteProperty)it.next();
           prop.setSite(site);
           session.saveOrUpdate(prop);
        }
        tx.commit();
     }catch(HibernateException e){
        tx.rollback();
     }
     return "index";
  }
}
 
site-edit.jsp:
<s:form action="site/%{site.id}">
  <s:hidden name="_method" value="put"/>
  <s:textvalue name="site.id"/>
  <s:textvalue name="site.name"/>
  <s:iterator value="site.properties">
     <s:textvalue name="propId"/>
     <s:textvalue name="propName"/>
     <s:textvalue name="site.id"/>
  </s:iterator>
  <s:submit/>
</s:form>
 
my question is,i can insert/update Site entity into database, while the properties<SiteProperty> cannot save to database, even in memory,the properties data refuse to take any change i made in client.
 
i'm confused by form/action mapping, how does the data transfer to action when client submit the form? i traced the action's setter/getter method,there is no step into these methods,how does the request data transfer to action?
 
thank you.


 

Re:how to update/persist associate entity?

Posted by xnpeng <xn...@163.com>.
class Site{
  String id;
  String name;
  Set<SiteProperty> properties;
  ...setter/getter
}

class SiteProperty{
String propId;
String propName;
Site site;
..
setter/getter
}

SiteController.java
class SiteController extends ActionSupport{
  Site site;
  setter/getter...
  public String update(){
     Session session = SessionFactory.getSession();
     Transaction tx = session.beginTransaction();
     try{
        session.saveOrUpdate(site);
        Set props = site.getProperties();
        Iterator it = props.iterator();
        while(it.hasNext()){
           SiteProperty prop = (SiteProperty)it.next();
           prop.setSite(site);
           session.saveOrUpdate(prop);
        }
        tx.commit();
     }catch(HibernateException e){
        tx.rollback();
     }
     return "index";
  }
}

site-edit.jsp:
<s:form action="site/%{site.id}">
  <s:hidden name="_method" value="put"/>
  <s:textvalue name="site.id"/>
  <s:textvalue name="site.name"/>
  <s:iterator value="site.properties">
     <s:textvalue name="propId"/>
     <s:textvalue name="propName"/>
     <s:textvalue name="site.id"/>
  </s:iterator>
  <s:submit/>
</s:form>

my question is,i can insert/update Site entity into database, while the properties<SiteProperty> cannot save to database, even in memory,the properties data refuse to take any change i made in client.

i'm confused by form/action mapping, how does the data transfer to action when client submit the form? i traced the action's setter/getter method,there is no step into these methods,how does the request data transfer to action?

thank you.