This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes Hibernate Insert Error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate Insert Error" Watch "Hibernate Insert Error" New topic
Author

Hibernate Insert Error

Gopi Kiran Manusani
Greenhorn

Joined: Nov 02, 2009
Posts: 9
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

Joined: Apr 14, 2004
Posts: 10336

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.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Gopi Kiran Manusani
Greenhorn

Joined: Nov 02, 2009
Posts: 9
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

Joined: Apr 14, 2004
Posts: 10336

You have not defined any other identity columns in there? Have a look at your table definition.
Gopi Kiran Manusani
Greenhorn

Joined: Nov 02, 2009
Posts: 9
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

Joined: Apr 14, 2004
Posts: 10336

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

Joined: Nov 02, 2009
Posts: 9
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

Joined: Apr 14, 2004
Posts: 10336

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

Joined: Nov 02, 2009
Posts: 9
Thanks a lot, it works now...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate Insert Error
 
Similar Threads
org.hibernate.MappingException : Unknown Entity?
Foreign key must have same number of columns
Firebird - Hibernate - Not Null
Doubt on hibernate
Hibernate how to update only one colum value