Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tweak NamingStrategy for Spring Data JDBC

how do i tweak the Spring Data JDBC NamingStrategy to behave like Hibernate´s PhysicalNamingStrategy?
I have the following entity:

/**
 * Campus domain model class.
 * Handles information about campus.
 *
 * @author [email protected]
 */
@Data
@AllArgsConstructor(access = AccessLevel.PRIVATE, onConstructor = @__(@PersistenceConstructor))
public class Campus {

    private final @Id
    @Wither
    long campusId;

    @NotNull
    @Size(min = 3)
    private String campusName;

    /**
     * Creates a new campus.
     *
     * @param campusName
     */
    public Campus(@NonNull String campusName) {
        this.campusId = 0;
        Assert.hasLength(campusName, "A valid value has to be provided for Campus!");
        this.campusName = campusName;
    }

}

When i want JDBC to persist a test record, it generates this sql:
[INSERT INTO campus (campus_name) VALUES (?)]

My existing Database has a column named campusname

I have read on the docu here https://docs.spring.io/spring-data/jdbc/docs/1.0.2.RELEASE/reference/html/#jdbc.entity-persistence.naming-strategy that i can tweak the naming strategy, but i don´t know how :)

Thank you for your help! Kind regards! Thomas

Solution:

/**
     * Naming strategy for naming entity columns
     * @see <a href="https://stackoverflow.com/questions/53334685/how-to-tweak-namingstrategy-for-spring-data-jdbc/53335830#53335830">How to implement {@link NamingStrategy}</a>
     *
     * @return PhysicalNamingStrategy
     */
    @Bean
    public NamingStrategy namingStrategy() {
        return new NamingStrategy() {
            @Override
            public String getColumnName(RelationalPersistentProperty property) {
                Assert.notNull(property, "Property must not be null.");
                return ParsingUtils.reconcatenateCamelCase(property.getName(), "");
            }
        };
    }
like image 256
Thomas Lang Avatar asked Nov 16 '18 09:11

Thomas Lang


People also ask

What is the difference between Spring JDBC and Spring Data JDBC?

Spring Data JDBC has less abstractions than Spring Data JPA, but uses Spring Data concepts to make it easier to do CRUD operations than Spring JDBC. It sits closer to the database because it does not contain the most part of the Spring Data magic when querying the database.

Is Spring Data JDBC good?

It makes it easier to build Spring powered applications that use data access technologies. Spring Data JDBC aims at being conceptually easy. In order to achieve this it does NOT offer caching, lazy loading, write behind or many other features of JPA. This makes Spring Data JDBC a simple, limited, opinionated ORM.

Does spring data use JDBC?

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.

How does Spring data work in 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.


1 Answers

Create a bean of type NamingStrategy in your application context and implement its methods to your liking.

The method that you need to override is getColumnName(RelationalPersistentProperty)

like image 169
Jens Schauder Avatar answered Dec 10 '22 18:12

Jens Schauder