Announcement Announcement Module
Collapse
No announcement yet.
What should be the primary key data type on Flex side? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • What should be the primary key data type on Flex side?

    I have Hibernate, Spring, BlazeDS and a Flex client.

    My domainObject1 on the server has a primary key that is an Integer. It also contains a HashSet of other domain objects (domainObject2, domainObject3 etc).

    On the Flex client the primary key is an int for all of the objects.

    When I send a new object from the Flex client to the server to be saved, domainObject1 is converted from a Flex object to a Java object, and is given a primary key value of 0. The same happens for each of the domainObjects in the Flex ArrayCollection. domainObject1 is saved correctly by Hibernate, but the other domainObjects throw an UnexpectedRollbackException. They do this because Hibernate seem to think that these domainObjects already exist in the database. I know this, because if I set there primary key to null before saving, everything works fine (I also see Hibernate trying to update the object, rather than saving it).

    In the past I have used the DTO layer to get around this and simply checked if the primary key being given to each domainObject was greater than 0 before setting it. I am not using a DTO layer anymore. I am therefore wondering how to get around this problem. Is it a bug in the Spring/Flex framework, or am I doing something wrong?

    Thanks for any tips.
    Last edited by klogger; Feb 11th, 2011, 06:05 AM. Reason: Added solved to title

  • #2
    I had a couple of thoughts:

    1) This isn't a problem in the Spring/Flex framework. Flex has to use an int for the primary key field. When it sends the object to the server, Spring/Flex has no way of knowing if it should be a null or a 0. If Flex had an Integer, it would make things easier;

    2) Hibernate is saving my domainObject1 correctly, even though it has an id of 0 associated with it. I don't know why it won't save the collection though, as it has the same value of 0 for the primary key fields. I guess that makes it a Hibernate problem, but I am not sure.

    Comment


    • #3
      I need to use the 'unsaved-value' property in the .hbm.xml file and set that to 0.

      Unfortunately, I am using reveng.xml in order to generate the hmb.xml files. Does anyone know how to insert the 'unsaved-value' property. It isn't covered in the manual.

      Comment


      • #4
        When it sends the object to the server, Spring/Flex has no way of knowing if it should be a null or a 0
        You are not assigning any value to primary key of the domain object on flex-side? If so whats the purpose of that field on flex-side?

        I am not using a DTO layer anymore
        Can't you tell hibernate to auto increment the primary key value? then it wont be null, plus it will be unique

        Comment


        • #5
          Thanks for the reply.

          You are not assigning any value to primary key of the domain object on flex-side? If so whats the purpose of that field on flex-side?
          It is a newly created object, and so won't get an id value until after it is saved on the server. Once the object is saved, it is returned and the id updated on the client so that the user can then edit the object. The problem is occuring during the saving of the object, and hence I can never return the id.

          Can't you tell hibernate to auto increment the primary key value? then it wont be null, plus it will be unique
          Hibernate is using the identity generator, and so the primary key will auto-increment. The problem is that for some reason Hibernate thinks the object has already been saved as the Spring/Flex framework assigns an id value of 0 to the object at the conversion stage. When I manually set the id of the objects in the set to null before calling save, everything works fine. That is a little cumbersome though. Setting the unsaved-value to 0 gets around this problem, but I am not sure how to do that from Hibernates Reverse Engineering (reveng.xml).

          Comment


          • #6
            I hacked the Hibernate template in the end. Details of how I did it are here.

            Comment

            Working...
            X