我有一台运行 MsSQL 数据库的虚拟机。我之前使用的是 Oracle 数据库。我的应用程序在启动时注入了一些示例数据,然后出现此错误:
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Invalid object name 'springbootdb.users'.
数据.sql
INSERT INTO "springbootdb"."users" (USER_ID, EMAIL, FIRST_NAME, INITIAL_NAME, LAST_NAME, PASSWORD, ROLE, ENABLED, CURRENT_STATUS, POSITION_TITLE) VALUES ('1', '[email protected]', 'asdf', 'asdf', 'asdf', '12345', 0, 1, 'In Office', 'Advanced Developer');
INSERT INTO "springbootdb"."users" (USER_ID, EMAIL, FIRST_NAME, INITIAL_NAME, LAST_NAME, PASSWORD, ROLE, ENABLED, CURRENT_STATUS, POSITION_TITLE) VALUES ('2', '[email protected]', 'asdf', 'asdf', 'asdf', '12345', 0, 1, 'In Office', 'Associate Developer');
INSERT INTO "springbootdb"."users" (USER_ID, EMAIL, FIRST_NAME, INITIAL_NAME, LAST_NAME, PASSWORD, ROLE, ENABLED, CURRENT_STATUS, POSITION_TITLE) VALUES ('3', '[email protected]', 'asdf', 'asdf', 'adsf', '12345', 1, 1, 'In Office', 'Line Manager');
应用程序.属性
# MsSQL Connection
spring.datasource.url=jdbc:sqlserver://11.11.111.1:1433;database=springbootdb;
spring.datasource.username=admin
spring.datasource.password=123!
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=create-drop
我测试了一下确实有连接,我也可以从sql账户中通过MSQLSMS访问数据库,如下图所示:
为什么我会收到这个错误?
答案1
插入的表名应该是springbootdb.dbo.users
答案2
使用 pojo 上的模式(表实体)
import javax.persistence.Table;
@Table(schema = "dbo", name = "users")