Results 1 to 6 of 6

Thread: To save multiple form copies through Hibernate

  1. #1

    Default To save multiple form copies through Hibernate

    Hi,

    I am using Spring MVC and Hibernate. I have a form with various fields including name, age, address etcc. I would like to save the form multiple times for different user names but the rest of the fields in the form are common to all the users. When I try to use a loop and call saveorupdate of hibernate, only one form gets saved and am not able to save all.
    How do I resolve this problem. Can this be solved via cloning the objects? If so where should I clone the objecst? Is it in a class where i implement the AbstractWizardFormController class? Any help would be greatly appreciable! Is there any other way!

    Thx in advance.

  2. #2

    Default

    Hi
    Code:
    for (int i=0; i < sampleName.length; i++)
               {
                   String tmp = sampleName[i];                
                   Sample formCopy = (Sample)sample.clone();
                   formCopy.getDetail().setName(tmp);   
                   model.put("form", formCopy); 
                   System.out.println ("Tmp is:" + sample.getName());
                   this.getSampleManager().saveOrUpdateSample(formCopy);  
    }
    I used the above code to save my form multiple times. The problem is all the form gets saved in the same name and name doesn't change. How do I get rids of this!
    And also there seems to be some problem while deleting my records as it says "child record...available"
    Thx.

  3. #3
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    Have you implemented the clone method yourself? Make sure that you clone everything, create a net Detail object etc. Don't reuse anything from the old object. If that happens Hibernate detects it as an already managed object and updates it if it finds any changes.

    I would simply create a new Sample object before saving instead, that ensures that you have a non-managed object.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  4. #4
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    Quote Originally Posted by mdeinum View Post
    I would simply create a new Sample object before saving instead, that ensures that you have a non-managed object.
    You could always add a copy constructor to make this a little nicer.
    Last edited by karldmoore; Aug 29th, 2007 at 10:28 AM.
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

  5. #5

    Default

    Yes, thx for ur response. I did try with creating new sample everytime. And I could save records in teh database. but while trying to delete any record I get an exception like this.

    Code:
    The system was unable to delete the samples!
    
    org.springframework.dao.DataIntegrityViolationException: Hibernate operation: Could not execute JDBC batch update; SQL [delete from SCSF_QC where QC_ID=?]; ORA-02292: integrity constraint (DEV_SCSF_DB.FKE1EB38C63F64C6E2) violated - child record found
    ; nested exception is java.sql.BatchUpdateException: ORA-02292: integrity constraint (DEV_SCSF_DB.FKE1EB38C63F64C6E2) violated - child record found
    
    java.sql.BatchUpdateException: ORA-02292: integrity constraint (DEV_SCSF_DB.FKE1EB38C63F64C6E2) violated - child record found
    Can anyone suggest why this occurs?

  6. #6
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    I would presume you might have a foreign key reference to another object, but you aren't cascading the delete.
    Last edited by karldmoore; Aug 29th, 2007 at 10:28 AM.
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •