Announcement Announcement Module
Collapse
No announcement yet.
Proper @Id mapping from .java to .as file Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Proper @Id mapping from .java to .as file

    I've already kinda asked this attached to this thread ...
    http://forum.springsource.org/showthread.php?t=89412

    But its really a separate issue than what that thread was about.

    Question is if in a Java JPA entity I have

    Code:
    @Id
    private Long id;
    In actionscript what sould I have?

    In JPA hibernate the unsaved-value mapping for a Long identifier seems to be 'null' (I can't figure out how to change it on the java side) But I don't know of an Actionscript type that can pass back 'null' it seems to always use 0 which then makes the java side think its a detached instance.

    In Spring-Flex prior to 1.5 SNAPSHOTS I could just use long(primitive) on the java side, but now the conversion stuff in Spring-Flex doesn't handle long so just thinking im missing something simple here?

    Cheers,
    Steve

  • #2
    Found it,

    browsing through the recent commits for 1.5 I see a class JpaNumericAutogeneratedIdConverter.java that converts the numeric value coming from Flex to 'null' if its value is less than 0 so for the Flex side I can just make my id as ...

    Code:
    var _id:Number = -1;
    and the -1 will be converted on the Java side.... Nice

    Comment


    • #3
      Glad you found it. Sorry for the lack of response...been ignoring the forums a lately in the press to get M1 out.

      The next thing we'll probably need to address is if any special handling is needed for other types of @Id's, including composites. Haven't given it too much thought yet, but will open some further Jira's along these lines when I'm at it next week. Feel free to open some issues yourself if you come across any gaps or have any additional suggestions.

      Comment


      • #4
        Thanks for the response, I will probably be fine with just long id's at the moment ... though String is another type I use sometimes (when using UUID's for keys) just if I'm trying to obfuscate the ID's a bit.

        What I do want to try next though is carrying on with the excluding fields from serialization, as in my other thread. I've had a look around and would like to take a crack at putting something in like an @AmfIgnore annotation, so I can control some parts of the Object graph that I never want to send back to the Flex side. (I'll try and feed back anything useful I come up with)

        Cheers,
        Steve

        Comment


        • #5
          I've started a thread about values under 0 from flex will be converted on the Java side to null and after reading this thread, I've managed to write my own JpaNumericAutogeneratedIdConverter.java to keep the negative value from being null.

          So basically, I extends JpaNumericAutogeneratedIdConverter as follow :

          Code:
          package org.springframework.flex.core.io;
          
          import org.springframework.core.convert.TypeDescriptor;
          import org.springframework.core.convert.support.PropertyTypeDescriptor;
          import org.springframework.util.NumberUtils;
          
          import javax.persistence.Id;
          import javax.persistence.GeneratedValue;
          import java.util.HashSet;
          import java.util.Set;
          
          public class MyJpaNumericAutogeneratedIdConverter extends JpaNumericAutogeneratedIdConverter {
              
              @SuppressWarnings("unchecked")
              @Override
              public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
                  Number numberValue = (Number) source;
                  if (numberValue.intValue() == 0) {
                      return null;
                  } else if (!sourceType.getType().equals(targetType.getType())){
                      return NumberUtils.convertNumberToTargetClass(numberValue, (Class<? extends Number>)targetType.getType());
                  }
                      return source;
              }
          }
          and also JpaHibernateConfigProcessor as follow :

          Code:
          package org.springframework.flex.core.io;
          
          import org.springframework.core.convert.ConversionService;
          import org.springframework.core.convert.support.GenericConversionService;
          
          public class MyJpaHibernateConfigProcessor extends JpaHibernateConfigProcessor {
              @Override
              protected ConversionService getDefaultConversionService() {
                  GenericConversionService conversionService = (GenericConversionService) super.getDefaultConversionService();
                  conversionService.addConverter(new MyJpaNumericAutogeneratedIdConverter());
                  return conversionService;
              }
          }
          I update my flex-config.xml to use the configprocessor :

          Code:
          <?xml version="1.0" encoding="UTF-8"?>
          <beans xmlns="http://www.springframework.org/schema/beans"
          	xmlns:flex="http://www.springframework.org/schema/flex"
          	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          	xsi:schemaLocation="
          		http://www.springframework.org/schema/beans
          		http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
          		http://www.springframework.org/schema/flex 
          		http://www.springframework.org/schema/flex/spring-flex-1.0.xsd">
            	
          	<flex:message-broker services-config-path="/WEB-INF/flex/services-config.xml">
                  <flex:config-processor ref="myJpaConfigProcessor"/>
          		<flex:secured/>
          	</flex:message-broker>
          
              <bean id="myJpaConfigProcessor" class="org.springframework.flex.core.io.MyJpaHibernateConfigProcessor"/>
          	
          </beans>
          When I started the application, MyJpaHibernateConfigProcessor.class is correctly create and add MyJpaNumericAutogeneratedIdConverter.class to the conversionService.
          My problem is that the override method convert() of MyJpaNumericAutogeneratedIdConverter.class is never call !!!

          Am I missing something ???

          Thx for any advices

          Comment


          • #6
            Originally posted by steph007 View Post
            When I started the application, MyJpaHibernateConfigProcessor.class is correctly create and add MyJpaNumericAutogeneratedIdConverter.class to the conversionService.
            My problem is that the override method convert() of MyJpaNumericAutogeneratedIdConverter.class is never call !!!
            Sorry for such a delayed response, but the problem is likely the fact that the original JpaNumericAutogeneratedIdConverter is still getting registered (and taking precedence because it was registered first) by your call to the superclass method. I'd suggest for now instead of calling the super method, just start with the same code from HibernateConfigProcessor.getDefaultConversionServi ce and add in your converter. I'll try to make this a bit easier in the future by adding another template method to potentially be overridden.

            Comment


            • #7
              thank you for your reply jeremy but it didn't work neither as it should.

              After some search, I found the following register bug FLEX-147.
              This is exactly what is happening and following the same method as described, let me use my own ConfigProcessor.

              Hope that it will save you a lot of debug time.

              Comment

              Working...
              X