have you got the correct cascade setting on the parent object
 
i use the xml files for hibernate rather than the @ syntax in java classes and 
i can set cascade="all" which means when i save/update the parent the children 
are also saved or updated.
 
also remember that for hibernate to realise the children need to be updated 
rather than saved, the child object needs to have a primary key set otherwise 
it will want to do a save ... and if cascade was only update then nothing would 
be done with child objects.
 
turn on debugging of sql in hibernate config file to see if any sql to 
update/save children is actually being created.
 


----------------------------------------
> From: brgrandj...@live.fr
> To: user@struts.apache.org
> Subject: Re: CRUD with a OneToMany association under Struts 2 / Hibernate 3
> Date: Wed, 31 Mar 2010 22:15:34 +0200
>
> Dear Rene,
>
> Thks a lot for replying to me because I am feeling a little bit alone with
> my CRUD ;-). In fact I am trying to build a dynamic MetaCrud.
> My pb is simple: in the same jsp page I would like to update a Parent object
> and its Childs (values):
>
> 
> 
>> name="name" label="Nom" />
> 
> 
> 
> 
> 
> 
> 
> 
>
> From an existing Parent object with many Childs objects I can easily modify
> parent.name for instance but the collection of Child objects (values) is
> always empty in the ParentAction (saveOrUpdate() method) after submitting.
> However I can display each values[i].name in the jsp page with the correct
> value.
> So it is not an issue with Hibernate but with the jsp or ModelDriven
> interface I don't know..Do you have any idea?
> Basically I was not able to find a struts or spring documentation about CRUD
> & association between two entities on the same jsp page.
> best regards
> bruno
>
>
>
> --------------------------------------------------
> From: "Rene Gielen" 
> Sent: Wednesday, March 31, 2010 7:12 PM
> To: "Struts Users Mailing List" 
> Subject: Re: CRUD with a OneToMany association under Struts 2 / Hibernate 3
>
>> 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 values = new HashSet();
>>> ..
>>> @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 values = new HashSet();
>>> @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 getValues() {
>>> return values;
>>> }
>>> public void setValues(Set 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 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 listParent() {
>>> List 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 {
>>>
>>> private static final long serialVersionUID = -2662966220408285700L;
>>> private Parent cl = new Parent();
>>> private List clList = new ArrayList();
>>> 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 getParentList() {
>>> return clList;
>>> }
>>>
>>> public void setParentList(List clList) {
>>> this.clList = clList;
>>> }
>>> }
>>>
>>> and finally the jsp page:
>>>
>>> 
>>>>>> "http://www.w3.org/TR/html4/loose.dtd";>
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 


>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>>>
>>> 
>>> 

>>> 

>>> 


>>> 
Child(s)
>>> 

>>> 
>>>>>> class="oddeven">
>>> 


>>> 
>>> 

>>> 
>>> 

>>> 
>>> Edit
>>> 

>>> 
>>> Delete
>>> 

>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>>>
>>
>> --
>> 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
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
> For additional commands, e-mail: user-h...@struts.apache.org
>                                         
_________________________________________________________________
We want to hear all your funny, exciting and crazy Hotmail stories. Tell us now
http://clk.atdmt.com/UKM/go/195013117/direct/01/
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org

Reply via email to