• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Hibernate Insert Error

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot, it works now...
 
I have always wanted to have a neighbor just like you - Fred Rogers. Tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic