Announcement Announcement Module
No announcement yet.
Is it possible that "_class" not created in a document? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Is it possible that "_class" not created in a document?


    Is there any way that we can store a document W/O "_class" filed that is auto generated for each document?


  • #2
    It is. You can simply configure a DefaultTypeMapper on MappingMongoConverter and setting the typeKey property on the Mapper to null. However, we recommend to let the converter write that information as you pretty much drop the possibility to correctly persist and retrieve subtypes, or working with interfaces inside the domain model.


    • #3
      Hi Oliver,

      I appreciated for your reply.

      I configured a DefaultTypeMapper on MappingMongoConverter and setting the typeKey property on the Mapper to null as follows:

      <bean id="defaultTypeMapper"
      class=" rt.DefaultTypeMapper">
      <property name="typeKey" value="null" />

      <bean id="defaultMongoConverter"
      class=" rt.MappingMongoConverter">
      <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
      <constructor-arg name="mappingContext" ref="mappingContext" />
      <property name="typeMapper" ref="defaultTypeMapper"></property>

      <bean id="mongoTemplate" class=" Template">
      <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
      <constructor-arg name="mongoConverter" ref="defaultMongoConverter" />
      <property name="writeResultChecking" value="EXCEPTION" />
      However, I noticed that still this results in creation of an entry in document with key ( which used to be "_class") equals to null, a value with class name ( come.xx.yy.ClassName) and type of String. Also, I noticed with configuring the default type mapper, I could not use my custom convertors any more.

      As per your recommendation, we do not have any problem to keep this filed in our database, however; when we send our documents to another system for processing, the "_class" has no usage on the other system. The complain that I have got is that why we are sending this filed when the other system is using different domain and mechanism to DE-serialize documents.

      I think the other solution is to write custom convertor for each domain, but that does not make sense.

      Unless otherwise there is not other way, I think I should be able to convince our partner, that we integrate with their system, just disregard this field.



      • #4
        Which SD Mongo version are you using? We actually have quite a few test verifying the key not being set if it is null. Beyond that configuring the TypeMapper shouldn't influence the way custom Converters are handled.

        We generally consider the type information stored as hint which type to use. So you should be able to configure a ConfigurableTypeMapper which takes a Map of type to alias to create entries like _class = 'B' and then have B resolved against a particular Java type so that the client written in another language could pretty much do the same.

        In case you still face issues I fear I need a bit more details.


        • #5
          We are using M4. Although I still have not yet tried ConfigurableTypeMapper, for now we are fine with having "_class" since our priority first is to address converter issues.

          Our main issues right now is why write converters are not working.

          I will get back on "_class" if I still face issues on this.



          • #6
            ConfigurableTypeMapper cannot be found in M5 anymore...
            I also couldn't find any information in the documentation how to use this, could you give a hint?