Reputation: 61
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?"
Upvotes: 1
Views: 1466
Reputation: 170
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;
Upvotes: 0
Reputation: 3593
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.
Upvotes: 1