I know there are few threads about this, but I cannot understand why it is not working with my code. I have the entire database with capital letters, columns, table names, sequences.But when I try to make a query, via sql
or criteria
, It transforms all values in lowercase. I found a workaround but I don't want to write queries like:
select a."COLUMN_1", a."COLUMN_2" from schema."A" a
And mappings like:
@Entity(name = "`A`")
public class A implements Serializable {
@Column(name = "`COLUMN_1`")
private Integer column1;
@Column(name = "`COLUMN_2`")
private Integer column2;
}
I tried to follow some threads in stackoverflow implementing my own naming strategy, but it neither didn't work .
public class ModifiedImprovedNamingStrategy extends PhysicalNamingStrategyStandardImpl{
@Override
public final Identifier toPhysicalColumnName(final Identifier name, final JdbcEnvironment context) {
return new Identifier(addUnderscores(name.getText()), name.isQuoted());
}
/**
* Adds the underscores.
*
* @param name
* the name
* @return the string
*/
protected static String addUnderscores(final String name) {
final StringBuilder buf = new StringBuilder(name.replace('.', '_'));
for (int i = 1; i < (buf.length() - 1); i++) {
if (Character.isLowerCase(buf.charAt(i - 1))
&& Character.isUpperCase(buf.charAt(i))
&& Character.isLowerCase(buf.charAt(i + 1))) {
buf.insert(i++, '_');
}
}
return "`" + buf.toString().toUpperCase(Locale.ROOT) + "`";
}
}
And then calling it in my applicationContext like that:
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"
p:dataSource-ref="dataSource">
<property name="packagesToScan" value="com.services.vo"/>
<property name="mappingLocations">
<list>
<value>classpath*:hibernate/queries/**.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.transaction.coordinator_class">org.hibernate.transaction.JDBCTransactionFactory</prop>
<prop key="hibernate.physical_naming_strategy">com.services.util.hibernate.ModifiedImprovedNamingStrategy</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
</bean>
My intention is to avoid writing those everywhere. I tried to set a breakpoint inside the overrided ModifiedImprovedNamingStrategy methods. When I try a unit test, but it is not stopping there.Is there any way to do what I want? or will I be forced to keep those ?
Thanks in advance