1

I've created a new project with Spring Initializr and I configured connection with SSMS database:

spring.datasource.url=jdbc:sqlserver://xxxxxxxx;databaseName=yyyyyy
spring.datasource.username=
spring.datasource.password=
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.jpa.show-sql=true
spring.jpa.hibernate.dialect=org.hibernate.dialect.SQLServer2012Dialect
spring.jpa.hibernate.ddl-auto=update

I also created a test model class to check everything works fine:

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(callSuper = false)
@ToString
@Builder
@Entity
@Table(name = "USR", schema = "dbo")
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer usrId;
}

This is my console output:

 .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.4.1)

2020-12-21 21:15:06.574  INFO 22192 --- [  restartedMain] c.e.employees.EmployeesApplication       : Starting EmployeesApplication using Java 11.0.8 on DESKTOP-OEEHFPE with PID 22192 (C:\Alina\employees\target\classes started by olaru in C:\Alina\employees)
2020-12-21 21:15:06.577  INFO 22192 --- [  restartedMain] c.e.employees.EmployeesApplication       : The following profiles are active: @spring.profiles.active@
2020-12-21 21:15:06.637  INFO 22192 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2020-12-21 21:15:07.211  INFO 22192 --- [  restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2020-12-21 21:15:07.226  INFO 22192 --- [  restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 5 ms. Found 0 JPA repository interfaces.
2020-12-21 21:15:07.646  INFO 22192 --- [  restartedMain] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2020-12-21 21:15:07.695  INFO 22192 --- [  restartedMain] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.4.25.Final
2020-12-21 21:15:07.798  INFO 22192 --- [  restartedMain] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2020-12-21 21:15:07.902  INFO 22192 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2020-12-21 21:15:08.200  INFO 22192 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2020-12-21 21:15:08.220  INFO 22192 --- [  restartedMain] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.SQLServer2012Dialect
2020-12-21 21:15:08.710  INFO 22192 --- [  restartedMain] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2020-12-21 21:15:08.721  INFO 22192 --- [  restartedMain] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2020-12-21 21:15:08.761  INFO 22192 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2020-12-21 21:15:08.873  INFO 22192 --- [  restartedMain] c.e.employees.EmployeesApplication       : Started EmployeesApplication in 2.792 seconds (JVM running for 4.218)
2020-12-21 21:15:08.885  INFO 22192 --- [extShutdownHook] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2020-12-21 21:15:08.892  INFO 22192 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2020-12-21 21:15:08.898  INFO 22192 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.

Process finished with exit code 0

I don't see any error in the console, but the table is not created. Please let me know if you have any idea what's wrong.

Olaru Alina
  • 458
  • 4
  • 8
  • In your entity class,you are providing "schema = "dbo". I am assuming that you have configured same schema name in application properties as well. Is that correct? – Sridhar Patnaik Dec 21 '20 at 19:26
  • No, but I've created with the exact same configurations a Grandle project and my tables were created... I just switched to maven with this. Is there something so different between maven and Grandle ? (beside syntax) – Olaru Alina Dec 21 '20 at 19:29
  • No. Syntax should not change between gradle and maven. Anyway, can you try deleting schema = "dbo' and see what happens – Sridhar Patnaik Dec 21 '20 at 19:31
  • I'm gettings some errors: 2020-12-21 21:34:34.573 ERROR 27060 --- [ restartedMain] j.LocalContainerEntityManagerFactoryBean : Failed to initialize JPA EntityManagerFactory: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] 2020-12-21 21:34:34.575 WARN 27060 --- [ restartedMain] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined – Olaru Alina Dec 21 '20 at 19:36
  • Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] – Olaru Alina Dec 21 '20 at 19:37
  • Have you tried `spring.jpa.hibernate.ddl-auto=create`? – SSK Dec 22 '20 at 04:35

1 Answers1

0

Did you add the required dependencies?

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>com.microsoft.sqlserver</groupId>
        <artifactId>mssql-jdbc</artifactId>
    </dependency>

You also need to enable entity scan, in case you didn't:

@Configuration
@EntityScan(basePackages = {"my.package"})
@EnableJpaRepositories(basePackages = {"my.package"})

Basically your first three properties would be enough, but the others shouldn't harm.

spring.datasource.url=jdbc:sqlserver://localhost;databaseName=my-app
spring.datasource.username=sa
spring.datasource.password=<<YOUR_PASSWORD>>

If you like create a Spring Boot app for MS SQL at https://bootify.io to see the full configuration.

Thomas
  • 451
  • 2
  • 5