java - 如何在 Spring Data JDBC 中将实体映射到表?

标签 java spring spring-data mapping spring-data-jdbc

在 Spring Data JPA 中,我们可以使用 @Table 注释将实体映射到特定表,我们可以在其中指定模式和名称。

但 Spring Data JDBC 使用 NamingStrategy 通过转换实体类名将实体映射到表名。例如,如果我们有一个名为 MetricValue 的实体类,那么该表在默认架构中应命名为 metricvalue。但我需要将 MetricValue 映射到 app 架构中的 metric_value 表。

有什么方法可以通过注释或任何其他方式覆盖此映射?

最佳答案

Spring Data JDBC has it's own @Table annotation还有一个 @Column 一个。

您只需将注释添加到您的实体并将名称指定为注释的值。

举几个例子:

@Table("entity") 
class MyEntity {

    private @Column("last_name") String name;

    @Column(value = "entity_id", keyColumn = "entity_index") 
    private List<SomeOtherEntity> someList;
}

这将读写MyEntity进/出表 entity而不是默认的 my_entity . 属性 name将存储在列 last_name 中. 以及来自 some_other_entity 的反向引用列至 entity将被命名为entity_id对于通常为 entity 的外键列(引用表的表名)。 列表索引将存储在entity_index中。而不是默认的 entity_key .

我创建了 an issue for improving the documentation .

关于java - 如何在 Spring Data JDBC 中将实体映射到表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53063266/

相关文章:

java - 我找不到 Could not resolve view with name 'index' in servlet with name 'DispatcherServlet' 的答案

java - 如何避免if-else树代码?

java - 如何判断 JPA 保存方法采取了什么操作

java - 具有规范、分页和标准获取连接的 Spring Data JPA 存储库

spring - 尝试使用 @CreatedBy 更新任何实体时出现 java.lang.StackOverflowError

java - 通过 void 方法将变量从父类(super class)传递给子类

java - 如何在Java中制作星形?

java - 当我使用 @FindBy 注释时没有出现此类元素异常,当我使用 driver.findelement(By.id()) 时工作​​正常

java - Spring Security permitAll 不允许匿名访问

spring - 在服务方法中获取主要用户对象