I'm trying to follow an example I saw by using the session.save() function to insert a new record into the table.
The problem is that the primary key is an auto-increment index and I get the following error:
SEVERE: Cannot insert explicit value for identity column in table 'tbUser' when IDENTITY_INSERT is set to OFF.I suspect I need to turn the IDENTITY_INSERT flag to ON, but have not been able to find an example that shows how to do that.
Any suggestions?
Thanks.
Code:
public int addUser( String userName, String passwd,
String firstName, String middleName, String lastName,
String email, String phone, Date termDate,
Date hireDate, int updtUserId, int id )
{
int status = 0;
TbUser user = null;
try
{
org.hibernate.Transaction tx = session.beginTransaction();
user = new TbUser();
user.setVcUserName(userName);
user.setVcUserPsw(passwd);
user.setVcUserFirstName(firstName);
user.setVcUserMiddleName(middleName);
user.setVcUserLastName(lastName);
user.setVcEmailAddress(email);
user.setVcPhoneNumber(phone);
user.setDtTerminatedDate(termDate);
user.setDtHireDate(hireDate);
user.setDtUpdTime( new Date() );
user.setIupdUserId(updtUserId);
try
{
session.save(user);
tx.commit();
status = 1;
}
catch(Exception e)
{
tx.rollback();
e.printStackTrace();
status = -1;
}
// finally
// {
// try
// {
// session.close();
// }
// catch (Exception e)
// {
// e.printStackTrace();
// }
// }
}
catch( Exception ex )
{
ex.printStackTrace();
status = -1;
}
return status;
}