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?