-->
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.  [ 2 posts ] 
Author Message
 Post subject: many-to-many with composite key; problem with nullable pk
PostPosted: Thu Apr 07, 2011 10:15 am 
Newbie

Joined: Thu Mar 17, 2011 11:09 am
Posts: 8
Hello,

I need to implement a many-to-many association with some association attributes. Therefore I found this tutorial: http://boris.kirzner.info/blog/archives ... osite-key/

I understood, what the author is doing, but it doesn't work in my case. The tables product and item are created and both are filled with my objects. The table product_item could not even be created because DB2 tells me, that a primary key may not be NULL. However I set both IDs to nullable = false. Here is the whole errormessage:
Code:
07.04.2011 16:05:59 org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: Running hbm2ddl schema export
07.04.2011 16:05:59 org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: exporting generated schema to database
07.04.2011 16:06:00 org.hibernate.tool.hbm2ddl.SchemaExport create
SCHWERWIEGEND: Unsuccessful: create table product_item (item_id integer, product_id integer, primary key (item_id, product_id))
07.04.2011 16:06:00 org.hibernate.tool.hbm2ddl.SchemaExport create
SCHWERWIEGEND: DB2 SQL Error: SQLCODE=-542, SQLSTATE=42831, SQLERRMC=ITEM_ID, DRIVER=4.9.78
07.04.2011 16:06:00 org.hibernate.tool.hbm2ddl.SchemaExport create
SCHWERWIEGEND: Unsuccessful: alter table product_item add constraint FK3C742463F24F48C9 foreign key (item_id) references item
07.04.2011 16:06:00 org.hibernate.tool.hbm2ddl.SchemaExport create
SCHWERWIEGEND: DB2 SQL Error: SQLCODE=-204, SQLSTATE=42704, SQLERRMC=DB2ADMIN.PRODUCT_ITEM, DRIVER=4.9.78
07.04.2011 16:06:00 org.hibernate.tool.hbm2ddl.SchemaExport create
SCHWERWIEGEND: Unsuccessful: alter table product_item add constraint FK3C742463253F490B foreign key (product_id) references product
07.04.2011 16:06:00 org.hibernate.tool.hbm2ddl.SchemaExport create
SCHWERWIEGEND: DB2 SQL Error: SQLCODE=-204, SQLSTATE=42704, SQLERRMC=DB2ADMIN.PRODUCT_ITEM, DRIVER=4.9.78
07.04.2011 16:06:00 org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: schema export complete


Anybody an idea?
Thanks in advance!
Stefan

PS: This is the error description from IBM:
Code:
-542

column-name CANNOT BE A COLUMN OF A PRIMARY KEY, A UNIQUE CONSTRAINT, OR A PARENT KEY BECAUSE IT CAN CONTAIN NULL VALUES
Explanation

A column that is identified in a PRIMARY KEY, a UNIQUE constraint clause, or a parent key (through a REFERENCES clause) is defined to allow null values. Null values are not permitted for these keys and clauses.
System action

The statement cannot be processed.
Programmer response

In the case of a column identified in a PRIMARY KEY or a UNIQUE constraint clause, correct the statement and resubmit it.

In the case of a column identified in a REFERENCES clause:

    Drop the parent table.
    Recreate it with referenced columns defined as NOT NULL.
    Resubmit the statement.

SQLSTATE

42831
Parent topic: SQL error codes



Top
 Profile  
 
 Post subject: Re: many-to-many with composite key; problem with nullable pk
PostPosted: Thu Apr 07, 2011 10:41 am 
Newbie

Joined: Thu Mar 17, 2011 11:09 am
Posts: 8
I could get rid of the first error (-542) by adding @Column(nullable = false) to the ProductItemPk class after the @ManyToOne Annotation.

Now the product_item table is being created, but both column types are Varchar. Because the Ids are Integers, those two columns can not be used as a foreign key.

I continue looking for a solution, however if anybody can help, I would be very thankful!

Stefan

Edit: The new error message
Quote:
INFO: exporting generated schema to database
07.04.2011 16:35:50 org.hibernate.tool.hbm2ddl.SchemaExport create
SCHWERWIEGEND: Unsuccessful: alter table product_item add constraint FK3C742463745E0A15 foreign key (item) references item
07.04.2011 16:35:50 org.hibernate.tool.hbm2ddl.SchemaExport create
SCHWERWIEGEND: DB2 SQL Error: SQLCODE=-538, SQLSTATE=42830, SQLERRMC=FK3C742463745E0A15;DB2ADMIN.ITEM, DRIVER=4.9.78
07.04.2011 16:35:50 org.hibernate.tool.hbm2ddl.SchemaExport create
SCHWERWIEGEND: Unsuccessful: alter table product_item add constraint FK3C742463AA504B8F foreign key (product) references product
07.04.2011 16:35:50 org.hibernate.tool.hbm2ddl.SchemaExport create
SCHWERWIEGEND: DB2 SQL Error: SQLCODE=-538, SQLSTATE=42830, SQLERRMC=FK3C742463AA504B8F;DB2ADMIN.PRODUCT, DRIVER=4.9.78
07.04.2011 16:35:50 org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: schema export complete


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