Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6725561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:49:22+00:00 2026-05-26T09:49:22+00:00

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

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T09:49:23+00:00Added an answer on May 26, 2026 at 9:49 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to Hibernate. We are migrating one of our existing web application
When I try to save a new entity to the database, I have the
Hi i am new to hibernate technology and i have few issues. Suppose if
I am new to Hibernate. I have an exception while running an Hibernate-based application.
I am new to hibernate and I am stumped. In my database I have
I'm quite new to hibernate and have stumbled on this problem, which I can't
im new in hibernate and JPA and i have some problems with annotations. My
I'm new to hibernate. I have a transaction that fails with a HibernateException, yet
I am quite new to hibernate. I have got the following tables: Contract ContractID
I am new to JPA/Hibernate. Currently using EJB3, Hibernate/JPA. I have an inheritacnce structure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.