1

I am implementing Spring-JPA application . In the model class I am using @Table annotation to map the table with the model class and against the column name using @column annotation to map a variable to a particular column name in that table. But whenever I am executing the application some extra column has been added to the table. I don't want want that default column name.

Model class:

@Entity
@Table(name="xyz",catalog ="test")
public class ModelTest {
      @Id
      private String cas_Id;
      @Column(name="cas_CaseNumber")
      private String cas_case_number;
      @Column(name="cas_ContactId")
      private String cas_contact_id;
      @Column(name="cas_AccountId")
      private String cas_account_id ;
      @Column(name="cas_OwnerId")
      private String cas_owner_id ;
      private String cas_Type;
      private String cas_Status;
      private String cas_Subject;
      @Column(name="cas_ClosedDate")
      private String cas_closed_date ;
      private String acc_Id;
      private String acc_Name;
      private String acc_Type;
      @Column(name="acc_ParentId")
      private String acc_parent_id ;
      @Column(name="acc_BillingStreet")
      private String acc_billing_street;
      @Column(name="acc_BillingCity")
      private String acc_billing_city;
      @Column(name="acc_BillingState")
      private String acc_billing_state ;
      @Column(name="acc_BillingPostalCode")
      private String acc_billing_postal_code;
      @Column(name="acc_BillingCountry")
      private String acc_billing_country;
      @Column(name="acc_ShippingStreet")  
        private String acc_shipping_street;
//with setter and getter

in the console:

Hibernate: alter table test.xyz add column cas_case_number varchar(255)
Hibernate: alter table test.xyz add column cas_contact_id varchar(255)
Hibernate: alter table test.xyz add column acc_billing_city varchar(255)
Hibernate: alter table test.xyz add column acc_billing_country varchar(255)
Hibernate: alter table test.xyz add column acc_billing_postal_code varchar(255)
Hibernate: alter table test.xyz add column acc_billing_state varchar(255)
Hibernate: alter table test.xyz add column acc_billing_street varchar(255)

I can't understand why this alter table query will executed as I have already those column defined in my entity class.

stumbler
  • 647
  • 3
  • 11
  • 26

1 Answers1

2

Same issue i'm facing in a while.

To solve this issue i've made some changes in my application.properties file,

application.properties

spring.jpa.hibernate.ddl-auto=validate

hope this works for you.

Ravi Mengar
  • 1,181
  • 1
  • 11
  • 18
  • I added in the application.properties now that alter command not coming but I am getting exception like Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: missing column – stumbler Sep 26 '19 at 03:38
  • I have changed the column name in the table and after that error was gone. But I think it does not looks like a permanent solution.As per my knowledge If we are using @column("Column name")then my entity should map to that particular column of that table right.Can anyone clarify the doubt why @column not able to map existing column in the table and instead created new column with underscore. – stumbler Sep 27 '19 at 07:23
  • If the tables are not existed with in database then by default hibernate will take the attribute name and create the tables with those attribute names.If tables are existed with in the database then what ever the column name you mentioned in the database table must be same as the column name which should be mentioned at @Column(name="") then only mapping will occurs. – Ravi Mengar Sep 27 '19 at 07:47
  • that is correct. But unfortunately that is not happening JPA is forced to create its default column name although there is a mapping @column. – stumbler Sep 27 '19 at 07:52
  • here is the answer although I am not trying:https://stackoverflow.com/questions/25283198/spring-boot-jpa-column-name-annotation-ignored – stumbler Sep 27 '19 at 07:55
  • try to use a different database or refresh your current database it may work. – Ravi Mengar Sep 27 '19 at 07:55