I am using Hibernate 3.0.5, Java 6, MySQL 5.5. I thought I had everything set up correctly, but clearly I do not.
Here are the class mappings:
<class name="com.rc.model.partner.Partner" table="partner">
...
<set name="pacChannelSet" table="pac_channel" lazy="false" cascade="all">
<key column="partnerid"/>
<one-to-many class="com.rc.model.partner.PacChannel"/>
</set>
</class>
<class name="com.rc.model.partner.PacChannel" table="pac_channel">
...
<many-to-one name="partner" class="com.rc.model.partner.Partner" column="partnerid"
foreign-key="partnerid" cascade="none"/>
</class>
Here is the Java code:
pacChannelDAO.saveOrUpdate(pacChannel1);
partner.addPacChannel(pacChannel1);
pacChannelDAO.saveOrUpdate(pacChannel2);
partner.addPacChannel(pacChannel2);
partnerDAO.saveOrUpdate(partner);
Here are the debug statements, indicating an attempt to nullify the partnerid in the pac_channel table, and error that results:
11:27:07,088 DEBUG [SQL] update partner set ... where partnerid=? and updateddate=?
11:27:07,165 DEBUG [SQL] update pac_channel set partnerid=?, channelname=?, apikey=?, active=?, dailyrequestlimit=? where pacchannelid=?
11:27:07,193 DEBUG [SQL] update pac_channel set partnerid=?, channelname=?, apikey=?, active=?, dailyrequestlimit=? where pacchannelid=?
11:27:07,300 DEBUG [SQL] update pac_channel set partnerid=null where partnerid=? and pacchannelid=?
11:27:07,413 ERROR [JDBCExceptionReporter] Cannot add or update a child row: a foreign key constraint fails (`mexp`.`pac_channel`, CONSTRAINT `chan_partnerid_fk` FOREIGN KEY (`partnerid`) REFERENCES `partner` (`partnerid`) ON DELETE NO ACTION ON UPDATE NO ACTION)
Why is Hibernate generating the last SQL statement with “set partnerid=null”? Any insight would be greatly appreciated. Thanks!
I never use XML to map my entities, but I think your mapping is wrong. The one side should be the inverse side of the many side, as shown in the documentation: