-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: null values for foreign key in a OneToMany relationship
PostPosted: Sun Dec 31, 2006 4:54 pm 
Newbie

Joined: Tue Aug 30, 2005 1:16 am
Posts: 3
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


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 31, 2006 4:57 pm 
Newbie

Joined: Tue Aug 30, 2005 1:16 am
Posts: 3
Sorry for not posting the db schema in the original post. Here it is
Code:
/*Table structure for table `invitation` */
CREATE TABLE `invitation` (
  `invitationid` int(11) NOT NULL auto_increment,
  `name` varchar(30) default NULL,
  `email` varchar(50) default NULL,
  `message` text,
  `boardid` int(11) default NULL,
  `boardsessionid` int(11) default NULL,
  PRIMARY KEY  (`invitationid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

/*Table structure for table `recipient` */
CREATE TABLE `recipient` (
  `recipientid` int(11) NOT NULL auto_increment,
  `email` varchar(50) default NULL,
  `invitation_invitationId` int(11) default NULL,
  PRIMARY KEY  (`recipientid`),
  KEY `FK_recipient` (`invitation_invitationId`),
  CONSTRAINT `recipient_ibfk_1` FOREIGN KEY (`invitation_invitationId`) REFERENCES `invitation` (`invitationid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 02, 2007 5:24 am 
Newbie

Joined: Fri Nov 24, 2006 10:43 am
Posts: 3
Location: NOIDA
Can you provide your Invitation.hbm.xml.

1. You have defined your POJO to implement one-To-Many relationship.

2. But at looking definition of your schema it gives impression of using <Many-to-One relationship.

recipient table
recipientid,email,invitation_invitationId

Note:

a) So, If you are using point#1 , your invitation table will have ids of recipients instead.

b) If you are going with point#2, your Recipient Object should refer to Invitation.

_________________
Rajneesh
Email : rajneeshk@contata.co.in
-------------------------------------

PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 03, 2007 5:22 pm 
Newbie

Joined: Fri Dec 15, 2006 11:09 am
Posts: 10
I think that you need to specify the correct foreign key column name in your annotation. I think it defaults to the property name if you do not specify otherwise.

For this getter of your Recipient class:

@ManyToOne
public Invitation getInvitation()
{
return invitation;
}

Try something like this instead:

@ManyToOne
@JoinColumn(name="invitation_invitationId")
public Invitation getInvitation()
{
return invitation;
}

Hope this helps,
Jeff


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 04, 2007 4:58 pm 
Newbie

Joined: Tue Aug 30, 2005 1:16 am
Posts: 3
Thanks for the replies...

rajneesh: i am using annotations so i do not have any xml files :)

jwood: i tired this too, still not working :(


any other idea ?

i am begining to get tired of this ....


Top
 Profile  
 
 Post subject: I have absolutely same problem
PostPosted: Fri May 18, 2007 10:49 am 
Newbie

Joined: Sun Dec 17, 2006 9:28 pm
Posts: 3
Did you solve it ?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.