Announcement Announcement Module
No announcement yet.
saving data via hibernate Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • saving data via hibernate

    hello everybody

    we are using struts, spring and hibernate although there are some problems occuring, specially one when i try to save in a struts action -
    this is just a testcase but i want to load data and save the modified data again in one action - as i said just a test.

    after removing all errors the actions seems to be doing what i want - except hibernate does not save the object.
    my structure is the following:

    group table - text table
    from group to text there is a ony-to-many relation
    when i get back the set i want to modify an item in the related text object and save the group item.
    after saving i get back the correct item with the modified value but in the database the old value remains.
    do i have to commit or something like that?
    i think spring handles session and transactions by it own, doesn't it?

    maybe somebody could help me in this case -
    thanx in advance



  • #2
    i think spring handles session and transactions by it own, doesn't it?
    You need to configure declarative transaction management, using TransactionProxyFactoryBean or the like. Please see examples such as the PetClinic and PetStore.

    Your Struts action is presumably not in a transaction scope. In general you should have transaction boundaries around business objects, and they shouldn't span into the web tier.


    • #3
      That is similar problem I'm having too. I'm using TransactionManger. I'm deleting the existing records, from the "Groups" table which is many-to-one to the "Users" table, before I add the new modified data record to the Groups table again.

      After save, it returns the object with the correct modified values, but the database still has the old values


      • #4
        Correction to my last post...

        After save, it returns the object with the correct modified values.
        However, when I look in the database, the records were deleted but the new records were never added.


        • #5
          Check your cascade="" setting of the parent object in the .hbm.xml file. If you don't want to specifically saveOrUpdate for each child object, the parent will need a cascade setting like "all-delete-orphan". There are half a dozen cascade settings to fine-tune the exact desired persistence by reachability - check the Hibernate documentation.


          • #6
            Now that I can add, update and delete a record in the User table but I cannot add or update to the collection that is related to that user record.

            When I delete a user all the roles related to that user get deleted too (which is what I expected it to do).

            Yet, why can I not add or update the collection (roles) of a user?

            The relationship of User to Role table is one-to-many and is set to lazy=true.

            Can someone help? I just want to be able to add a new user and update an existing user such that the roles will get added or updated too.

            Thank you


            • #7
              You may well get an answer here, but at this point you'll have a better chance of getting a reply if you go to the Hibernate forums, since this is pretty much a Hibernate usage issue as far as I can tell.