I was trying to migrate hibernate from 4.3 to 5.2. The problem i am facing is HQL generated with version 5.2.17 has '_id' appended with all the columns which has associations.
public class Person {
private Long id;
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Customer customer;
private String othercolumns;
}
public class Customer {
private Long id;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "customer", fetch = FetchType.LAZY)
private Set<Person> persons = new HashSet<Person>();
private String othercolumns;
}
Now if i query for all person for a specific customer. My query is like :
SELECT o FROM Person AS o WHERE o.customer = 2
HQL generated for above query :
select person0_.id as id1_60_, person0_.account_creation_date as account_2_60_, person0_.contact_id as contact_3_60_, person0_.customer_id as custome36_60_, person0_.deleted_at as deleted_4_60_ from person person0_ where person0_.customer_id=?
In the above generated HQL why customer column is appended with _id ? Due to which my query is returning nothing. Meanwhile i tried firing this query after changing customer_id to customer and it worked.
I have implemented physical naming strategy as below.
public class ImprovedNamingStrategy implements PhysicalNamingStrategy {
@Override
public Identifier toPhysicalCatalogName(Identifier identifier, JdbcEnvironment jdbcEnv) {
return convert(identifier);
}
@Override
public Identifier toPhysicalColumnName(Identifier identifier, JdbcEnvironment jdbcEnv) {
return convert(identifier);
}
@Override
public Identifier toPhysicalSchemaName(Identifier identifier, JdbcEnvironment jdbcEnv) {
return convert(identifier);
}
@Override
public Identifier toPhysicalSequenceName(Identifier identifier, JdbcEnvironment jdbcEnv) {n
return convert(identifier);
}
@Override
public Identifier toPhysicalTableName(Identifier identifier, JdbcEnvironment jdbcEnv) {
return convert(identifier);
}
private Identifier convert(Identifier identifier) {
if (identifier == null || StringUtils.isBlank(identifier.getText())) {
return identifier;
}
String regex = "([a-z])([A-Z])";
String replacement = "$1_$2";
String newName = identifier.getText().replaceAll(regex, replacement).toLowerCase();
return Identifier.toIdentifier(newName);
}
}
Any help would be appreciated.