Announcement Announcement Module
Collapse
No announcement yet.
MongoDB - Multiple constructors found for @Transient annotated field in @Document ent Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • MongoDB - Multiple constructors found for @Transient annotated field in @Document ent

    Hi

    I'm getting an error trying to use the java.util.UUID class within my @Document entity. I have read this post http://forum.springsource.org/showth...ngoDB-1.0.0.M2, which seems to solve the issue, but I cannot seem to get it implemented. I have the following settings:
    The xml setup of the mongo stuff:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    	xmlns:mongo="http://www.springframework.org/schema/data/mongo"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans 
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
          http://www.springframework.org/schema/data/mongo
          http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd">
    
    	<bean id="mongo" class="org.springframework.data.mongodb.core.MongoFactoryBean">
    		<property name="host" value="${env.E2_MONGODB_HOST}" />
    		<property name="port" value="${env.E2_MONGODB_PORT}" />
    	</bean>
    
    	<mongo:db-factory id="mongoDbFactory"
    		dbname="${env.E2_MONGODB_METADATA_DATABASENAME}" username="${env.E2_MONGODB_METADATA_USERNAME}"
    		password="${env.E2_MONGODB_METADATA_PASSWORD}" mongo-ref="mongo" />
    
    	<bean id="mappingContext"
    		class="org.springframework.data.mongodb.core.mapping.MongoMappingContext" />
    
    	<bean id="mappingMongoConverter"
    		class="org.springframework.data.mongodb.core.convert.MappingMongoConverter">
    		<constructor-arg ref="mongoDbFactory" />
    		<constructor-arg ref="mappingContext" />
    	</bean>
    
    	<bean id="metadataMongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
    		<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
    		<constructor-arg name="mongoConverter" ref="mappingMongoConverter" />
    	</bean>
    
    	<mongo:repositories
    		base-package="com.esoftsystems.e2.metadata.business.internal.mongo"
    		mongo-template-ref="metadataMongoTemplate" />
    
    </beans>
    I have a @Document entity like this

    Code:
    @Document
    public class Metadata {
      
      @Id
      private String id;
      
      @Transient
      private UUID uuid = UUID.randomUUID();
    
      private String name;
      
      @PersistenceConstructor
      public Metadata(String name) {
        this.name = name;
      }
      
      public String getId() {
        return id;
      }
    
      public String getName() {
        return name;
      }
    }
    But when starting the application I get the following error:
    Code:
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'metadataMongoRepository': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: Multiple constructors with arguments found in class java.util.UUID! Annotate one with @PersistenceConstructor explicitly to select it to be used in persistence operations.
    If I remove the UUID I can start the application fine. Does anybody have a hint to what I'm doing wrong ?

    Thanks
    Christoffer

  • #2
    UUID is not a native MongoDB type, thus we treat it like an entity by default unless you register a custom converter for it (see [0] for details). Feel free to open a ticket for including it by default.

    [0] http://static.springsource.org/sprin.../html/#d0e2654

    Comment


    • #3
      Hi

      I looks promessing, but I get the same exception of multiple UUID constructors when trying to follow the reference example:
      The @Document
      Code:
      @Document()
      public class Metadata {
        
        @Id
        private UUID uuid;
        
        private String name;
      
        public Metadata(UUID uuid, String name) {
          this.uuid = uuid;
          this.name = name;
        }
      
        public String getName() {
          return name;
        }
      
        public UUID getUUID() {
          return uuid;
        }
      }
      The read and write converters:
      Code:
      public class MetadataReadConverter implements Converter<DBObject, Metadata> {
        @Override
        public Metadata convert(DBObject source) {
          return new Metadata(UUID.fromString((String)source.get("_id")), (String)source.get("name"));
        }
      }
      public class MetadataWriteConverter implements Converter<Metadata, DBObject> {
        @Override
        public DBObject convert(Metadata source) {
          DBObject dbo = new BasicDBObject();
          dbo.put("_id", source.getUUID().toString());
          dbo.put("name", source.getName());
          return dbo;
        }
      }
      And the setup using the mongo xsd:
      Code:
        <mongo:mapping-converter id="mappingMongoConverter" >
          <mongo:custom-converters>
            <mongo:converter ref="readMetadataConverter" />
            <mongo:converter ref="writeMetadataConverter" />
          </mongo:custom-converters>
        </mongo:mapping-converter>
        
        <bean class="com.esoftsystems.e2.metadata.business.internal.mongo.MetadataReadConverter" id="readMetadataConverter"/>
        <bean class="com.esoftsystems.e2.metadata.business.internal.mongo.MetadataWriteConverter" id="writeMetadataConverter"/>
        <bean id="metadataMongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
          <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
          <constructor-arg name="mongoConverter" ref="mappingMongoConverter" />
        </bean>
      So basically no effect using these converters. Any idea on what I'm doing wrong ?

      Thank
      Christoffer

      Comment


      • #4
        What version of SD MongoDB are you using? We did some significant overhaul of the converter handling for M4 and the config you showed should work actually. Actually it should be enough to rather simply implement a UuidToStringConverter or StringToUuidConverter respectively.

        Comment


        • #5
          I have been using the build-snapshots.

          But it has totally been my fault. By mistake I blindly let eclipse import the javax.persistence.Transient annotation instead of the org.springframework.data.annotation.Transient annotation. Changing this import makes things work fine.

          Sorry and thanks for your patience and responses, I'll remember to copy imports next time I have any issues ;-)

          Comment


          • #6
            No problem, glad it worked out for you .

            Comment

            Working...
            X