1

Normally I stored my objects in the DB by inserting or updating them via sql / hql statements.

query = session.createQuery("INSERT INTO Employee(firstName, lastName, salary)");
query.executeUpdate();

Recently I read about Hibernates session.save() method which takes a class and seems to automatically do the magic for inserting the class with it's attributes into the DB.

So I was wondering, what does "save" do?

Does it just generate an SQL statement for the user?

Or are there any advantages or disadvantages compared to the normal hqlQuery way?

Michael Piefel
  • 18,660
  • 9
  • 81
  • 112
MidgarZolom
  • 195
  • 5
  • 15
  • Have a look at [Inserting data in one table using HQL in Hibernate](http://stackoverflow.com/questions/12745041/inserting-data-in-one-table-using-hql-in-hibernate) – OO7 Nov 10 '14 at 12:39

1 Answers1

0

save:

Save method stores an object into the database. It will Persist the given transient instance, first assigning a generated identifier. It returns the id of the entity created.

As docs says,

Persist the given transient instance, first assigning a generated identifier. (Or using the current value of the identifier property if the assigned generator is used.) This operation cascades to associated instances if the association is mapped with cascade="save-update".

Also have a look at What are the differences between the different saving methods in Hibernate?

Community
  • 1
  • 1
Santhosh
  • 8,181
  • 4
  • 29
  • 56