I'm not sure if I understand what your actual question is, nor whether
it is particularly Struts 2 related (rather than just Hibernate) - but
you might want to have a look in the CRUD demo section of the Struts 2
showcase application. Maybe you will also find this demo useful:
http://github.com/rgielen/struts2crudevolutiondemo

- René

bruno grandjean schrieb:
> Hi
> 
> I am trying to implement a simple CRUD with a OneToMany association under 
> Struts 2 / Hibernate 3.
> I have two entities Parent and Child:
> 
> @Entity
> @Table(name="PARENT")
> public class Parent {
>  private Long id;
>  private Set<Child> values = new HashSet<Child>();  
> ..
> @Entity
> @Table(name="CHILD")
> public class Child {
>  private Long id; 
>  private String name; 
> ..
> 
> I can easily create, delete Parent or read the Child Set (values) but it is 
> impossible to update Child Set.
> The jsp page (see below) reinit the values Set, no record after updating!
> Could u explain to me what's wrong?
> 
> here are my code:
> 
> @Entity
> @Table(name="PARENT")
> public class Parent {
>  private Long id;
>  private Set<Child> values = new HashSet<Child>();  
>  @Id
>  @GeneratedValue
>  @Column(name="PARENT_ID") 
>  public Long getId() {
>   return id;
>  }
>  public void setId(Long id) {
>   this.id = id;
>  }
>  @ManyToMany(fetch = FetchType.EAGER)    
>  @JoinTable(name = "PARENT_CHILD", joinColumns = { @JoinColumn(name = 
> "PARENT_ID") }, inverseJoinColumns = { @JoinColumn(name = "CHILD_ID") })  
>  public Set<Child> getValues() {  
>   return values;
>  }
>  public void setValues(Set<Child> lst) {
>   values = lst;  
>  }
> }
> 
> @Entity
> @Table(name="CHILD")
> public class Child {
>  private Long id; 
>  private String name; 
>  @Id
>  @GeneratedValue
>  @Column(name="CHILD_ID") 
>  public Long getId() {
>   return id;
>  }
>  public void setId(Long id) {
>   this.id = id;
>  }
>  @Column(name="NAME")
>  public String getName() {  
>   return name;
>  }
>  public void setName(String val) {
>   name = val;
>  } 
> }
> 
> public interface ParentDAO {
>  public void saveOrUpdateParent(Parent cl);
>  public void saveParent(Parent cl);
>  public List<Parent> listParent();
>  public Parent listParentById(Long clId);
>  public void deleteParent(Long clId);
> }
> 
> public class ParentDAOImpl implements ParentDAO {
>  @SessionTarget
>  Session session;
>  @TransactionTarget
>  Transaction transaction;
> 
>  @Override
>  public void saveOrUpdateParent(Parent cl) {  
>   try {
>    session.saveOrUpdate(cl);
>   } catch (Exception e) {
>    transaction.rollback(); 
>    e.printStackTrace();
>   }
>  }
> 
>  @Override
>  public void saveParent(Parent cl) {
>   try {
>    session.save(cl);
>   } catch (Exception e) {
>    transaction.rollback();
>    e.printStackTrace();
>   }
>  }
>  
>  @Override
>  public void deleteParent(Long clId) {
>   try {
>    Parent cl = (Parent) session.get(Parent.class, clId);
>    session.delete(cl);
>   } catch (Exception e) {
>    transaction.rollback();
>    e.printStackTrace();
>   } 
>  }
>  
>  @SuppressWarnings("unchecked")
>  @Override
>  public List<Parent> listParent() {
>   List<Parent> courses = null;
>   try {
>    courses = session.createQuery("from Parent").list();
>    } catch (Exception e) {
>    e.printStackTrace();
>   }
>   return courses;
>  }
> 
>  @Override
>  public Parent listParentById(Long clId) {
>   Parent cl = null;
>   try {
>    cl = (Parent) session.get(Parent.class, clId);
>   } catch (Exception e) {
>    e.printStackTrace();
>   }
>   return cl;
>  }
> }
> 
> public class ParentAction  extends ActionSupport implements 
> ModelDriven<Parent> {
> 
>  private static final long serialVersionUID = -2662966220408285700L;
>  private Parent cl = new Parent();
>  private List<Parent> clList = new ArrayList<Parent>(); 
>  private ParentDAO clDAO = new ParentDAOImpl();
>  
>  @Override
>  public Parent getModel() {
>   return cl;
>  }
>  
>  public String saveOrUpdate()
>  {
>   clDAO.saveOrUpdateParent(cl);  
>   return SUCCESS;
>  }
> 
>  public String save()
>  {  
>   clDAO.saveParent(cl);  
>   return SUCCESS;
>  }
> 
>  public String list()
>  {
>   clList = clDAO.listParent();
>   return SUCCESS;
>  }
>  
>  public String delete()
>  {
>   HttpServletRequest request = (HttpServletRequest) 
> ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
>   clDAO.deleteParent(Long.parseLong(request.getParameter("id")));
>   return SUCCESS;
>  }
> 
>  public String edit()
>  {
>   HttpServletRequest request = (HttpServletRequest) 
> ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
>   cl = clDAO.listParentById(Long.parseLong(request.getParameter("id")));
>   return SUCCESS;
>  }
>  
>  public Parent getParent() {
>   return cl;
>  }
> 
>  public void setParent(Parent cl) {
>   this.cl = cl;
>  }
> 
>  public List<Parent> getParentList() {
>   return clList;
>  }
> 
>  public void setParentList(List<Parent> clList) {
>   this.clList = clList;
>  }
> }
> 
> and finally the jsp page:
> 
> <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
>  pageEncoding="ISO-8859-1"%>
> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
> "http://www.w3.org/TR/html4/loose.dtd";>
> <%...@taglib uri="/struts-tags" prefix="s"%>
> <html>
> <head>
> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
> <title>Parent Registration Page</title>
> <s:head />
> <style type="text/css">
> @import url(style.css);
> </style>
> </head>
> <body>
> <s:form action="saveOrUpdateParent">
>  <s:push value="parent">
>   <s:hidden name="id" />  
>    <s:push value="values">
>    <s:iterator id="p" value="values">
>    <s:textfield label="Nom" name="values(%{id}).name" value="%{name}"/>
>         </s:iterator>
>   </s:push>
>     <s:submit />
>  </s:push>
> </s:form>
> 
> <s:if test="parentList.size() > 0">
>  <div class="content">
>  <table class="userTable" cellpadding="5px">
>   <tr class="even">
>    <th>Child(s)</th>
>   </tr>
>   <s:iterator value="parentList" status="userStatus">
>    <tr
>     class="<s:if test="#userStatus.odd == true 
> ">odd</s:if><s:else>even</s:else>">
>     <td><s:property value="values.size()" /></td>
>     <s:iterator value="values">
>      <td><s:property value="name" /></td>
>     </s:iterator>
>     <td><s:url id="editURL" action="editParent">
>      <s:param name="id" value="%{id}"></s:param>
>     </s:url> <s:a href="%{editURL}">Edit</s:a></td>
>     <td><s:url id="deleteURL" action="deleteParent">
>      <s:param name="id" value="%{id}"></s:param>
>     </s:url> <s:a href="%{deleteURL}">Delete</s:a></td>
>    </tr>
>   </s:iterator>
>  </table>
>  </div>
> </s:if>
> </body>
> </html>
> 

-- 
René Gielen
IT-Neering.net
Saarstrasse 100, 52062 Aachen, Germany
Tel: +49-(0)241-4010770
Fax: +49-(0)241-4010771
Cel: +49-(0)163-2844164
http://twitter.com/rgielen

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org

Reply via email to