2012-09-12 16 views
5

私のenumはmongodb(C#appから)にintとして格納されています。今ではJavaで、私はそれらを取得しようとすると、例外をスローします(それは列挙型からのみ変換できます)。私はそれを行うことができる方法はありますか?Spring - Enumを文字列として格納/取得しているモンゴル

また、mongodb(Javaから)にいくつかのコレクションを保存すると、列挙型の値が文字列に変換されます(値/基数ではありません)。利用可能なオーバーライドはありますか?

これは、クラスレベルでmongodb-converterを書くことで実現できますが、これらの列挙型はさまざまなクラスにあるため、クラスごとにmondodb-converterを記述したくありません。

フィールドレベルには何かがありますか?

答えて

8

、 iが終了し、今では簡単な解決策がある場合、私は幸せになることにも表示されます(ここではそれがある:)働いて[OK]を、これは私がやった):

最初の定義:

public interface IntEnumConvertable { 
     public int getValue();  

}

、簡単な列挙型が実装しますそれは:

public enum tester implements IntEnumConvertable{ 
    vali(0),secondvali(1),thirdvali(5); 

    private final int val; 
    private tester(int num) 
    { 
     val = num;   
    } 
    public int getValue(){ 
     return val; 
    } 
} 

OK]をクリックして、今、あなたは今2コンバータが必要になります、一方が他方には、より複雑である 、簡単です。単純なもの(この単純な赤ちゃんも簡単な変換を扱い、キャストが不可能なときに文字列を返します。列挙型の列挙型を列挙型にしたい場合には大丈夫です)。

public class IntegerToEnumConverterFactory implements ConverterFactory<Integer, Enum> { 
     @Override 
     public <T extends Enum> Converter<Integer, T> getConverter(Class<T> targetType) { 
      Class<?> enumType = targetType; 
      while (enumType != null && !enumType.isEnum()) { 
       enumType = enumType.getSuperclass(); 
      } 
      if (enumType == null) { 
       throw new IllegalArgumentException(
         "The target type " + targetType.getName() + " does not refer to an enum"); 
      } 
      return new IntegerToEnum(enumType); 
     } 
     @ReadingConverter 
     public static class IntegerToEnum<T extends Enum> implements Converter<Integer, Enum> { 
      private final Class<T> enumType; 

      public IntegerToEnum(Class<T> enumType) { 
       this.enumType = enumType; 
      } 

      @Override 
      public Enum convert(Integer source) { 
        for(T t : enumType.getEnumConstants()) { 
         if(t instanceof IntEnumConvertable) 
         { 
          if(((IntEnumConvertable)t).getValue() == source.intValue()) { 
           return t; 
          }       
         }      
        } 
        return null; 
      } 
     } 
} 

となりましたハックの一部のために、私はmongoConverter内のコンバータの工場を登録するための任意の「programmitacly」方法を見つけるdidntのpersonnalyので、私:

public class IntegerEnumConverters { 
    @WritingConverter 
    public static class EnumToIntegerConverter implements Converter<Enum<?>, Object> { 
     @Override 
     public Object convert(Enum<?> source) { 
      if(source instanceof IntEnumConvertable) 
      { 
       return ((IntEnumConvertable)(source)).getValue(); 
      } 
      else 
      { 
       return source.name(); 
      }    
     } 
    } 
} 

、より複雑なものは、実際には、コンバータの工場ですコードを掘り出して少しキャスティングして、ここに(あなたの@Configurationクラスにこの2つの赤ちゃん関数を入れて)

 @Bean 
     public CustomConversions customConversions() { 
      List<Converter<?, ?>> converters = new ArrayList<Converter<?, ?>>(); 
      converters.add(new IntegerEnumConverters.EnumToIntegerConverter());  
// this is a dummy registration , actually it's a work-around because 
// spring-mongodb doesnt has the option to reg converter factory. 
// so we reg the converter that our factory uses. 
converters.add(new IntegerToEnumConverterFactory.IntegerToEnum(null));  
      return new CustomConversions(converters); 
     } 

    @Bean 
    public MappingMongoConverter mappingMongoConverter() throws Exception { 
     MongoMappingContext mappingContext = new MongoMappingContext(); 
     mappingContext.setApplicationContext(appContext); 
     DbRefResolver dbRefResolver = new DefaultDbRefResolver(mongoDbFactory()); 
     MappingMongoConverter mongoConverter = new MappingMongoConverter(dbRefResolver, mappingContext);   
     mongoConverter.setCustomConversions(customConversions());  
     ConversionService convService = mongoConverter.getConversionService(); 
     ((GenericConversionService)convService).addConverterFactory(new IntegerToEnumConverterFactory());     
     mongoConverter.afterPropertiesSet(); 
     return mongoConverter; 
    } 
+1

ありがとうございます。私はLowerStringToEnumConverterを実装するアイデアを使用しました – Demwis

+1

ありがとうございました!それは非常に春のデータは簡単な方法を提供していないことが有線です – ruX

+0

これは非常に有用だった。 –

2

カスタムコンバータを実装し、それを春に登録する必要があります。春 - MongoDBの変換コードで長い掘削した後

http://static.springsource.org/spring-data/data-mongo/docs/current/reference/html/#mongo.custom-converters

+0

これはクラスレベルで動作します。**私はすでに質問の中でこれを言及したくありません。 – gsagrawal

+0

あなたはそれを試してみましたか?私はそれもフィールドのために働くと思う。 – gkamal

+2

他のオプションは、変換を行うエンティティにintのための別のゲッター/セッターを追加することです。 enumのgetter/setterは@Transientとしてマークする必要があります。 – gkamal

関連する問題