Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to map entity to table in Spring Data JDBC?

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?

like image 752
keddok Avatar asked Oct 30 '18 11:10

keddok


People also ask

What is entity JDBC?

In Spring Data JDBC, the entity is required to have an @Id. Spring Data JDBC uses the @Id annotation to identify entities. Similar to Spring Data JPA, Spring Data JDBC uses, by default, a naming strategy that maps Java entities to relational database tables, and attributes to column names.

Is Spring Data JDBC an ORM?

This makes Spring Data JDBC a simple, limited, opinionated ORM.

What is Spring Data JDBC?

Spring Data JDBC is an object-relational mapping framework for relational databases that aims to avoid most of the complexity of other ORM frameworks. It does that by avoiding features like lazy loading, managed lifecycles of entity objects and caching.

What are the entities in spring data JDBC?

As it is not a Hibernate Backed JPA Implementation, there are no entities in Spring Data JDBC. However, we can designate any Plain Old Java Object (POJO) as an entity and use it with repositories. In the next section we will see how to do that.

Can spring data JDBC map a POJO to a database table?

As stated above, Spring Data JDBC can map any POJO to a database table, if, Name of the POJO is same as that of table. Otherwise, it uses @Tableannotation to refer to the actual table name. The POJO has a primary key and that is annotated as @Id.

How do I create a new table in spring data JDBC?

Create Database Table Unlike the Hibernate and JPA combination, Spring Data JDBC doesn’t generate database tables automatically. Hence, we need to create them manually or use data.sql file or liquibase to generate the database schemas. Next is the command to create a Student table for our example.

What is the relation between branch and student in spring data?

A basic quick Spring Data JDBC example on how to map One-to – Many database tables relation in entities. 1. Database Tables Let’s have a look into following tables, We can say that the relation between BRANCH and STUDENT is One-to-Many.


1 Answers

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";
            }
        };
    }
}
like image 157
Kartoch Avatar answered Oct 04 '22 17:10

Kartoch