Ruchi
Ruchi

Reputation: 5192

HIbernate overwrite data when persist/save entity

I am new in Hibernate.when i save particular entity then it rewrite data from existing one.

I have used ID as auto generated as below:

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id")
private int id;

Here i save Entity as below:

class StudDAO() {

public static void main(String[] args){
     StudDAO obj = new StudDAO();
     Stud stud = new Stud();
     stud.setName("Test");
     stud.setCity("Mumbai");
     obj.createStud(stud);
}

public void createStud(Stud stud) {
  try {

    Session session = HibernateSessionFactory.getSessionFactory().openSession();
    Transaction transaction = session.beginTransaction();
    session.save(stud);
    transaction.commit();

    } catch (Exception e) {
      // TODO: handle exception
       e.printStackTrace();
       transaction.rollback();
    }
}

}

if i will change entity value next time then it should generate next id rather than starting form 1st id.

any time result would be same like

mysql> select * from stud;

+----+--------+------+
| id | city   | name |
+----+--------+------+
|  1 | Mumbai | Test |
+----+--------+------+

1 row in set (0.00 sec)

what i want is in result like below:

mysql> select * from stud;
+----+--------+------+
| id | city   | name |
+----+--------+------+
|  1 | Mumbai | Test |
|  2 | Mumbai | Test |
+----+--------+------+
2 rows in set (0.00 sec)

Please help me for same..

Upvotes: 9

Views: 19679

Answers (5)

Navinder Singh
Navinder Singh

Reputation: 1

If you are using class generator Auto then the records will automatically get added to a new auto incremented id. And the same data in the previous record will be written into the next id also. So if you to you insert a new row either change the data test and Mumbai each time you run the application(Bad Practice) or use Scanner class to read the data from the Keyboard and pass it dynamically to the setter. Hope You understood it.

Upvotes: 0

Hem M
Hem M

Reputation: 326

adding a line in your database table created might help

 `id` INT(11) NOT NULL AUTO_INCREMENT

AUTO_INCREMENT increments the id count as and when you add a row into the table.

Upvotes: 0

Skurpi
Skurpi

Reputation: 1020

I realize this is asked a year ago, but I had the exact same problem as you and I figured I'd post it in case anyone else has it.

It turned out it was a setting in my session factory:

Look for the setting hibernate.hbm2ddl.auto, most probably you have it set to create. This setting does things when your session factory is created. What create does is that it drops the existing schema (at least the tables which it will use) and creates a new one, making it appear like it is overwriting the rows in your table.

There are a few different values you can choose from here. For development I believe you'd want it set update as it will create new tables for you if they don't already exist (just like create), but if the table already exists it will update the schema.

For production you should stick to validate as it will not alter you schema, just validate it :)

For more details of the different values, check this excellent answer

Upvotes: 21

Bozho
Bozho

Reputation: 597106

Try using saveOrUpdate(..)

Note that JPA (Hibernate) entities are identified by their @Id. If your object has the same id as the one in the db, an update will occur. Otherwise, logically, insert will happen.

Upvotes: 3

Derek Greer
Derek Greer

Reputation: 16262

If you are wanting to do an update rather than adding a new row to the database each time then you need to retrieve the desired entity, make any desired updates, and then save the entity again.

Upvotes: 2

Related Questions