Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HIbernate overwrite data when persist/save entity

Tags:

hibernate

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..

like image 517
Sweety Avatar asked Oct 27 '10 12:10

Sweety


People also ask

Which is better save or persist in hibernate?

So, it is always better to use Persist() rather than Save() as save has to be carefully used when dealing with Transient object . Important Note : In the above example , the pk of vehicle entity is a generated value , so when using save() to persist a detached entity , hibernate generates a new id to persist .

What is difference between save and saveOrUpdate in hibernate?

Difference between save() and saveOrUpdate() in Hibernate Main difference between save and saveOrUpdate method is that save() generates a new identifier and INSERT record into database while saveOrUpdate can either INSERT or UPDATE based upon existence of record.

How Save method works in hibernate?

Hibernate save method returns the generated id immediately, this is possible because primary object is saved as soon as save method is invoked. If there are other objects mapped from the primary object, they gets saved at the time of committing transaction or when we flush the session.

What is difference between persist and merge in hibernate?

Persist should be called only on new entities, while merge is meant to reattach detached entities. If you're using the assigned generator, using merge instead of persist can cause a redundant SQL statement.


2 Answers

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

like image 119
Skurpi Avatar answered Oct 05 '22 00:10

Skurpi


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.

like image 37
Bozho Avatar answered Oct 05 '22 00:10

Bozho