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 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;
}
}
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 spring-data-jdbc
add a comment |
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 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;
}
}
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 spring-data-jdbc
add a comment |
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 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;
}
}
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 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 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;
}
}
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 spring-data-jdbc
spring-data spring-data-jdbc
edited Nov 16 '18 at 10:34
Thomas Lang
asked Nov 16 '18 at 9:14
Thomas LangThomas Lang
184110
184110
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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)
Thank you Jens. Your hint was helpful. I ended up with theBean
above. Just for the record - please see the solution above on the question ...
– Thomas Lang
Nov 16 '18 at 10:33
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53334685%2fhow-to-tweak-namingstrategy-for-spring-data-jdbc%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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)
Thank you Jens. Your hint was helpful. I ended up with theBean
above. Just for the record - please see the solution above on the question ...
– Thomas Lang
Nov 16 '18 at 10:33
add a comment |
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)
Thank you Jens. Your hint was helpful. I ended up with theBean
above. Just for the record - please see the solution above on the question ...
– Thomas Lang
Nov 16 '18 at 10:33
add a comment |
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)
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)
answered Nov 16 '18 at 10:22
Jens SchauderJens Schauder
47.4k18115243
47.4k18115243
Thank you Jens. Your hint was helpful. I ended up with theBean
above. Just for the record - please see the solution above on the question ...
– Thomas Lang
Nov 16 '18 at 10:33
add a comment |
Thank you Jens. Your hint was helpful. I ended up with theBean
above. Just for the record - please see the solution above on the question ...
– Thomas Lang
Nov 16 '18 at 10:33
Thank you Jens. Your hint was helpful. I ended up with the
Bean
above. Just for the record - please see the solution above on the question ...– Thomas Lang
Nov 16 '18 at 10:33
Thank you Jens. Your hint was helpful. I ended up with the
Bean
above. Just for the record - please see the solution above on the question ...– Thomas Lang
Nov 16 '18 at 10:33
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53334685%2fhow-to-tweak-namingstrategy-for-spring-data-jdbc%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown