title: Spring Data 3.0.5 MongoDB and ElasticSearch Domain Class mixed Annotation
body: <p>I'm migrating our application from Spring Boot 1.5.9 to version 2.0.0.</p>
  
  <p>In version 1.5.9 we have successfully used mixed Annotations on several Domain Classes e.g:</p>
  
  <pre class="lang-java prettyprint-override"><code>...
  @org.springframework.data.mongodb.core.mapping.Document(collection = "folder")
  @org.springframework.data.elasticsearch.annotations.Document(indexName = "folder")
  public class Folder {
      ...
  }
  </code></pre>
  
  <p>The same approach causes probems in Spring Boot 2.0.0. When MongoDB annotatnion <strong>@DBRef</strong> is used, Spring throws exception while ElasticsearchRepository creation:</p>
  
  <pre class="lang-java prettyprint-override"><code>java.lang.IllegalStateException: No association found!
  </code></pre>
  
  <p>Here comes classes and confs</p>
  
  <p>pom.xml</p>
  
  <pre class="lang-xml prettyprint-override"><code>   ...
      &lt;properties&gt;
          &lt;java.version&gt;1.8&lt;/java.version&gt;
      &lt;/properties&gt;
  
      &lt;parent&gt;
          &lt;groupId&gt;org.springfrsamework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
          &lt;version&gt;2.0.0.RELEASE&lt;/version&gt;
      &lt;/parent&gt;
      ...
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-data-mongodb&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-data-elasticsearch&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-tomcat&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
              &lt;artifactId&gt;lombok&lt;/artifactId&gt;
              &lt;version&gt;1.16.18&lt;/version&gt;
              &lt;scope&gt;provided&lt;/scope&gt;
          &lt;/dependency&gt;
  
      &lt;/dependencies&gt;
      ...
  </code></pre>
  
  <p>Application.java</p>
  
  <pre class="lang-java prettyprint-override"><code>...
  @EnableMongoRepositories("com.hydra.sbmr.repoMongo")
  @EnableElasticsearchRepositories("com.hydra.sbmr.repoElastic")
  @SpringBootApplication
  public class Application extends SpringBootServletInitializer {
  
      public static void main(String[] args) {
          SpringApplication.run(Application.class, args);
      }
  
  }
  </code></pre>
  
  <p>Folder.java (<strong>Note this @DBRef couses exception</strong>)</p>
  
  <pre class="lang-java prettyprint-override"><code>package com.hydra.sbmr.model;
  
  import lombok.Getter;
  import lombok.Setter;
  import org.springframework.data.annotation.Id;
  import org.springframework.data.mongodb.core.mapping.DBRef;
  
  @org.springframework.data.mongodb.core.mapping.Document(collection = "folder")
  @org.springframework.data.elasticsearch.annotations.Document(indexName = "folder")
  public class Folder {
  
      @Id
      @Getter @Setter private String id;
  
      // Why MongoDB core mapping @DBRef causes java.lang.IllegalStateException: No association found! exception
      // while ElasticsearchRepository creation???
      @DBRef
      @Getter @Setter private Profile profile;
  
      @Getter @Setter private String something;
  
  }
  </code></pre>
  
  <p>Profile.java</p>
  
  <pre class="lang-java prettyprint-override"><code>package com.hydra.sbmr.model;
  
  import lombok.Getter;
  import lombok.Setter;
  import org.springframework.data.annotation.Id;
  
  @org.springframework.data.mongodb.core.mapping.Document(collection = "profile")
  public class Profile {
  
      @Id
      @Getter @Setter private String id;
  
      @Getter @Setter String blah;
  
  }
  </code></pre>
  
  <p>FolderElasticRepository.java</p>
  
  <pre class="lang-java prettyprint-override"><code>package com.hydra.sbmr.repoElastic;
  
  import com.hydra.sbmr.model.Folder;
  import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
  
  public interface FolderElasticRepository extends ElasticsearchRepository&lt;Folder, String&gt; {
  
  }
  </code></pre>
  
  <p>You can find whole mini project on GitHub: <a href="https://github.com/hydraesb/sbmr" rel="nofollow noreferrer">https://github.com/hydraesb/sbmr</a></p>
  
  <p>My question:</p>
  
  <ul>
  <li>Is there any solution that will work with mixed Annotatnions on Domain Classes (mongo and elastic) in Spring Boot 2.0.0???</li>
  </ul>