Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate Insert Error

 
Gopi Kiran Manusani
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Iam getting follwoing Error when i try to insert a recod using hibernate
" Cannot insert explicit value for identity column in table 'tbRoutingRules' when IDENTITY_INSERT is set to OFF."

Code i am using is as follows


Mapping File for Routing Rule is





java Class for the mapping



Here routingRuleID is PrimaryKey and Auto Increment in table defination.
Please suggest me where exactly i am doing wrong
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have used a primative for your primary key. This means you are always trying to set it to 0 since primatives can't be null. Use the wrapper class instead.
 
Gopi Kiran Manusani
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still getting same error after using wrapper class instead of premitive.
It got printed null for RoutingRuleID in console.Here is the updated code.

 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have not defined any other identity columns in there? Have a look at your table definition.
 
Gopi Kiran Manusani
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only One Identity column defined in the tables, i.e, RoutingRuleID.Through backend i am able to insert with the generated query , but i could not insert from application.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try turning on show_sql to see what Hibernate is actually using. The error is fairly uncategorical so something must be setting the identity field. Hiebrnate itself should not be doing this I don't think.
 
Gopi Kiran Manusani
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my code..


Following it got printed in the console...

RoutingRUle ID: null

Hibernate: insert into tbRoutingRules (TeamID, TransactionEventType, Ordinal, Field, Operator, CompareValue, RouteToTeamID, RoutingRuleID) values (?, ?, ?, ?, ?, ?, ?, ?)Nov 2, 2009 6:47:59 PM org.hibernate.util.JDBCExceptionReporter logExceptions
WARNING: SQL Error: 544, SQLState: S0001
Nov 2, 2009 6:47:59 PM org.hibernate.util.JDBCExceptionReporter logExceptions
SEVERE: Cannot insert explicit value for identity column in table 'tbRoutingRules' when IDENTITY_INSERT is set to OFF.
Nov 2, 2009 6:47:59 PM org.hibernate.event.def.AbstractFlushingEventListener performExecutions
SEVERE: Could not synchronize database state with session
org.hibernate.exception.SQLGrammarException: could not insert: [com.conagrafoods.merchstore.mappings.RoutingRules]
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
D'oh! Sorry should have spotted this straight away. You are using an increment generation strategy, not an identity generation strategy. So you are asking for Hibernate to generate a value for the primary key. You should change this to identity if that is what you want to use.
 
Gopi Kiran Manusani
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot, it works now...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic