-->
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.  [ 6 posts ] 
Author Message
 Post subject: .:: IDENTITY_INSERT to ON
PostPosted: Fri Mar 26, 2004 9:39 am 
Beginner
Beginner

Joined: Tue Jan 27, 2004 8:25 am
Posts: 45
I have to import some tables to my new database via hibernate, and I need the same primary keys values for the tables.
Is there a way to set the IDENTITY_INSERT of the table to ON, so I can create an object with the same code, and save it with that code?

Thanks in advance!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 26, 2004 11:06 am 
Beginner
Beginner

Joined: Tue Jan 27, 2004 8:25 am
Posts: 45
ok, I have managed to do it by:
Code:
HibernateSession.currentSession().connection().createStatement().executeUpdate("SET IDENTITY_INSERT Hotel ON");


But when I assign the code of the hotel,
hotel.setCode(new Long(rs.getInt("code"))) and afterwards I save the hotel with hibernate:

Code:
HibernateSession.currentSession().connection().createStatement().executeUpdate("SET IDENTITY_INSERT Hotel ON");
            HibernateSession.currentSession().save(hotel);
            HibernateSession.currentSession().connection().createStatement().executeUpdate("SET IDENTITY_INSERT Hotel OFF");

I have an SqlException, which says:
Code:
java.sql.SQLException: Explicit value must be specified for identity column in table 'Hotel' when IDENTITY_INSERT is set to ON.


How this can be? I already have setted the hotel's code...

can anyone help me please?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 26, 2004 3:32 pm 
Regular
Regular

Joined: Tue Aug 26, 2003 3:09 pm
Posts: 58
Seeing your mapping files would help. My guess is the mapping uses some id generator other than "assigned".


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 24, 2005 4:55 pm 
Newbie

Joined: Tue Nov 22, 2005 3:16 pm
Posts: 4
I'm having the same problem, but I'm using 3.1rc3. This is my mapping file:

<class name="com.sagebrushcorp.oberon.dataobject.Provider" table="PROVIDERS" lazy="false">
<id name="id">
<generator class="identity" />
</id>
<property name="name" />
<list name="contacts" table="CONTACTS" lazy="false" cascade="all-delete-orphan" fetch="join">
<key column="providerId" foreign-key="true" not-null="true" />
<list-index column="id" />
<composite-element class="com.sagebrushcorp.oberon.dataobject.Contact">
<property name="name"/>
<property name="email"/>
<property name="phone" />
<property name="type" />
</composite-element>
</list>
</class>


I've found that there's a MS bug on setting the IDENTITY_INSERT, so I patched my MSDE. All to no result... if I call the "set IDENTITY_INSERT PROVIDERS on" from SquirrelSQL, using the same jdbc driver, url, username and password as from hibernate, it works and the insert statements go through.

But if I call use session.connection().createStatement().executeUpdate("SET IDENTITY_INSERT PROVIDERS ON") from within the import program, it seems to have no effect and I still get:

ERROR| org.hibernate.event.def.AbstractFlushingEventListener - Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: could not insert: [com.sagebrushcorp.oberon.dataobject.Provider]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:69)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2077)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2426)
at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:51)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:243)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:227)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:296)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:908)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:344)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at com.sagebrushcorp.oberon.pcd.TestDataCreator.migrateProviders(TestDataCreator.java:92)
at com.sagebrushcorp.oberon.pcd.TestDataCreator.main(TestDataCreator.java:46)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:78)
Caused by: java.sql.SQLException: Cannot insert explicit value for identity column in table 'PROVIDERS' when IDENTITY_INSERT is set to OFF.
at net.sourceforge.jtds.jdbc.SQLDiagnostic.addDiagnostic(SQLDiagnostic.java:364)
at net.sourceforge.jtds.jdbc.TdsCore.tdsErrorToken(TdsCore.java:2754)
at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2195)
at net.sourceforge.jtds.jdbc.TdsCore.getMoreResults(TdsCore.java:620)
at net.sourceforge.jtds.jdbc.JtdsStatement.processResults(JtdsStatement.java:483)
at net.sourceforge.jtds.jdbc.JtdsStatement.executeSQL(JtdsStatement.java:445)
at net.sourceforge.jtds.jdbc.JtdsPreparedStatement.executeUpdate(JtdsPreparedStatement.java:402)
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:23)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2061)
... 17 more


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 25, 2005 5:30 am 
Regular
Regular

Joined: Sat Nov 05, 2005 5:33 am
Posts: 70
Location: Linz, Austria
I guess it won't work with this mapping since you tell Hibernate to use the identity generator.
If you want to assign your value, you'll probably have to use the assigned generator.

_________________
hth,
Heinz
Don't forget to rate if this helped


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 26, 2005 1:07 pm 
Newbie

Joined: Tue Nov 22, 2005 3:16 pm
Posts: 4
I don't understand why "IDENTITY_INSERT" status was not set for the PROVIDERS table. I've set that by using the same connection the session is using, and still I get the SQLException (note that its not a HibernateException).


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

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.