Announcement Announcement Module
Collapse
No announcement yet.
Saving Parent / Child Object Graph Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Saving Parent / Child Object Graph

    I seem to be having issues saving a basic Parent / Child object graph using Spring & Hibernate. My Parent table (called item) has a single Primary Key called item_id. My Child table (called item_payment_type) has a Composite Key, consisting of item_id (a foreign key to the Parent table) and payment_type_id (a foreign key to another table called payment_types). My Child table is simply an association table between the Parent (item) and the payment_types table.

    The item_id value is automatically generated from the database upon record insert into the item table (I'm using MySQL). When Hibernate attempts to persist the entire object graph, Hibernate throws an exception indicating that the item_id in the Child cannot be null, which I completely understand, however, I was under the assumption that Hibernate would automatically propegate the item_id down to the Child records upon insert of the Parent. I obviously cannot set this value when building out the object graph b/c I do not have this value yet.

    Also, In my .hbm docs I have cascade set to "all" and I have inverse set to "true". I'm also setting the Child records to my Parent, and I'm also setting the Parent to each Child record.

    Has anybody out there run into this issue or have any suggestions?

    Thanks,

    Matt

  • #2
    Can you post the code how you do the saving Item Object?

    For example, I would do the following: The itemDao object here is a spring DAO implementation

    item.addToItemPaymentTypes(itemPaymentType);
    itemPaymentType.setItem(item);

    itemDao.save(item);
    itemDao.flushSession();


    Are which DAO object are you calling to do the save? If you are calling the child DAO to do the save, I think you get your current problem. Since child won't cascade save the parent.

    Comment


    • #3
      floralee,

      Thank you very much for your reply, I more than appreciate it!

      Your sample code above is almost exactly how I am currently saving the object graph.

      In my Controller in the onSubmit method:
      Code:
      Item item = (Item) command;
      
      String[] paymentsTypesFromJsp= request.getParameterValues("paymentTypes");
      
      Set paymentTypes = new HashSet();
      
      for &#40;int i = 0; i < paymentsTypesFromJsp.length; i++&#41; &#123;
      			
        ItemPaymentTypeId itemPaymentTypeId = new ItemPaymentTypeId &#40;&#41;;
        itemPaymentTypeId.setPaymentTypeId&#40;new Integer&#40;paymentsTypesFromJsp&#91;i&#93;&#41;&#41;;
        //itemPaymentTypeId.setItemId&#40;IdoNotHaveThisValue&#41;;
        ItemPaymentType itemPaymentType = new ItemPaymentType&#40;&#41;;
        itemPaymentType.setId&#40;itemPaymentTypeId&#41;;
        itemPaymentType.setItem&#40;item&#41;;
        paymentTypes.add&#40;itemPaymentType&#41;;
      
      &#125;
      
      item.setPaymentTypes&#40;paymentTypes&#41;;
      itemService.saveOrUpdate&#40;item&#41;;
      The Service layer then calls through to the Hibernate Item DAO:
      Code:
      getHibernateTemplate.saveOrUpdate&#40;item&#41;;
      Also, I do not have a separate DAO for ItemPaymentType.

      The error that I'm receiving is something like this:

      "Hibernate could not execute batch update because item_id cannot be null" - referencing the item_id in the ItemPaymentTypeId object, which I cannot set b/c I do not have this value when building out the object graph.

      Again, thanks for any help, I more than appreciate it - this one has me stumped!

      Thanks,

      Matt

      Comment


      • #4
        Hey.. Was their resolution on this?

        Hey.. Was their resolution on this?

        Comment

        Working...
        X