1

Consider we are going to use following EMPLOYEE table to store our objects:

create table EMPLOYEE (
   id INT NOT NULL auto_increment,
   first_name VARCHAR(20) default NULL,
   last_name  VARCHAR(20) default NULL,
   salary     INT  default NULL,
   PRIMARY KEY (id)
);

Following is the mapping of Employee class with annotations to map objects with the defined EMPLOYEE table:

    import javax.persistence.*;

@Entity
@Table(name = "EMPLOYEE")
public class Employee {
   @Id @GeneratedValue
   @Column(name = "id")
   private int id;

   @Column(name = "first_name")
   private String firstName;

   @Column(name = "last_name")
   private String lastName;

   @Column(name = "salary")
   private int salary;  

   public Employee() {}
   public int getId() {
      return id;
   }
   public void setId( int id ) {
      this.id = id;
   }
   public String getFirstName() {
      return firstName;
   }
   public void setFirstName( String first_name ) {
      this.firstName = first_name;
   }
   public String getLastName() {
      return lastName;
   }
   public void setLastName( String last_name ) {
      this.lastName = last_name;
   }
   public int getSalary() {
      return salary;
   }
   public void setSalary( int salary ) {
      this.salary = salary;
   }
}

My Question is "If I want to add a new column to my database in future, do I need to modify this code?"

Marco Ferrari
  • 4,914
  • 5
  • 33
  • 53
Aryan
  • 61
  • 8
  • 1
    Adding a new column to the database won't break anything, so "need to" is a bit strong. However, if you don't reflect the new column in the Entity class, then you are not able to use that column with hibernate (in INSERT, SELECT, etc). Do you expect the new column to be used in the hibernate/spring application? – Ian Mc Jan 15 '16 at 06:04
  • @Marco: I am looking for the process that does the job and users do not update Entity class manually. In the following comment one user mentioned update – Aryan Jan 17 '16 at 12:12
  • @Marco: If two organizations merged, the CEO wants keep their Emp ID's unchanged and Unique ID needs to be created to each user to avoid complications in future if they buy new company. So, new column needs to introduced in Emp table. In that case, Developer should not go and modify the Entity code. And by the way same question was asked in recent interview to one of my friend. – Aryan Jan 17 '16 at 12:22

2 Answers2

1

If you want to change DB, it's ok, but remember to update your application Entity class. You can do it manually or leave it for Hibernate to do it automatically, by adding:

<property name="hibernate.hbm2ddl.auto">update</property>

to your Hibernate configuration files. If you will go with automatic version, just change the Entity class, and Hibernate will update DB automatically.

m.aibin
  • 3,528
  • 4
  • 28
  • 47
  • Pay attention when you use the hibernate.hbm2ddl.auto configuration directive. It's not intended for production use. See: http://stackoverflow.com/questions/438146/hibernate-hbm2ddl-auto-possible-values-and-what-they-do – Marco Ferrari Jan 15 '16 at 08:40
0

When you add new column to database table, application will still work properly. But when you need to use this new column in application, you should update entity class by adding new field and mapping the value.

@Column(name = "new_column_name")
private int newFieldName;  
lukaszwrzaszcz
  • 170
  • 1
  • 7
  • I am looking for the process that does the job and developers do not update Entity class manually. But your suggestion would ask developers modify the code. – Aryan Jan 17 '16 at 12:14
  • If two organizations merged, the CEO wants keep their Emp ID's unchanged and Unique ID needs to be created to each user to avoid complications in future if they buy new company. So, new column needs to introduced in Emp table. In that case, Developer should not go and modify the Entity code. And by the way same question was asked in recent interview to one of my friend. – Aryan Jan 17 '16 at 12:22
  • You can use reflection api to add and annotate new fields. Then, you can provide list of created classes as a parameter during SessionFactory initialization: `` – lukaszwrzaszcz Mar 18 '16 at 08:58