Announcement Announcement Module
Collapse
No announcement yet.
Working: Postgres UUID ID fields Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Working: Postgres UUID ID fields

    I was able to get Postgres UUID ID fields working with Roo 1.1M3, in case anyone is interested. This only works with Spring MVC, as the GWT scaffold currently requires Long IDs (a limitation of GWT's Record interface).

    First, you'll need the latest beta of Hibernate (3.6beta4 as of this writing). This adds java.util.UUID support.

    In your entity class, add something like the following for your ID field:
    Code:
        @Id
        @GeneratedValue(generator="system-uuid")
        @GenericGenerator(name="system-uuid", strategy = "uuid2")
        @Column(name = "id")
        @Type(type="pg-uuid")
        private UUID id;
    
        public UUID getId() {
            return this.id;
        }
    
        public void setId(UUID id) {
            this.id = id;
        }
    Roo will then delete the ID definition from the aspect file and use your definition and update your controllers, etc.

    You will also need to add String <-> UUID converters to Spring MVC. This allows Spring's IdToEntityConverter to work correctly with UUID IDs. Create two converters, something like these:

    Code:
    public class StringToUUIDConverter implements Converter<String, UUID> {
        @Override
        public UUID convert(String source) {
            return UUID.fromString(source);
        }
    }
    
    public class UUIDToStringConverter implements Converter<UUID, String> {
        @Override
        public String convert(UUID source) {
            return source.toString();
        }
    }
    Then update your webmvc-config.xml to include the converters:

    Code:
    <mvc:annotation-driven conversion-service="conversionService"/>
        
    <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        <property name="converters">
            <list>
                <bean class="com.foobar.StringToUUIDConverter" />
                <bean class="com.foobar.UUIDToStringConverter" />
            </list>
        </property>
    </bean>
    And that should do it.

  • #2
    I just found https://jira.springsource.org/browse/SPR-6696, which makes me surprised you needed to write a converter. You might like to log a bug against Roo at https://jira.springsource.org/browse/ROO (and reference this forum thread) just so Stefan can take a quick look at this when he gets back from holidays. If UUID support doesn't work out-of-the-box with Spring Framework, we should revisit the intent of SPR-6696 and open a Spring Framework ticket accordingly.

    I'm pleased you got it working in the meantime, and thanks for sharing your approach. My suggestion above is just aimed at eliminating the need for Roo users to need to go to the trouble of writing a converter.

    Cheers
    Ben

    Comment


    • #3
      I think that the bug you referenced doesn't affect this. The resolution to that bug was to add a UUID property editor. The problem I faced was with the IdToEntityConverter class, which uses the conversion service to convert a string to an identifier. Without the UUID converters, the IdToEntityConverter class cannot figure out how to call the appropriate finder method to retrieve the entity.

      Comment

      Working...
      X