1

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.

2 Answers2

0

It's due to the Naming Strategy that Hibernate adopts. So, a reference of an object in a table is a foreign key. Hibernate assumes that this foreign key name is: reference_name + _id.

Fabiano
  • 1,344
  • 9
  • 24
-1

Resolved !! This was default behavior of hibernate when no implicit naming strategy is supplied. I just added

<property name="hibernate.implicit_naming_strategy" value="org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyHbmImpl"/>

in my persistence.xml and it worked.