NullPointerException while implementing Hibernate custom Naming Strategies
Environment
- Red Hat build of Quarkus
- 2.7.6
- Hibernate
Issue
- Getting NullPointerException when implementing Hibernate custom Naming strategies in Quarkus project
NullPointerException: Cannot invoke "org.hibernate.boot.model.naming.Identifier.getText()" because "identifier" is null.
Resolution
Disclaimer: Links contained herein to external website(s) are provided for convenience only. Red Hat has not reviewed the links and is not responsible for the content or its availability. The inclusion of any link to an external website does not imply endorsement by Red Hat of the website or their entities, products or services. You agree that Red Hat is not responsible or liable for any loss or expenses that may result due to your use of (or reliance on) the external site or content.
Replace the code of CustomNamingStratiges class which implements PhysicalNamingStrategy with the below code:
import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.model.naming.PhysicalNamingStrategy;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
public class CustomPhysicalNamingStrategy implements PhysicalNamingStrategy {
@Override
public Identifier toPhysicalCatalogName(final Identifier identifier, final JdbcEnvironment jdbcEnv) {
return formatIdentifier(identifier);
}
@Override
public Identifier toPhysicalColumnName(final Identifier identifier, final JdbcEnvironment jdbcEnv) {
return formatIdentifier(identifier);
}
@Override
public Identifier toPhysicalSchemaName(final Identifier identifier, final JdbcEnvironment jdbcEnv) {
return formatIdentifier(identifier);
}
@Override
public Identifier toPhysicalSequenceName(final Identifier identifier, final JdbcEnvironment jdbcEnv) {
return formatIdentifier(identifier);
}
@Override
public Identifier toPhysicalTableName(final Identifier identifier, final JdbcEnvironment jdbcEnv) {
return formatIdentifier(identifier);
}
// use your specific formatter or converter method for your use-case
private Identifier formatIdentifier(final Identifier identifier) {
if (identifier == null) {
return identifier;
}
String name = identifier.getText();
String newName = name.substring(0, 1).toUpperCase() + name.substring(1);
return Identifier.toIdentifier(newName, identifier.isQuoted());
}
}
Also add this CustomNamingStratiges class in application.properties file like below:
quarkus.hibernate-orm.physical-naming-strategy = xxxx.xxxxx.xxxxxx.CustomPhysicalNamingStrategy
Please note that the above code is just an example and the method formatIdentifier() will format the column name like below:
username --> Username
You need to use your own specific converter method for your use case.
For more information please check this link: Hibernate 5 Naming Strategy Configuration
Diagnostic Steps
Check the log in console when you build and run the Quarkus project
This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.
Comments