gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:19:51 24 4
gpt4 key购买 nike

在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com