【问题标题】:How to fetch schema name for a Spring data jpa entity from config如何从配置中获取 Spring 数据 jpa 实体的模式名称
【发布时间】:2018-11-10 03:00:34
【问题描述】:

在我的 spring boot 应用程序中,我使用的是 spring data jpa。在实体中,我需要从配置中选择架构名称,架构会发生变化并且需要可配置。我尝试了以下方法,但它不起作用

@Entity
@Table(schema="${schema.name}", name="MyTable")

schema.name 在 application.properties 文件中定义

我收到“无法提取结果集”错误

有没有办法做到这一点?

===============编辑================

@Entity
@Table(name="MyTable")
public class MyData {

    @Id
    @Column(name="MyID")
    @JsonProperty("MyID")
    private String MyID;

    @Column(name="number")
    @JsonProperty("number")
    private String number;

    @Column(name="value")
    @JsonProperty("value")
    private String value;

    ......getter and setters go here.....
}

【问题讨论】:

    标签: spring-boot spring-data spring-data-jpa


    【解决方案1】:

    请在 application.properties 文件中设置架构和其他数据库相关配置。
    在实体中保留模式名称不是一个好习惯。

    如下代码:

    spring.datasource.url=jdbc:mysql://192.168.97.39:3309/schema-name?autoReconnect=true&autoReconnectForPool=true&characterEncoding=UTF-8
    spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
    
    spring.datasource.username=userName
    spring.datasource.password=Password
    

    这里是spring.datasource.url属性中配置的Schema名称

    【讨论】:

    • 我从 @Table 中删除了模式名称并将其移至 spring.datasource.url。现在出现以下错误。线程“主”org.springframework.beans.factory.BeanCreationException 中的异常:在类路径资源 [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class] 中定义名称为“entityManagerFactory”的 bean 创建错误:调用 init方法失败;嵌套异常是 org.hibernate.DuplicateMappingException:表 [MyTable] 包含引用多个物理列名称的逻辑列名称 [txnid]:[txnid]、[txn_id]。
    • 从所有实体中删除所有架构
    • 我只有一个实体,并删除了从中命名的架构。上面解释的错误就是由此造成的。
    • 根据错误信息“DuplicateMappingException”,由于映射错误而出现异常。检查 '@ManytoOne', @'OneToMany' 0r '@ManyToMany'annotations 或共享实体文件
    • 没有任何关联。只用一张桌子
    【解决方案2】:

    使用

    spring.jpa.properties.hibernate.default_schema=schema
    

    你可以看到this similar question here

    【讨论】:

      猜你喜欢
      • 2015-12-30
      • 2019-01-25
      • 1970-01-01
      • 1970-01-01
      • 2019-12-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-01
      • 1970-01-01
      相关资源
      最近更新 更多