title: Using @BsonDiscriminator and @BsonProperty with Spring Data Mongo. Is it possible?
body: <p>I need to start using <code>@Document</code> annotation (<code>org.springframework.data.mongodb.core.mapping.Document</code>) on a class which is used as an entity for <code>mongodb</code>. This class currently uses the <code>@BsonDiscriminator</code> annotation (<code>org.bson.codecs.pojo.annotations.BsonDiscriminator</code>) and the <code>@BsonProperty</code> annotation (<code>org.bson.codecs.pojo.annotations.BsonProperty</code>).</p>
  <p>Can I continue to use <code>@BsonDiscriminator</code> and <code>@BsonProperty</code>?<br />
  Is it correct to keep using them together with the <code>@Document</code> annotation?<br />
  Maybe Spring has its own similar annotations?</p>
  <p>I would not want to completely abandon these annotations, since the application is already running and I would like to keep the structure of the classes that are written to the database as close as possible. The ideal would be only add the ability to use the spring.</p>
  <p>My classes now:</p>
  <pre class="lang-java prettyprint-override"><code>@Data
  @NoArgsConstructor
  @BsonDiscriminator(value = &quot;RealEstateDocument&quot;)
  public class RealEstateDocument {
    @BsonId
    private String id;
    
    @BsonProperty(&quot;create&quot;)
    private Instant creationTimestamp;
  
    private RealEstateType type;
    
    @BsonProperty(useDiscriminator = true)
    private AddressDocument address;
    
    private String addressRaw;
  }
  
  public enum RealEstateType {
    REAL_ESTATE(null, &quot;&quot;),
    REAL_ESTATE_LAND(REAL_ESTATE, &quot;land&quot;),
    REAL_ESTATE_BUILDING(REAL_ESTATE, &quot;building&quot;);
  
    private final RealEstateType parent;
    public final String title;
  }
  
  @Data
  @BsonDiscriminator(value = &quot;AddressDocument&quot;)
  public class AddressDocument {
    @BsonProperty(value = &quot;country&quot;, useDiscriminator = true)
    private CountryDocument country;
  
    @BsonProperty(value = &quot;federalDistrict&quot;)
    private String federalDistrict;
  }
  
  @Data
  @BsonDiscriminator(value = &quot;CountryDocument&quot;)
  public static class CountryDocument {
    @BsonProperty(&quot;name&quot;)
    private String name;
  }
  </code></pre>