1

i am using the following hibernate mapping file

<class name="com.abdus.hibernate.UserTable" table="tbl_users">
    <meta attribute="class-description">
        This class contains the user details.
    </meta>
    <id name="userId" type="long" column="userId">
        <generator class="native" />
    </id>
    <property name="firstName" type="string" column="firstName" not-null="true" />
    <property name="lastName" type="string" column="lastName" not-null="true" />
    <property name="emailId" type="string" column="emailId" not-null="true" />
    <property name="password" type="string" column="password" not-null="true" />
</class>

And here is my code to insert a new record

public Long add(UserDomain userDomain) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction transaction = null;
    Long userId = null;
    try {
        transaction = session.beginTransaction();
        UserTable userTable = new UserTable();
        userTable.setFirstName(userDomain.getFirstName());
        userTable.setLastName(userDomain.getLastName());
        userTable.setEmailId(userDomain.getEmailId());
        userTable.setPassword(userDomain.getPassword());
        userId = (Long) session.save(userTable);
        System.out.println("userId returned is " + userId);
        transaction.commit();
        userTable.toString();
    } catch (Exception e) {
        transaction.rollback();
        e.printStackTrace();
    } finally {
        session.close();
    }
    return userId;
}

But what i see is every time i insert a record, it always gets inserted a user_id as 1. Meaning there is always only one record in the DB with user_id 1. Why does hibernate not increment the value of user_id every time i insert a record?

Abdus Samad
  • 343
  • 2
  • 12
  • 27

4 Answers4

1

try

<generator class="increment"/>

I hope it solves the problem.

Zohaib
  • 7,026
  • 3
  • 26
  • 35
  • This works if I repeat the inserts within a same jvm. If i restart the server then the increment value is reset to 1 again. I noticed not just that, but if i had inserted lets say 3 records ina single JVM, when i restart the server and insert a record, all the 3 previous records are gone, and only the new record is present. hibernate is very strange – Abdus Samad Nov 22 '11 at 16:28
1

I never used this before but in the spec,Native option explains lie below.

It picks identity,sequence,or hilo,depending on the database.

It is somehow vague to me.

What I understnad is depending on the database, it would pick different ids and the database physically has to have the object,for example, identity or sequence.

For me, I am using Oracle so I sepecifically using

 <generator class="sequence">
            <param name="sequence">SEQUENCE_NAME</param>
 </generator>

If you are using one sepecific DB, please use more specific class. Hope this would help.

exiter2000
  • 548
  • 5
  • 14
  • `I am using MYSQL. I changed my table to create this way `CREATE TABLE tbl_users ( userid integer NOT NULL AUTO_INCREMENT, firstname varcHAR(100) NOT NULL, lastname varcHAR(100) NOT NULL, emailid varcHAR(100) NOT NULL, password varcHAR(50), PRIMARY KEY (userid) );` and wrote the following in mapping file `` this does increment in same JVM, but when server is restarted it is again reset. ` – Abdus Samad Nov 22 '11 at 17:05
  • What about identity optionns. http://stackoverflow.com/questions/1838520/hibernate-problems-with-auto-increment-id-mysql-5 – exiter2000 Nov 22 '11 at 17:25
  • I used identity also. Same issue " this does increment in same JVM, but when server is restarted it is again reset." – Abdus Samad Nov 22 '11 at 17:36
0
<generator class="native"/> 

generated errors while connecting to Sybase Database.

For that purpose, we tried using

<generator class="increment"/> 
FatherMathew
  • 960
  • 12
  • 15
0
Finally i resolved this. I had this in the hibernate.cfg.xml file <property name="hbm2ddl.auto">create</property>

This was causing the schema to be created every time. So i did two things to resolve my issue
- remove the following <property name="hbm2ddl.auto">create</property>
- create the table with auto-increment
Abdus Samad
  • 343
  • 2
  • 12
  • 27