MinionAttack
MinionAttack

Reputation: 540

Hibernate renames some columns at start with Spring Boot 1.4

I'm having some problems with my PostgreSQL database columns on some tables.

If I declare a column of String with a name like a_column_name Hibernate accepts the column of the table with the name a_column_name.

But my problem comes when I declare the column type to Integer or BigDecimal. At start, Hibernate creates a new column of type int4 or numeric(19, 2) with a name like acolumnname with NULL's.

Hibernate: alter table t_myTable add column acolumnname int4

or

Hibernate: alter table t_myTable add column acolumnname numeric(19, 2)

I've tried to set the name-strategy on the config file of Spring boot:

jpa:
    show-sql: true
    hibernate:
      ddl-auto: update
    properties:
      hibernate.cache.use_second_level_cache: false
      hibernate.cache.use_query_cache: false
      hibernate.generate_statistics: true
      naming.physical-strategy: PhysicalNamingStrategyStandardImpl

But with no result...

So my question is why Hibernate accepts the column name when it's of String type but doesn't like it when it's Integer or BigDecimal.

Regards.

---- Update 1 ----

After search the Entity to post here the code as Veselin Davidov asked, I've noticed that some columns are accepted while others not.

The entities are created from a company framework that parses a JSON with the DB structure (tables, fields, type of fields...) and generates the JAVA entity class. So after see the answer from SAM I've changed the code of the Entity template to add a @Column(name = "your_column_name"). Now, when Hibernate starts, it doesn't add the columns with the wrong names and uses the DB columns.

Upvotes: 2

Views: 2654

Answers (1)

sam
sam

Reputation: 2004

if your Hibernate entity class is like the following:

@Entity
public class Registration{

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

    @Column
    @Basic
    private String name;

    // getters and setters

}

then your registration table will have fields named with "id" and "name" as you have used auto-ddl=update. To avoid that you can specify the column names with @Column(name = "your_column_name") like below:

@Table(name = "registration")
@Entity
public class Registration{

   @Id
   @Column(name = "r_id")
   @GeneratedValue(strategy = GenerationType.AUTO)
   private int id;

   @Basic
   @Column(name = "r_name")
   private String name;

   // getters and setters

}

If you are using Intellij IDEA then the IDE can generate the required Entity class.

You can go to your persistence tab -> right-click on your project name -> Generate Persistence Mapping -> By Database Schema. Then select the tables whose Entity you want to generate. Voila everything comes with ease.

Now coming to your problem, in your Entity class if you set some filed with Integer type then it will update your table to int4 and with allow null. The BigDecimal is also same numeric(10,2) with allow null. To avoid allow null in your database use primitive type int and double.

Upvotes: 2

Related Questions