0

In spring documentation talking about Customizing type mapping Reference

Using @TypeAlias and TypeInformationMapper .. but I cannot find any practical example
can someone please reference?

When defining MappingMongoConverter it is related the entities persistence?

Thanks

assaf_miz84
  • 687
  • 2
  • 15
  • 33

1 Answers1

0

When you persist an entity using spring-data then mongo document created will have an _class attribute which stores the fully qualified name of the class. The @TypeAlias is to customize the value saved in the _class attribute.

This example from the spring reference shows how the _class attribute is added to the mongo document. If you attach a @TypeAlias("sample) then the _class attribute will have the value "sample" instead of the fully qualified name.

public class Sample {
  Contact value;
}

public abstract class Contact { … }

public class Person extends Contact { … }

Sample sample = new Sample();
sample.value = new Person();

mongoTemplate.save(sample);

{ "_class" : "com.acme.Sample",
  "value" : { "_class" : "com.acme.Person" }
}
gkamal
  • 20,777
  • 4
  • 60
  • 57