title: Using SQL Server 2008 Schema with JPA
body: <p>I got a MS SQL Server (2008) database with two schemas, dbo as default for general purpose e.g. authentication and myapp for domain objects. I want to have JPA-Entitys in both Schemas. I use SpringBoot for configuration.</p>
  
  <p>Entitys tables are created in the right schema as they should, e.g. <em>myschema.job</em>, but relationship tables, e.g. <em>Job_Employee</em> are created within the default schema dbo. How can I set in whicht schema automatically created tables are stored (without changing the default schema as this just shifts the problem)?</p>
  
  <pre><code>@Entity
  @Table(schema="myschema")
  public class Job {[...]
  </code></pre>
  
  <p>My application.yml looks like:</p>
  
  <pre><code>spring:
    profiles: dev
    datasource:
      datasource1:
        url: jdbc:sqlserver://localhost;databaseName=mydb;schema=myschema
        username: SA
        password: ###
      datasource2:
        url: jdbc:sqlserver://localhost;databaseName=mydb;schema=dbo
        username: SA
        password: ###    
    jpa:
        show-sql: true
        hibernate.ddl-auto : create-drop
        properties:
          hibernate.dialect: org.hibernate.dialect.SQLServer2012Dialect
        hibernate.default_schema: dbo
  </code></pre>
  
  <p>And the datasources are Configured in</p>
  
  <pre><code>@Configuration
  @EnableJpaRepositories
  public class JPAConfiguration {
  
      @Bean
      @Primary
      @ConfigurationProperties("spring.datasource.datasource1")
      public DataSourceProperties firstDataSourceProperties() {
          return new DataSourceProperties();
      }
  
      @Bean
      @Primary
      @ConfigurationProperties("spring.datasource.datasource1")
      public DataSource firstDataSource() {
          return firstDataSourceProperties().initializeDataSourceBuilder().build();
      }
  
      @Bean
      @ConfigurationProperties("spring.datasource.datasource2")
      public DataSourceProperties secondDataSourceProperties() {
          return new DataSourceProperties();
      }
  
      @Bean
      @ConfigurationProperties("spring.datasource.datasource2")
      public DataSource secondDataSource() {
          return secondDataSourceProperties().initializeDataSourceBuilder().build();
      }
  }
  </code></pre>
  
  <p>Thanks!</p>