我如何调整 Spring Data JDBCNamingStrategy
使其表现得像 Hibernate的PhysicalNamingStrategy
?
我有以下实体:
/**
* Campus domain model class.
* Handles information about campus.
*
* @author thomas.lang@th-deg.de
*/
@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;
}
}
当我希望 JDBC 保留测试记录时,它会生成以下 sql:
[INSERT INTO campus (campus_name) VALUES (?)]
我现有的数据库有一个名为campusname
我在这里阅读了文档https://docs.spring.io/spring-data/jdbc/docs/1.0.2.RELEASE/reference/html/#jdbc.entity-persistence.naming-strategy我可以调整命名策略,但我不知道如何:)
感谢您的帮助!亲切的问候!托马斯
解决方案:
/**
* 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(), "");
}
};
}