Thanks for your reply, Mike!
Yes, there is actually a Client ID column in the Document table. The database schema existed before I started doing this application. In fact I created it by hand and I used it in the first version of this application which used JPA and not Hibernate. Everything worked then so there must be a way to make it work
with Hibernate.
These are the relevant parts of the database schema:
Code:
---------------------Document--------------------------------
| document | CREATE TABLE `document` (
`docId` bigint(20) unsigned NOT NULL auto_increment,
`clientId` bigint(20) unsigned default NULL,
.....
PRIMARY KEY (`docId`),
UNIQUE KEY `docId` (`docId`),
KEY `clientId` (`clientId`),
CONSTRAINT `doc_ibfk_1` FOREIGN KEY (`clientId`) REFERENCES `client` (`clientId`),
CONSTRAINT `FK_doc_clientId` FOREIGN KEY (`clientId`) REFERENCES `client` (`clientId`)
) ENGINE=InnoDB AUTO_INCREMENT=60 ... |
----------------------Client-------------------------------------
| client | CREATE TABLE `client` (
`clientId` bigint(20) unsigned NOT NULL auto_increment,
.......
`docRef` bigint(20) unsigned default NULL,
PRIMARY KEY (`clientId`),
UNIQUE KEY `clientId` (`clientId`),
KEY `docRef` (`docRef`),
CONSTRAINT `FK_client_docRef` FOREIGN KEY (`docRef`) REFERENCES `document` (`docId`),
CONSTRAINT `client_ibfk_1` FOREIGN KEY (`docRef`) REFERENCES `document` (`docId`)
) ENGINE=InnoDB AUTO_INCREMENT=19 ... |
(Some of the things including the extra FK Constraints must have been added by JPA since I didn't write them from the start.)
But just now I realized that I wanted the 'clientId' of Document to be NOT NULL so I changed that in the db schema (in case it would help solving my problem).
The new line (in the Document table) looks like this:
Code:
`clientId` bigint(20) unsigned NOT NULL,
But that was obviously a bad idea because when I tried running the app. I got the SQLException "Field 'clientId' doesn't have a default value".
Is it not possible to set a Foreign Key field to 'NOT NULL'? (I'm running out of time. That's why I'm testing things without being sure about them.)
Anyway, the problem was there even before that little change and I still have no idea what to do about it. Please help me figure it out!
/Ylva