Hibernate 5.1.x naming strategy (backward compatible with Hibernate 4.x)

I am using Spring Boot 1.3.3.RELEASE. By default, Spring Boot uses Hibernate Version 4.x. I am trying to use the new Hibernate ie 5.1.0 FINAL (for now).

I use Gradle to override the version of Hibernate. I added the following line

ext['hibernate.version']="5.1.0.Final"

completed steps SpringBoot 1.3.0 with hibernate 5 support?

I use the following for naming Strategies

spring.jpa.properties.hibernate.naming.implicit-strategy: org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyHbmImpl

spring.jpa.properties.hibernate.naming.physical_strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

I have an Entity class

@Entity
public class AppUser {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotNull
    @Length(max = 100)
    private String username;

    @NotNull
    @Length(max = 100)
    private String firstName;

    @NotNull
    @Length(max = 100)
    private String lastName;

    @Length(max = 100)
    private String middleName;

    @NotNull
    @Length(max=100)
    private String email;

    @NotNull
    @Length(max = 100)
    private String password;

    @NotNull
    private boolean enabled;

}

In Hibernate 4.x, it makes a request

create table app_user (
        id bigint not null auto_increment,
        email varchar(100) not null,
        enabled bit not null,
        first_name varchar(100) not null,
        last_name varchar(100) not null,
        middle_name varchar(100),
        password varchar(100) not null,
        username varchar(100) not null,
        primary key (id)
    )

at 5.x he fulfilled the request

create table AppUser (
        id bigint not null auto_increment,
        email varchar(100) not null,
        enabled bit not null,
        firstName varchar(100) not null,
        lastName varchar(100) not null,
        middleName varchar(100),
        password varchar(100) not null,
        username varchar(100) not null,
        primary key (id)
    )

How to set a naming strategy so that Hibernate Uses 5.x emphasizes (like 4.x) the table name and column name

+4
2

-, org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

- Hibernate .

Hibernate 5 , . JPA ( , AppUser). , .

,

public class UnderscorePhysicalStartegy extends PhysicalNamingStrategyStandardImpl {

    @Override
    public Identifier toPhysicalTableName(Identifier name, JdbcEnvironment context) {
        return context.getIdentifierHelper()
                .toIdentifier(NamingStrategyUtils.classToName(name.getText()));
    }

}

NamingStrategyUtils.

,

@Entity
@Table(name = "AppUser")
public class AppUser {

}

app_user. , .

. Hibernate5NamingStrategy, (, ).

: HibernateNamingStrategy.

Hibernate5NamingStrategy

StrategyOptions.

, (, f_):

StrategyOptions options = StrategyOptions.builder().withoutPrefixes().build();
Hibernate5NamingStrategy strategy = new Hibernate5NamingStrategy(options);

: Hibernate 5

, ImprovedNamingStrategy Hibernate 5 Hibernate 4 ImprovedNamingStrategy.

+5

:

@Table @Column , , .. user_id, .. @Column (name = "user_id"), user_id; , user_id, ( , spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyHbmImpl). , , , - userId user_id, ( ).

, , , , : spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl. , @Table @Columns, .

, org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl . , , , . spring.jpa.hibernate.naming.physical-strategy=example.CustomStrategy

0

Source: https://habr.com/ru/post/1650331/


All Articles