HIbernate overwrites data when saving / saving an object

I am new to Hibernate. When I save a specific object, it overwrites the data from the existing one.

I used the ID as automatically generated, as shown below:

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

Here I save the object as shown 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 change the value of the entity next time, then it should generate the next identifier, and not start with the 1st id.

any time result will be the same as

 mysql> select * from stud; +----+--------+------+ | id | city | name | +----+--------+------+ | 1 | Mumbai | Test | +----+--------+------+ 

1 row per set (0.00 s)

what i want is as follows:

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

Please help me for the same.

+6
hibernate
source share
4 answers

I understand that this was asked a year ago, but I had the same problem as you, and I decided that I would publish it in case someone else received it.

It turned out that this was a setting in my factory session:

Look for the hibernate.hbm2ddl.auto parameter, most likely you have set create . This setting does things when your factory session is created. What creates is that it deletes the existing schema (at least the tables that it will use), and creates a new one, creating it as if it were rewriting the rows in your table.

There are several different values โ€‹โ€‹that you can select here. For development, I believe that you need to install the update , because it will create new tables for you, if they do not already exist (as for create), but if the table already exists, it will update the schema.

For production, you should stick with checking because it will not change your circuit, just confirm it :)

For more details on the different values, check out this excellent answer.

+13
source share

If you want to do the update, and not add a new row to the database every time, then you need to get the desired object, make the necessary updates and then save the object again.

+2
source share

Try using saveOrUpdate(..)

Note that JPA (Hibernate) objects are identified by their @Id . If your object has the same identifier as in db, an update will occur. Otherwise, a logical insertion will occur.

+1
source share

adding a row to your created database table may help

  `id` INT(11) NOT NULL AUTO_INCREMENT 

AUTO_INCREMENT increases the number of identifiers as when adding a row to the table.

0
source share

All Articles