Petar Tahchiev
Petar Tahchiev

Reputation: 4386

How to handle the ORA-00972: identifier is too long Exception with Hibernate 5 a Naming Strategy

So I recently hit this error with Oracle and Hibernate:

Caused by: java.sql.SQLSyntaxErrorException: ORA-00972: identifier is too long
    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:450)
    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:399)
    at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:1059)
    at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:522)
    at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:257)

and after some google-ing I came to these projects:

https://github.com/schauder/hibernate-naming-strategy-for-oracle (almost 9 years old) https://code.google.com/archive/p/hibernate-naming-strategy-for-oracle/ (not maintained any more)

however none of these work with Hibernate5. In Hibernate5 there has been 2 new naming strategies - a physical and implicit one. Does any of you know if there is a hibernate5 naming strategy to overcome the oracle's 30 characters limit?

Upvotes: 3

Views: 4482

Answers (1)

Vlad Mihalcea
Vlad Mihalcea

Reputation: 153840

Actually, you can customize the way Hibernate 5 maps the physical naming strategy. For instance, you can write the following PhysicalNamingStartegy:

public class OracleNamingStrategy extends PhysicalNamingStrategyStandardImpl {

    @Override
    public Identifier toPhysicalColumnName(Identifier name, JdbcEnvironment context) {
        Identifier original = super.toPhysicalColumnName(name, context);
        if(original.getText().length() > 30) {
            return Identifier.toIdentifier(original.getText().substring(0, 30), original.isQuoted());
        }
        return original;
    }
}

And provide this naming strategy as a configuration property:

properties.put("hibernate.physical_naming_strategy", "com.vladmihalcea.book.hpjp.hibernate.naming.OracleNamingStrategy");

Considering the following entity mapping:

@Entity(name = "Person")
public static class Person  {

    @Id
    private Long personId;

    @ManyToOne
    private PersonAddress addressIsAVeryLongColumnThatExceedsThirtyCharacters;

    @ManyToOne
    private PersonLocation location;
}

Hibernate will just truncate the column size like this:

create table Person (personId number(19,0) not null, addressIsAVeryLongColumnThatEx number(19,0), location_id number(19,0), primary key (personId))

Code available on GitHub.

Upvotes: 4

Related Questions