title: Specify username, password and connection URL for embedded database in Hibernate/JPA2.1
body: <p>I have this database defined in java config as follows:</p>
  
  <pre><code>@Bean
  public DataSource dataSource() {
      return new EmbeddedDatabaseBuilder()
              .setType(EmbeddedDatabaseType.H2)
              .addScript("classpath:/db/sql/create-db.sql")
              .addScript("classpath:/db/sql/insert-data.sql")
              .build();
  }
  </code></pre>
  
  <p>I need to specify username and password for this configuration, but they are hardcoded somewhere deep inside this builder's classes. How can I specify my custom connection details?</p>
  
  <p>If it matters, my full class looks as follows:</p>
  
  <pre><code>@Configuration
  public class DatabaseConfig {
  
      @Resource
      Environment environment;
  
      @Bean
      public NamedParameterJdbcTemplate jdbcTemplate(DataSource dataSource) {
          return new NamedParameterJdbcTemplate(dataSource);
      }
  
      @Bean
      public DataSource dataSource() {
          return new EmbeddedDatabaseBuilder()
                  .setType(EmbeddedDatabaseType.H2)
                  .addScript("classpath:/db/sql/create-db.sql")
                  .addScript("classpath:/db/sql/insert-data.sql")
                  .build();
      }
  
      @Bean
      public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
  ...
      }
  
      @Bean
      public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) throws Exception {
          HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
  
          LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
  ...
          factory.setDataSource(dataSource);
  ...
          return factory;
      }
  
      Properties jpaProperties() {
          Properties props = new Properties();
          ...
          return props;
      }
  }
  </code></pre>
  
  <p>and my main configuration file as follows:</p>
  
  <pre><code>@Configuration
  @ComponentScan({"com.myapp.conf"})
  @EnableAsync
  @EnableScheduling
  @EnableJpaRepositories(basePackageClasses = {CustomerRepository.class})
  @EnableTransactionManagement
  @PropertySources({
          @PropertySource("classpath:app.properties")
  })
  public class ApplicationConfig {
  ...
  }
  </code></pre>