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(), "");
}
};
}
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.
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.
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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With