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