I have two tables
packet and packet_notes
packet table is having pk_packet_id as its primary key, autoincrement
packet notes table is having a foreign key fk_packet_id referencing to pk_packet_id of packet table.
Now I have made two Models for these tables Say Packet.java and PacketNotes.java
in PacketNotes hbm file i have a mapping like
Code:
<many-to-one class="Packet" fetch="select" name="packet" foreign-key="fk_packet_id_relation3" cascade="save-update">
<column name="fk_packet_id"/>
</many-to-one>
and its very Obvious that I have a setter and getter for that in PacketNotes.java file like
Code:
setPacket(Packet packetObj)
getPacket()
Now what I am thinking is
Suppose both the tables are blank...
then if I do like this
Code:
Packet p = new Packet();
//not setting any primary key value here
p.setPacketName("first");
PacketNotes notes = New PacketNotes();
notes.setPacketNoteName("first packet note");
notes.setPacket(p);
and if i try to save this notes object like: save(notes);
then what i am thinking is ?
1. packet saved first and an pk_packet_id value will be 1,
2. insert will happen on notes and the Id of packet(1) is assigned automatically to fk_packet_id?
please clear my doubt as it is not happening, and its giving me error like
can't update or add child foreign key contraint fails.