-->
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.  [ 4 posts ] 
Author Message
 Post subject: Value already present for Identity column
PostPosted: Wed Jun 10, 2009 2:25 pm 
Newbie

Joined: Mon Apr 06, 2009 4:58 pm
Posts: 3
Hello all

I reverse engineered my database to produce entity classes for me. For one particular entity, i have a coloumn as follows
@Entity
Person
@javax.persistence.Id @javax.persistence.Column(name = "PID", unique = true, nullable = false, insertable = true, updatable = true) public int getPID(){
..
}

In the database the column is an Identity column

When I try to instantiate an object of type Person, it already seems to have a value of 0 for PID. And when I try to insert the Person object into the database, i get this

Cannot insert explicit value for identity column in table "Person" when IDENTITY_INSERT is set to OFF

Which I can understand because a value of 0 is being given to a column which is meant to be auto generated. My question is how was the value for 0 assigned to PID column.

Any ideas what am I missing?

Thanks
K


Top
 Profile  
 
 Post subject: Re: Value already present for Identity column
PostPosted: Fri Jun 12, 2009 8:11 am 
Regular
Regular

Joined: Fri May 12, 2006 4:05 am
Posts: 106
Hi,

1.) Since your pid is an int-Property it must have some value, since primitive ints can't be null (only Integers could be).
2.) As pid is an identity-column the value is generated by the database. This should be told to your JPA-Provider by adding an @GeneratedValue-annotation.

Only adding the @GeneratedValue should be sufficient to solve your problem because then hibernate should ignore any value that pid is set to.


Top
 Profile  
 
 Post subject: Re: Value already present for Identity column
PostPosted: Fri Jun 12, 2009 2:25 pm 
Newbie

Joined: Mon Apr 06, 2009 4:58 pm
Posts: 3
Thanks for your reply. I added @GeneratedValue annotation and that seemed to have solved the issue. But I was wondering why wasnt that annotation put in when I reverse engineered that particular entity.

My assumption was hibernate would have enough knowledge to know that a particular column in a table is system generated and it should put the appropriate annotations for it

Thanks again
K


Top
 Profile  
 
 Post subject: Re: Value already present for Identity column
PostPosted: Sun Aug 25, 2013 8:19 pm 
Newbie

Joined: Fri Aug 23, 2013 3:53 am
Posts: 1
nir_kar wrote:
Thanks for your reply. I added @GeneratedValue annotation and that seemed to have solved the issue. But I was wondering why wasnt that annotation put in when I reverse engineered that particular entity.

My assumption was hibernate would have enough knowledge to know that a particular column in a table is system generated and it should put the appropriate annotations for it

Thanks again
K


I have tried the same but am getting this error, this is a sample test with the table Sample have id(identity) and value(varchar) columns

Exception in thread "main" org.hibernate.exception.GenericJDBCException: could not insert: [com.refdata.util.Sample]
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:40)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2158)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2638)
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:48)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:298)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:107)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:535)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:523)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:519)
at com.refdata.util.Test.main(Test.java:30)
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: A result set was generated for update.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:190)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:417)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:350)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:5696)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1715)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:180)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:155)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeUpdate(SQLServerPreparedStatement.java:314)
at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:73)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:33)
... 16 more
Java Result: 1

This is my hibernate config file.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="hibernate.connection.url">jdbc:sqlserver://localhost;databaseName=test</property>
<property name="hibernate.connection.username">test</property>
<property name="hibernate.connection.password">111</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.connection.pool_size">100</property>
<property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>
<mapping class="com.refdata.util.ReferenceGroup"></mapping>
<mapping class="com.refdata.util.Sample"></mapping>
</session-factory>
</hibernate-configuration>

Am using sqljdbc4.jar for the driver


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.