Home > OS >  Spring Data JPA inserting underline character between variable words
Spring Data JPA inserting underline character between variable words

Time:02-08

Spring Data JPA inserts underscore character between words in variables, that does not happen if second term is not an word:

@Entity
@Table(name = "table")
public class Entity{
@Id
@GeneratedValue
private Long id;
private String variableA;
private String variableYes;
}

For 'variableA' it creates table with variableA SQL text insertion, for 'variableYes' it inserts in SQL variable_Yes, I didn't found documentation for this particularity. Eclipse IDE console:

create table questao (id bigint not null, variableA char(255), variable_Yes varchar(255), primary key (id));

CodePudding user response:

CamelCase is used by default. When encountering a capital letter (not last and next letter in lower case) When is considered the beginning of a new word. You can try variableAA and variableAa, which generate variableaa and variable_aa. It's good practice to explicitly specify a name using @Column(name = "variable_a").

CodePudding user response:

hibernate allow you define strategy. see: https://docs.jboss.org/hibernate/orm/5.3/userguide/html_single/Hibernate_User_Guide.html#PhysicalNamingStrategy

You can define custom implementations of this PhysicalNamingStrategy. if you don't, i guess it use SpringPhysicalNamingStrategy as default strategy.

In SpringPhysicalNamingStrategy, it generate column name by property name use the below method:

private boolean isUnderscoreRequired(char before, char current, char after) {
        return Character.isLowerCase(before) && Character.isUpperCase(current) && Character.isLowerCase(after);
    }

So, variableA is not match the rule, but variableYes match. And you database is casesensitive, so variableYes convert to variable_Yes, but variableA nothing changed.

  •  Tags:  
  • Related