- This topic has 4 replies, 3 voices, and was last updated 14 years, 5 months ago by support-shalini.
-
AuthorPosts
-
leung1_2000MemberHi
It is a hibernate question.
I am trying to associate 2 tables by foreign key constraint. These 2 tables are job and jobtype.
There are only 2 jobtype. So the oid of the jobtype table will be used as the foreign key in job table, and the jobtype table should not be inserted any new row.
jobtype
0 lookingforjob
1 offerigjobjob
oid|description|jobtypeThe mapping for jobtype in job.hbm.xml is the following
<many-to-one class=”Jobtype” name=”jobtype” cascade=”all”/>As a result, the hibernate create the job table, but without the foreign key constraint reference. And eveytime a job is inserted to the job table, a new jobtype is also inserted to the jobtype table.
CREATE TABLE `job` (
`oid` BIGINT(20) NOT NULL AUTO_INCREMENT,
`description` VARCHAR(255) DEFAULT NULL,
`jobtype` BIGINT(20) DEFAULT NULL,
PRIMARY KEY (`oid`),
KEY `FK11F9D5D27BCB9` (`jobtype`)
) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1How can I config the hibernate to create the job table with foreign key constraint references the oid of another table?
Thanks a lot
support-shaliniMemberleung1_2000,
I could not replicate this at my end. No new rows were inserted in the jobtype table when inserting new rows into Jobs table.
Can you share your project with us so that i can replicate at my end. You can send a mail to support@genuitec.com with subject ATTN:Shalini and a reference to this post.
Brian FernandesModeratorLeung,
You your email to support you mentioned you would like to create a foreign key for a constraint that does not exist in your database.
While we do not provide a UI for this, this can be done manually. If you don’t have a hibernate.reveng.xml file, on page 2 of the RE wizard, select “Setup” to create one. you can now cancel the wizard and edit the file in the XML editor.
You can create an element like so in the file
<table name="localtablename"> <foreign-key constraint-name="constName" foreign-table="foreigntablename"> <many-to-one property="propertyname"/> <column-ref local-column="columnName" foreign-column="foreignColumnName"/> </foreign-key> </table>
You can find more details in this document.See section 6.2.4.3.
After you create the file, attempt to RE again, and make sure this file is selected on page 2 of the RE wizard before hitting finish.
Hope this helps.
leung1_2000MemberHi Brian,
Thank you very much for your instruction. Can you please provide the solution in plain hibernate? I will try the hibernate tool after learning the basic hibernate.
Thanks
Leung
support-shaliniMemberLeung,
Can you try changing your id to assigned in jobtype.hbm.xml as given below?
<id name=”jobtype” type=”java.lang.Long”>
<column name=”jobtype” />
<generator class=”assigned” />
</id>
Now when you run your application twice, a ConstraintViolationException exception will be thrown and no new rows will be inserted into jobtype table.
Let me know how that works for you -
AuthorPosts