-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: SET IDENTITY_INSERT tableName ON returns result of -1
PostPosted: Wed Nov 02, 2011 4:09 pm 
Newbie

Joined: Wed Nov 02, 2011 3:50 pm
Posts: 1
Originally, I received the following error from Hibernate:

4340 [main] ERROR org.hibernate.util.JDBCExceptionReporter - Cannot insert explicit value for identity column in table 'UserSynonym' when IDENTITY_INSERT is set to OFF.

So I searched the Internet for solutions.

Here is my table definition:

<hibernate-mapping package="org.abg.convert.domain">
<class name="UserSynonym" table="UserSynonym">
<id name="orgUserName" length="48">
<generator class="assigned" />
</id>
<property name="userName" length="48" />
</class>
</hibernate-mapping>

I am using SQLServer. I tried typing the following SQL directly into the management studio:

set IDENTITY_INSERT UserSynonym ON

This executed correctly;

However, when I attempt to execute this from within my Java code running under Hibernate, I get a return result of -1 and my inserts still fail with the error that the IDENTITY_INSERT is set to OFF.

Here is my Java code for setting the "IDENTITY_INSERT"

public int identityInsert(String opt) {

Session session = HibernateUtil.getSessionFactory().getCurrentSession();
String query = "SET IDENTITY_INSERT "+tableName+" "+opt;
System.out.println("IDENTITY_INSERT query: "+query);
session.beginTransaction();
int result = session.createSQLQuery(query).executeUpdate();
System.out.println("Result of IDENTITY_INSERT: "+result);
session.getTransaction().commit();

return result;

}

I have verified that the "tableName" variable has the correct name; i.e., UserSynonym. I am calling this method iimmediately before I start to load the data.

Does anyone have any suggestions as to what I am doing wrong or a better way to solve this problem.

Thanks for any help.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.