Spring boot JPA insert in TABLE with uppercase name with Hibernate

后端 未结 6 987
别那么骄傲
别那么骄傲 2020-12-01 07:08

i have a table entity mapped as :

@Entity
public class ItemsToRegister implements Serializable{

@Id
@Column(name = \"ID_ITEM_TO_REGISTER\")
@GeneratedValue         


        
6条回答
  •  感情败类
    2020-12-01 07:44

    As @jasonleakey suggested we can consider using naming-strategy as below.

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

    this tells Hibernate to generate SQL's as exactly as specified in the @Table (name=" ") or @Column(name=" "). All well.

    But keep in mind - while using PhysicalNamingStrategy without @Table, @Column annotations in the entity class, hibernate generates SQL using class name and variable names. Consider the below java class

    Class Employee {
       private String firstName;
       private String lastName; 
    }
    

    then the generated sql would be,

    select employee0_.firstName,employee0_lastName from Employee employee0_;
    

    Unfortunately this is not a great choice as typically we would have defined the columns in DB as FIRST_NAME and LAST_NAME and table name as EMPLOYEE. Had you not used PhysicalNamingStrategy the SQL would have been

    select employee0_.first_name,employee0_last_name from employee employee0_;
    

    so it's really a choice between the below two options.

    • Use PhysicalStrategy and explicitly define all tables names/column names in java code with @Table and @Column annotations.
      or
    • Define lowercase table name in db and let hibernate automatically generate table names/column names for us.

提交回复
热议问题