-->
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.  [ 8 posts ] 
Author Message
 Post subject: Cannot insert the value NULL into column......
PostPosted: Mon Jan 26, 2004 4:08 am 
Beginner
Beginner

Joined: Mon Jan 26, 2004 3:31 am
Posts: 20
Location: Jakarta, Indonesia
Hi, I found a lot people has post topic about this error, but can't find one that is similar to mine.

I'm using Hibernate 2.1.1, MS SQL Server 2000 with driver from data direct.

I have one simple table "boss" with no reference.
It have 2 fields :
- id bigint (pk)
- name varchar 70

This is the mapping:
<class name="Boss" table="boss">
<id name="id" type="long" unsaved-value="null">
<generator class="identity"/>
</id>
<property name="name" type="string"/>
</class>

The bean class :
public class Boss
{
private Long id;
private String name;

public Long getId() {
return id;
}

public void setId (Long _id) {
id = _id;
}

public String getName() {
return name;
}

public void setName (String _name) {
name = _name;
}
}

And this is the code that save the object :
Transaction tx = sess.beginTransaction();
Boss b = new Boss();
b.setName("Mr. Tong Ki Cai");
sess.save(b); //error on this line

This is the error :
Cannot insert the value NULL into column 'id', table 'metro.dbo.boss'; column does not allow nulls. INSERT fails.

Susilo


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 26, 2004 5:34 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Looks to me like you did not specify the id column to be autgenerated by the database. Don't know how to do this in SQLServer though.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 26, 2004 6:29 am 
Beginner
Beginner

Joined: Mon Jan 26, 2004 3:31 am
Posts: 20
Location: Jakarta, Indonesia
I only set "id" to be primary key, not auto-generated key. Do I have to? I thought the value will come from Hibernate, not the database.

If that's the problem, I can't find how to set it to auto-generated. Anyone know how? I'm using MS SQL Server 2000.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 26, 2004 6:35 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Well, identity generator means the ID gets assigned by the database. Use something like increment or uuid if you want hibernate to assign the id.

The Syntax for identity should be something like this:

Code:
CREATE TABLE [dbo].[Items] (
        [ItemID] [int] IDENTITY (1, 1) NOT NULL ,
   [ItemName] [char] (50) NOT NULL
)


Google is your friend.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 26, 2004 6:37 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
http://www.sqlteam.com/item.asp?ItemID=8003


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 26, 2004 7:12 am 
Beginner
Beginner

Joined: Mon Jan 26, 2004 3:31 am
Posts: 20
Location: Jakarta, Indonesia
Yes, you're right!
I've managed to fix it.
Thanks a lot, man!
I've spent too long on this error.

Can I ask one last question?
In the documentation, it said this about using increment : "unique only when no other process is inserting data into the same table"
What other process do they mean? Multi-Thread in Java? Manual insert to the table?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 26, 2004 7:20 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Any other process inserting data in the same table which does not use this increment generator I suppose. This might be another application, or some java stuff running in a different VM (cluster for example)


Top
 Profile  
 
 Post subject: a different solution?
PostPosted: Sun Jun 11, 2006 10:45 am 
Senior
Senior

Joined: Sun Jun 11, 2006 10:41 am
Posts: 164
Hi, I was facing the same issue, only I cannot simply decide to redefine the db tables, since they are not "mine" to change. The docs say that using a "native" generator should select most suitable approach for the dbms I'm working with, so:
Assuming the table only has a primary key (without any special dmsb-side id generation settings), can Hibernate help me auto-generate id's (needless to say, "native" didn't work and gave the exception in the first place)?


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