Saving owned/child objects in Hibernate
- by maxdj
I'm having a hard time wrapping my head around the way hibernate objects work. Here's a little chunk of what my model looks like:
JobOpening:
@ManyToMany(fetch=FetchType.EAGER,cascade=CascadeType.ALL)
@JoinTable(
name="jobOpening_questionSet",
joinColumns=@JoinColumn(name="jobOpenings_id"),
inverseJoinColumns=@JoinColumn(name="questionSets_id")
)
@IndexColumn(name="question_sets_idx",base=0,nullable=false)
public List<QuestionSet> getQuestionSets() {
return questionSets;
}
QuestionSet:
@ManyToMany(mappedBy="questionSets",fetch=FetchType.EAGER)
public Set<JobOpening> getJobOpenings() {
return jobOpenings;
}
@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
@IndexColumn(name="questions_idx",base=0)
@JoinColumn(name="questionset_id",nullable=false)
public List<FormQuestion> getQuestions() {
return questions;
}
FormQuestion:
@ManyToOne
@JoinColumn(name="questionset_id", insertable=false, updatable=false, nullable=false)
@OnDelete(action=OnDeleteAction.CASCADE)
public QuestionSet getQuestionSet() {
return questionSet;
}
Now how would I go about, say, modifying a question in the question set, or changing which jobs a questionset is associated with? For example, to edit a question in a questionset, I imagine I should be able to just get the question via its id, change some values, and merge() it back in, but this doesn't work.
I'm using Hibernate from Spring (appfuse), usually as detached objects.