Remove _class columns from spring MongoDB

Source: Internet
Author: User

When you call the Save method of Mongotemplate, Spring-data-mongodb TypeConverter automatically adds a _class attribute to the document, and the value is the name of the class you saved. There's no harm in this design. Spring-data-mongodb is designed to convert a document to a Java object when it is converted to a specific subclass. But sometimes we don't want this field to appear, mainly because it looks more "annoying". You can solve this problem by setting the Mappingmongoconverter mongotypemapper.

The first argument to the constructor of the Defaultmongotypemapper class is the type name in MongoDB. When set to NULL, the _class property is not automatically added when saving. So I need to overwrite it.

Spring's configuration file method:

<mongo:mongo host= "localhost" port= "27017"/> <mongo:db-factory dbname= "database"/> <bean id= " Mappingcontext "class=" Org.springframework.data.mongodb.core.mapping.MongoMappingContext "/> <bean i        D= "Defaultmongotypemapper" class= "Org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper" > <constructor-arg name= "Typekey" ><null/></constructor-arg> </bean> <bean id= "Mappin Gmongoconverter "class=" Org.springframework.data.mongodb.core.convert.MappingMongoConverter "> <constru Ctor-arg name= "Mongodbfactory" ref= "mongodbfactory"/> <constructor-arg name= "Mappingcontext" ref= "MappingCon Text "/> <property name=" typemapper "ref=" Defaultmongotypemapper "/> </bean> <bean I D= "Mongotemplate" class= "org.springframework.data.mongodb.core.MongoTemplate" > <constructor-arg name= " Mongodbfactory "ref=" MongodbfacTory "/> <constructor-arg name=" Mongoconverter "ref=" Mappingmongoconverter "/> </bean>  

Annotation method:

@EnableMongoRepositories (basepackages = {"COM.DHP"}, Repositoryfactorybeanclass = Dhbmongorepositoryfac Torybean.class) @PropertySource ("Classpath:mongo.properties") @EnableMongoAuditing public class Mongoconfig extends A      bstractmongoconfiguration {@Value ("${mongo.connectionstring}") Private String connectionString;        @Value ("${mongo.dbname}") Private String dbName;        @Autowired private ApplicationContext AppContext;      @Override protected String GetDatabaseName () {return dbName; } @Override @Bean public Mongo Mongo () throws Exception {Mongoclienturi Mongoclienturi = new Mo          Ngoclienturi (connectionString);      return new Mongoclient (Mongoclienturi);  } @Override @Bean public mongotemplate mongotemplate () throws Exception {Mongodbfactory Factory            = Mongodbfactory ();        Mongomappingcontext mongomappingcontext = new Mongomappingcontext ();  Mongomappingcontext.setapplicationcontext (AppContext);          Mappingmongoconverter converter = new Mappingmongoconverter (new Defaultdbrefresolver (Factory), Mongomappingcontext);            Converter.settypemapper (new Defaultmongotypemapper (null));      return new Mongotemplate (factory, Converter); } @Bean public static Propertysourcesplaceholderconfigurer Propertyconfigindev () {return new Prope      Rtysourcesplaceholderconfigurer ();   }  }

  

When using servlet 3 initialization to ensure that the application context is added to MONGO, the exception is reported if you do not add these two sentences: org.springframework.expression.spel.SpelEvaluationException
@Autowired
Private ApplicationContext AppContext;
Mongomappingcontext.setapplicationcontext (AppContext);

Remove _class columns from spring MongoDB

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.