-->
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.  [ 1 post ] 
Author Message
 Post subject: How to save Many to Many values?
PostPosted: Sun Mar 20, 2011 2:54 pm 
Newbie

Joined: Wed Feb 16, 2011 1:56 pm
Posts: 7
I've an Applicant class which has a many to many relationship with Category class.

My Objects are as follows:
Code:
@Entity
@Table(name = "applicants")
public class Applicants implements java.io.Serializable {

   private static final long serialVersionUID = 1L;
   private Integer applicantId;
   .
   .
   some more fields
   .
   .
   
   private Set<Category> categories = new HashSet<Category>(0);
   
   getters & setters...
   
   [B]@ManyToMany(fetch = FetchType.LAZY)
   @JoinTable(name = "applicants_category", joinColumns = { @JoinColumn(name = "ApplicantID", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "CategoryID", nullable = false, updatable = false) })
   public Set<Category> getCategories() {
      return this.categories;
   }[/B]

   public void setCategories(Set<Category> categories) {
      this.categories = categories;
   }
   
}


Code:
@Entity
@Table(name = "category")
public class Category implements java.io.Serializable {
   
   private static final long serialVersionUID = 1L;
   private Integer categoryId;
   .
   .
   some more fields
   .
   private Set<Applicants> applicantses = new HashSet<Applicants>(0);
   
   getters & setters...
   
   [B]@ManyToMany(fetch = FetchType.LAZY, mappedBy = "categories")
   public Set<Applicants> getApplicantses() {
      return this.applicantses;
   }[/B]

   public void setApplicantses(Set<Applicants> applicantses) {
      this.applicantses = applicantses;
   }
}


Now I've a jsp from where I've a model attribute of Applicant class named "applicantAttribute" and "categoryList" is a List<Category> & I'm trying to save the form objects as shown below:

Code:
<form:form modelAttribute="applicantAttribute" method="POST" action="registerApplicant.html">
   
      .
      .
      .
      some form elements
      .
      .
      
      [B]<form:label path="categories">Categories</form:label>
      <form:checkboxes items="${categoryList}" path="categories" itemValue="categoryName" itemLabel="categoryName" />[/B]
      
      <input type="submit" value="Register"/>
   
</form:form>


On submitting, my implementation logic is as follows:

Code:
public String registerApplicant(Applicants applicants){

      Session session = sessionFactory.getCurrentSession();
      [B]session.save(applicants);[/B]                                    
      
      return "success";
}   


It gives me the following error:
Code:
org.hibernate.TransientObjectException: object references an unsaved transient instance -[B] save the transient instance before flushing: com.myproject.contact.form.Category[/B]


I tried to add CascadeType.ALL etc. which resulted in duplicate entries in Category table.

How can I add applicants_categories mapping which is many to many? What am i doing wrong?

Please guide!! I've wasted around 24 hours already trying to solve it. Any help will be really appreciated.


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

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.