In Spring Data JPA we can map an entity to a specific table by using @Table
annotation where we can specify schema and name.
But Spring Data JDBC uses a NamingStrategy
to map an entity to a table name by converting the entities class name. For example, if we have the entity class named MetricValue
then the table should be named metricvalue
in default schema. But I need to map MetricValue
to the metric_value
table in app
schema.
Is there any way to override this mapping by annotation or any other?
The naming behavior is defined by the default implementation of the interface NamingStrategy
From reference documentation, section 4.4.3 of version 1.0.2:
When you use the standard implementations of CrudRepository that Spring Data JDBC provides, they expect a certain table structure. You can tweak that by providing a NamingStrategy in your application context.
The default implementation has the following behavior (from javadoc version 1.0.2):
Defaults to no schema, table name based on Class and column name based on RelationalPersistentProperty with name parts of both separated by '_'.
So create a bean which implements NamingStrategy
in register it in your application context.
This is an example from @keddok comment:
@Configuration
@EnableJdbcRepositories
public class MetricStoreRepositoryConfig extends JdbcConfiguration {
@Autowired
private DataSource dataSource;
@Bean
NamedParameterJdbcOperations operations() {
return new NamedParameterJdbcTemplate(dataSource);
}
@Bean
PlatformTransactionManager transactionManager() {
return new DataSourceTransactionManager(dataSource);
}
@Bean
NamingStrategy namingStrategy() {
return new NamingStrategy() {
@Override
public String getSchema() {
return "metric";
}
};
}
}