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