-->
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.  [ 12 posts ] 
Author Message
 Post subject: error: saving transient object, primary key inserted as null
PostPosted: Thu May 18, 2006 10:55 am 
Newbie

Joined: Thu May 18, 2006 10:47 am
Posts: 5
Hi I'm trying to save a transient object, with session.saveOrUpdate(object), however when the statement executes the primary id is included in the query and is given a null value.

Is there anyway to insert without the primary key??

I am using xDoclet to tag the object class, and using the generator class
'identity' to generate keys.


Thanks


Hibernate version:3.1

Full stack trace of any exception that occurs:
10:46:05,546 WARN JDBCExceptionReporter:71 - SQL Error: 339, SQLState: S1000

10:46:05,546 ERROR JDBCExceptionReporter:72 - DEFAULT or NULL are not allowed as explicit identity values.

10:46:05,562 ERROR HibernateCustomerService:? - org.hibernate.exception.GenericJDBCException: could not insert: [domain.WebAccessCode]

Name and version of the database you are using:
MS SQL 2005

The generated SQL (show_sql=true):
Hibernate: insert into dbo.CustWebAccess (cwa_AccessCode, cwa_custnum, cwa_nopricetext, cwa_password, cwa_permitacct, cwa_per
mitadmin, cwa_permitbackorder, cwa_permiteditusers, cwa_permitorder, cwa_permitseecost, cwa_permitseesell, cwa_permitupload,
RecID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, null)


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 18, 2006 11:31 am 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
in the mapping file, define where the primary key is supposed to come from.

something like
Code:
<id
name="..."
column="...">
<generator class="native">
..
</generator
</id>


and/or, set the unsaved-value attribute to null so hibernate knows that it is a new row and it's not supposed to actually set the pk to null.

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 18, 2006 1:15 pm 
Newbie

Joined: Thu May 18, 2006 10:47 am
Posts: 5
Hi,

I added the unsaved-value="null", but I'm still getting the same error, I've also tried the 'native' generator with no success.

it's still trying to insert RecID with the query with the value null.

Thanks

Here's part of the .hbm.xml file that's generated:

<id
name="recId"
column="recID"
type="java.lang.Integer"
unsaved-value="null"
>
<generator class="identity">
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-WebAccessCode.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 18, 2006 1:27 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
Where is the pk supposed to come from for your row?

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 18, 2006 1:48 pm 
Newbie

Joined: Thu May 18, 2006 10:47 am
Posts: 5
Since I'm making a session.saveOrUpdate(Object) call,

I thought that it's supposed to get auto-generated by the database, as according to the id generator-class, which in this case I have set to identity.

No where in my application do I actually set the PK, I'm under the assumption that Hibernate and the database handles all of that.

So currently I'm creating the object, using:

new WebAccessCode(),

setting all it's properties except the PK, and sending it to the query.

Am I missing something?

thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 18, 2006 2:09 pm 
Newbie

Joined: Fri Nov 04, 2005 11:37 am
Posts: 6
Hi,

are you sure your database supports autoincrement? which database are you using.

-Praful


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 18, 2006 3:25 pm 
Newbie

Joined: Thu May 18, 2006 10:47 am
Posts: 5
Hi,

I'm using MS SQL server 2005,

I've run a manual insert through a sql browser

without specifying the primary key and it works fine, so I'm pretty sure it'll auto-increment.

Could the problem be related to my database driver? I'm using

jtds-1.2.jar for my driver.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 18, 2006 3:35 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
try
Code:
<generator class="native"/>


this will tell hibernate that the database is going to generate the primary key

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 18, 2006 4:10 pm 
Newbie

Joined: Thu May 18, 2006 10:47 am
Posts: 5
Yup

tried the 'native' generator class as well, didn't work.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 18, 2006 4:34 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
how are you trying to save it? Is it a new object?

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject: Having the same problem
PostPosted: Sat Mar 17, 2007 10:19 am 
Newbie

Joined: Sat Mar 17, 2007 10:09 am
Posts: 1
Most of my entity tables in Sql Server 2005 database relay on identity columns to autogenerate the primary key.

I used "native" and "identity" generators, as well as proposed solutions above.

The things is how to tell hibernate to not to pass the NULL value to the underlaying driver when inserting.

In this sample table:

[User]
Id int (identity) PK
Name varchar
Age int

For example, it should generate the following SQL:

insert into [User] (Name, Age) values ('jvargas', 24);

instead of this one:

insert into [User] (Id, Name, Age) values (NULL, 'jvargas', 24);

Because Sql Server 2005 do not allow to pass NULL or DEFAULT values when inserting on Identity columns, the columns should not be specified at all when inserting.

So, how to ignore the PK column in the generated insert ?


Top
 Profile  
 
 Post subject: Re: error: saving transient object, primary key inserted as null
PostPosted: Thu Dec 02, 2010 5:33 pm 
Newbie

Joined: Thu Dec 02, 2010 5:29 pm
Posts: 1
jvargas,
Do the following to get up and running:


Using Enterprise Manager remove the identity insert property from your table.

Apply a primary key to this column (if you haven't already).

Then set your key generation to increment.

EX:
Code:
  <id name="id" column="id_id" unsaved-value="0">
            <generator class="increment"/>
        </id>



I've found it's better to allow hibernate to handle the key generation in lieu of sql server.

Hope that helps.


Philip


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