I'm trying to update a database using hibernate..I used the following code for that and that overwrite the database when I tried to enter some values after the first set of values entered.. How can I insert a set of values without erasing previous values??
This is the code that I used.
String firstName = txtfnm.getText();
String lastName = txtlnm.getText();
String empId = txtuid.getText();
//Configuration config = new Configuration();
Configuration config = new Configuration();
config.addAnnotatedClass(Employee.class);
config.configure("hibernate.cfg.xml");
//to create a new table
new SchemaExport(config).create(true, true);
ServiceRegistry servicereg = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
//create a session factory
SessionFactory factory = config.buildSessionFactory(servicereg);
Session session = factory.getCurrentSession();
//SessionFactory session = factory.getCurrentSession();
session.beginTransaction();
Employee emp1 = new Employee();
emp1.setFname(firstName);
emp1.setLname(lastName);
emp1.setUsrid(Integer.valueOf(empId));
session.update(emp1);
session.getTransaction().commit();
This is the entity class
@Entity
@Table(name="empdtls")
public class Employee {
@Column(name="emp_id")
private int usrid;
@Column(name="emp_fnm")
private String fname;
@Column(name="emp_lnm")
private String lname;
@Id
public int getUsrid() {
return usrid;
}
public void setUsrid(int usrid) {
this.usrid = usrid;
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;
}
}