Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.0 M2
    • Fix Version/s: None
    • Component/s: Mapping
    • Labels:
      None

      Description

      I can't persist enums with Spring-Data-Mongo. I've got the following error-message:

      java.lang.IllegalArgumentException: can't serialize class example.Domain
      	at org.bson.BSONEncoder._putObjectField(BSONEncoder.java:205)
      	at org.bson.BSONEncoder.putObject(BSONEncoder.java:121)
      	at org.bson.BSONEncoder.putObject(BSONEncoder.java:67)
      	at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:215)
      	at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:180)
      	at com.mongodb.DBCollection.insert(DBCollection.java:72)
      	at com.mongodb.DBCollection.save(DBCollection.java:537)
      	at com.mongodb.DBCollection.save(DBCollection.java:517)
      	at org.springframework.data.document.mongodb.MongoTemplate$10.doInCollection(MongoTemplate.java:708)
      	at org.springframework.data.document.mongodb.MongoTemplate.execute(MongoTemplate.java:280)
      	at org.springframework.data.document.mongodb.MongoTemplate.saveDBObject(MongoTemplate.java:705)
      	at org.springframework.data.document.mongodb.MongoTemplate.doSave(MongoTemplate.java:605)
      	at org.springframework.data.document.mongodb.MongoTemplate.save(MongoTemplate.java:595)
      	at org.springframework.data.document.mongodb.repository.SimpleMongoRepository.save(SimpleMongoRepository.java:68)
      	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      	at java.lang.reflect.Method.invoke(Method.java:616)
      	at org.springframework.data.repository.support.RepositoryFactorySupport$QueryExecuterMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:355)
      	at org.springframework.data.repository.support.RepositoryFactorySupport$QueryExecuterMethodInterceptor.invoke(RepositoryFactorySupport.java:336)
      	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
      	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
      	at $Proxy21.save(Unknown Source)
      	at de.idealo.fe.dwh.service.SiteServiceTest.testSiteService_Alias(SiteServiceTest.java:46)
      	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      	at java.lang.reflect.Method.invoke(Method.java:616)
      	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
      	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
      	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
      	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
      	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
      	at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
      	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
      	at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
      	at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
      	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
      	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
      	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
      	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
      	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
      	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
      	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
      	at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
      	at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
      	at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
      	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
      	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
      	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
      	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
      	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
      	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
      	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
      

      The classes:

      @Document(collection = "sites")
      public class Site {
          ...
          protected Domain domain;
          ...
      }
      
      public enum Domain {
      	MY_DOMAIN_DE(Long.valueOf(1), "my.domain.de"),
      	MY_DOMAIN_COM(Long.valueOf(1), "my.domain.com");
      	
      	protected Long id;
      	protected String url;
      
      	private Domain(Long newId, String newUrl) {
      		id = newId;
      		url = newUrl;
      	}
      
      	public Long getId() {
      		return id;
      	}
      
      	public String getUrl() {
      		return url;
      	}
      	
      }
      

      I think the best way to store java-enums is to save the enum as String (using the name() method).
      When reading the enum field the MappingConverter should lookup the parameter-type (class) of the setter method of the according field and use the valueOf() method of the enum passing the stored string to it - if the parameter-type is an enum.

        Activity

        Hide
        Robert Stiller added a comment -

        One more thing: When writing an interface which extends MongoRepository it could be possible, that one will use an enum as parameter. Spring-Data-Mongo should be able to properly convert the enum using the name() method or - general speaking - to convert the enum with the same mechanism used to store the enum.

        public interface SiteService extends MongoRepository<Site, String> {
          public List<Site> getByDomain(Domain domain); //domain should be converted correct
        }
        
        Show
        Robert Stiller added a comment - One more thing: When writing an interface which extends MongoRepository it could be possible, that one will use an enum as parameter. Spring-Data-Mongo should be able to properly convert the enum using the name() method or - general speaking - to convert the enum with the same mechanism used to store the enum. public interface SiteService extends MongoRepository<Site, String > { public List<Site> getByDomain(Domain domain); //domain should be converted correct }
        Hide
        Oliver Gierke added a comment -

        Updated AbstractMongoConverter to handle enums correctly. Added unit test for AbstractMongoConverter and integration tests to verifiy repositories handling enum parameters correctly as well.

        Show
        Oliver Gierke added a comment - Updated AbstractMongoConverter to handle enums correctly. Added unit test for AbstractMongoConverter and integration tests to verifiy repositories handling enum parameters correctly as well.

          People

          • Assignee:
            Oliver Gierke
            Reporter:
            Robert Stiller
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: