-->
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.  [ 4 posts ] 
Author Message
 Post subject: @ManyToOne persisting problem
PostPosted: Fri May 22, 2009 6:32 pm 
Newbie

Joined: Fri May 22, 2009 6:03 pm
Posts: 5
hello,
In my apllication, there are students and classes. a student can take many classes. So there is OneToMany <--> ManyToOne relationship. Im my case, the best implementation is using a join table. I followed examples I found on internet and at books. So I wrote the code (summary) below..

class Class:

@Id
@GeneratedValue
@Column(name="CLASS_ID")
private Integer id;

private String name;

@ManyToOne
@JoinTable(schema="temel", name = "STUDENT_CLASS",
inverseJoinColumns = @JoinColumn(name = "STUDENT_ID", referencedColumnName = "STUDENT_ID"),
joinColumns = @JoinColumn(name = "CLASS_ID", referencedColumnName = "CLASS_ID"))
private Student student;

(Constructor, getters, setters)

class Student:

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "STUDENT_ID")
private Integer id;

private String name;

@OneToMany(mappedBy = "student", cascade=CascadeType.ALL)
private Collection<Class> classes = new ArrayList<Class>();

(Constructor, getters, setters)

because I use "hibernate.hbm2ddl.auto" all tables, including join table, are created. When I push save button, the student and the class data is saved correctly. but there is no information in my join table. I tried many things. I can not find my mistake. I'd appreciate for any help and tips... thanks...


Top
 Profile  
 
 Post subject: Re: @ManyToOne persisting problem
PostPosted: Wed May 27, 2009 4:00 am 
Newbie

Joined: Fri May 22, 2009 6:03 pm
Posts: 5
no help??


Top
 Profile  
 
 Post subject: Re: @ManyToOne persisting problem
PostPosted: Wed May 27, 2009 6:55 am 
Regular
Regular

Joined: Thu Sep 06, 2007 2:22 am
Posts: 108
Location: Noida,India
My hunch is you are not handling the bidirectional association properly.

Please make sure- if you are adding a class for a student (by just adding a class in a list of class for that student), please make sure you also add class to student. Like this

Student s= new Student();
Class c= new Class();

//handle bidirectional relationship
s.getClasses().add(c);
c.setStudent(s);

let me know whether it is working or not.


Top
 Profile  
 
 Post subject: Re: @ManyToOne persisting problem
PostPosted: Wed May 27, 2009 8:22 am 
Newbie

Joined: Fri May 22, 2009 6:03 pm
Posts: 5
thank you soooo much. I have been struggling for this problem for 2 weeks. I always thought hibernate would handle bidirectional relation for me. I kept checking my annotations to find out problem.. thanks again for the great support.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.