Hibernate-Version: 3.2.0
Database: Mysql 5
Beans
Code:
@Entity
public class Invitation implements Serializable
{
private static final long serialVersionUID = 8534169160324262634L;
private long invitationId;
private String name;
private String email;
private String message;
private long boardId;
private long boardSessionId;
private Collection<Recipient> recipients;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
public long getInvitationId()
{
return invitationId;
}
@OneToMany(mappedBy="invitation", cascade = CascadeType.ALL)
public Collection<Recipient> getRecipients()
{
return recipients;
}
........
Code:
@Entity
@Table(name="recipient")
public class Recipient implements Serializable
{
private static final long serialVersionUID = -1041973318619144163L;
private long recipientId;
private String email;
private Invitation invitation;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
public long getRecipientId()
{
return recipientId;
}
@ManyToOne
public Invitation getInvitation()
{
return invitation;
}
........
Simple Unit Test Code:
ArrayList<Recipient> recipients = new ArrayList<Recipient>();
Recipient recipient1 = new Recipient();
recipient1.setEmail("th1@th.com");
Recipient recipient2 = new Recipient();
recipient2.setEmail("th2@th.com");
Recipient recipient3 = new Recipient();
recipient3.setEmail("th3@th.com");
Recipient recipient4 = new Recipient();
recipient4.setEmail("th4@th.com");
recipients.add(recipient1);
recipients.add(recipient2);
recipients.add(recipient3);
recipients.add(recipient4);
Invitation invitation = new Invitation();
invitation.setBoardId(1);
invitation.setBoardSessionId(2);
invitation.setEmail("th@th.com");
invitation.setName("TH");
invitation.setMessage("Hello This is a message");
invitation.setRecipients(recipients);
sessionFactory.getCurrentSession().persist(invitation);
Above unit test execute successfully but the results in the underlying database are not correct.
The foreign key of the recipient table are all null
invitation table
invitationid,name,email,message,boardid,boardsessionid
24,TH,th@th.com,Hello This is a message,1,2
recipient table
recipientid,email,invitation_invitationId
13,th1@th.com,null
14,th2@th.com,null
15,th3@th.com,null
16,th4@th.com,null
All these null should be 24. i do not know whats going on. am i missing something ? or doing wrong?
i have double checked almost everything :(
Any sort of help could be appreciated.
Thanks