【问题标题】:How can I globally specify my schema, rather than add it to every @Table annotation?如何全局指定我的架构,而不是将其添加到每个 @Table 注释中?
【发布时间】:2016-10-25 16:03:56
【问题描述】:

我的所有表都在 SQLServer 数据库中的相同架构中,因此我宁愿不必在每个 @Table 注释中指定架构。我已将架构名称放在我的连接字符串中:

spring.datasource.url=jdbc:sqlserver://localhost;instanceName=SQLEXPRESS;databaseSchema=TST;databaseName=TestDB;integratedSecurity=true;

当我的注释是@Table(name="MY_TABLE"),并且Hibernate 尝试插入时,我收到Invalid object name 'MY_TABLE'. 错误消息。

如果注释是@Table(name="MY_TABLE", schema="TST"),则插入按预期工作。

SQLServer 方言不支持连接字符串中的架构吗?

这里是所有 Spring/Hibernate 属性:

spring.datasource.url=jdbc:sqlserver://localhost;instanceName=SQLEXPRESS;databaseSchema=EXP;databaseName=YRC_PILOT2;integratedSecurity=true;
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.jpa.database-platform = org.hibernate.dialect.SQLServerDialect
spring.jpa.show-sql=true

如果我必须为每个表指定架构,那就这样吧。但如果我想切换模式名称,这似乎有点笨拙。

【问题讨论】:

标签: sql-server spring hibernate jpa


【解决方案1】:

使用spring.jpa.properties.hibernate.default_schema=yourschemaname

你可以使用spring.jpa.properties.*来设置JPA原生属性

来自 Spring Boot 文档:

spring.jpa.properties.* 中的所有属性都作为普通 JPA 属性传递(去掉前缀)

http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-configure-jpa-properties

【讨论】:

    【解决方案2】:

    感谢datGnomeLifeNeil Stockton 我开始在连接字符串之外寻找。这使我找到了How to set up default schema name in JPA configuration?,它实际上回答了我的问题,而Possible to set default schema from connection string? 没有。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-04
      • 1970-01-01
      • 2023-04-08
      • 2015-01-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多