-->
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.  [ 3 posts ] 
Author Message
 Post subject: Help Saving and Object to the DB
PostPosted: Thu Feb 05, 2004 5:52 pm 
Newbie

Joined: Thu Feb 05, 2004 5:42 pm
Posts: 3
I cannot save my objects to the DB. I can do updates fine, but am having trouble creating a new object and saving it in my database.

My DAO object has the following code:
Session session = openSession();
Transaction tx = null;

try {
tx = session.beginTransaction();
Product p = new Product (new Long(4444),
"Chair",
new Double(1.99));
session.save(p);
tx.commit();
}
catch(...) { ... handle exception ... }

When I turn on show_sql=true I see the following in my log file:

Hibernate: insert into PRODUCT (DESCRIPTION, PRICE, ID) values (?, ?, default)

Are the "?" and the word "default" OK?
Then I receive an error from the DB2 database:

JDBCException W net.sf.hibernate.util.JDBCExceptionReporter SQL Error: -407, SQLState: 23502

JDBCException E net.sf.hibernate.util.JDBCExceptionReporter [IBM][CLI Driver][DB2/NT] SQL0407N
Assignment of a NULL value to a NOT NULL column "TBSPACEID=2, TABLEID=2, COLNO=0" is not allowed. SQLSTATE=23502

Can anyone shed some light on what is going on here?
The DB says my values are NULL, but they are not.

Thanks,
Bill


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 05, 2004 6:26 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Please give us your mapping

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 05, 2004 6:56 pm 
Newbie

Joined: Thu Feb 05, 2004 5:42 pm
Posts: 3
I just figured it out.

In my mappnig file I have generator="native" and the problem was that since I am using a DB2 database I have to create the table with

create table products(
id integer not null primary key with default (start 0, increment 1)

I did not do that, I just set up the table as:

create table products(
id integer not null primary key)

When Hibernate generated the SQL it looked like:

insert into product (description, id) values (?, default)

The "default" string apparently has special meaning in DB2, and it tells the database to generate the next unique value for this row. Since I didn't tell DB2 how to generate the key when I created the table, it didn't know how to do so it complained that I was trying to insert NULL into a NOT NULL column.

So to sum it up, once I created the table as define above, everything worked.

Thanks
Bill


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