title: Spring Data - hibernate.cfg.xml in different package
body: <p>I'm new to Spring, and trying to create a hexagonal architecture with spring data </p>
  
  <p>I have a gradle multi-module setup with on package for the domain, one for persistence, and another for configuration, where the DddApplication lives.</p>
  
  <p>When starting it up, I get <code>Not a managed type: class com.example.ddd.domain.model.Customer</code>, unless I annotate the class with  javax.persistence.@Entity etc. </p>
  
  <p>However I don't want annotations in the class. I had set up entities in hibernate.cfg.xml and Customer.hbm.xml in the <code>persistence</code> package.</p>
  
  <p>I think spring boot isn't using hibernate.cfg.xml from the other package.</p>
  
  <p>Is there any way I can configure Spring Boot to recognise the entities based on the hibernate.cfg.xml in the <code>persistence</code> package?</p>
  
  <pre><code>package com.example.ddd.configuration
  
  // ...
  
  @SpringBootApplication
  @EntityScan("com.example.ddd.domain.model")
  @EnableJpaRepositories("com.example.ddd.persistence.repository")
  class DddApplication {
      private val log: Logger = LoggerFactory.getLogger(DddApplication::class.java)
  
      @Bean
      fun loadData(repository: CustomerRepositoryJpa): CommandLineRunner? {
          return CommandLineRunner { args: Array&lt;String?&gt;? -&gt;
  //...
          }
      }
  }
  
  fun main(args: Array&lt;String&gt;) {
      runApplication&lt;DddApplication&gt;(*args)
  }
  </code></pre>
  
  <pre><code>package com.example.ddd.persistence.repository
  
  //...
  
  @Repository
  interface CustomerRepositoryJpa : JpaRepository&lt;Customer?, Long?&gt; {}
  </code></pre>
  
  <pre><code>package com.example.ddd.domain.model
  
  open class Customer private constructor() {
      val id: Long? = null
      lateinit var firstName: String
      lateinit var lastName: String
      override fun toString(): String {
          return String.format("Customer[id=%d, firstName='%s', lastName='%s']", id,
                  firstName, lastName)
      }
  
      companion object {
          fun new(firstName: String, lastName: String) : Customer {
              val e = Customer()
              e.firstName = firstName
              e.lastName = lastName
              return e
          }
      }
  }
  </code></pre>
  
  <p>application.properties and hibernate.cfg.xml are in the resources of the <code>persistence</code> package.</p>