简体   繁体   中英

How can I insert data into a database using hibernate without erasing its previous data?

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





}

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 eg 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()

同意Mitul,但我想您也应该关闭会议。

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

In ur case ur 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)

您需要将hibernate.hbm2ddl.auto属性设置为hibernate配置文件中的update

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM