0

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;
    }





}

4 Answers 4

1

For updating an entity we have to load the entity from database and update using session...

In u r case u r creating new entity that's y its updating all the fields...

please change code after session.beginTransaction() as fallow....

the following code will get entity from database, having empid "4" ... Its primary key

Employee emp1 = (Employee) session.get(Employee.class, 4);

    emp1.setFname(firstName);

    emp1.setLname(lastName);

    emp1.setUsrid(Integer.valueOf(empId));

    session.update(emp1)
Sign up to request clarification or add additional context in comments.

1 Comment

he want to add new data and you are explaining how to update the entity.?
0

hibernate consider id to the primary key so in your case

@Id
@Column(name="emp_id")
private int usrid;

userId is the primary key if you are trying to save employee object whose userId(same value e.g 4) is already there in database it will update the value

one more thing if you want to save new fresh data why are you using update() method. use save() or Persist() or in case you don't know the object with same userId is already there in database then go for saveOrUpdate()

2 Comments

I did use save() and Persist().But it doesn't work.Still it overwrites the tables data.
by the way creating every time new sessionfactory is wrong approach from your code it looks like you are creating it. make you code something like sessionFactory will created only one time and then get sessions from the sessionFactory whenever you want to perform some operation. it will work for sure try it. for more details please go through this link
0

Agree with Mitul, but i suppose you should also close the session.

Comments

0

You need to set hibernate.hbm2ddl.auto property as update inside hibernate configuration file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.