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.