Results 1 to 4 of 4

Thread: Saving Parent / Child Object Graph

  1. #1

    Default 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. #2
    Join Date
    Sep 2005
    Posts
    5

    Default

    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.

  3. #3

    Default

    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

  4. #4
    Join Date
    Sep 2004
    Posts
    346

    Default Hey.. Was their resolution on this?

    Hey.. Was their resolution on this?

Similar Threads

  1. Hibernate Long Session Per Flow?
    By akw in forum Web Flow
    Replies: 21
    Last Post: Dec 12th, 2005, 08:06 PM
  2. Replies: 2
    Last Post: Oct 10th, 2005, 05:12 PM
  3. UpgradeAcegi Security System from 0.6.1 to 0.8.3
    By mannobug in forum Security
    Replies: 3
    Last Post: Sep 23rd, 2005, 07:00 PM
  4. Replies: 3
    Last Post: Jul 27th, 2005, 10:41 AM
  5. Proposal for Object Graph Assembler
    By garpinc2 in forum Web Flow
    Replies: 6
    Last Post: May 11th, 2005, 10:37 PM

Posting Permissions

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