2

I have following problem with relations between 3 entites: Form, FormConfig, and GroupForms. The model is manyToMany relation between Form and GroupForms, but there is some additional data associated with this join, so I modeled entity FormConfig. Form is related with FormConfig in OneToMany relation, and GroupForms is related to FormConfig in OneToMany relation. In code it looks like:

Form.java:

...
@OneToMany(mappedBy = "form", cascade = CascadeType.ALL)
private List<FormConfig> formConfigs = new ArrayList<FormConfig>();
...

GroupForms.java:

...
@OneToMany(mappedBy="group", fetch=FetchType.EAGER)
private List<FormConfig> formConfigs = new ArrayList<FormConfig>();

FormConfig:

...
@ManyToOne
@JoinColumn(name = "kf_grupa_id")
private GroupForms group;

@ManyToOne
@JoinColumn(name = "kf_formularz_id")
private Form form;
....

I created some group, and now I want to create new Form and join it to GroupForms, so:

void createFormInGroup(GroupForms groupForms) {
   Form form = new Form();
   /*setters execution*/    
   form.set(..);
   ....
   FormConfig formConfig = new FormConfig();
   /*setters execution*/
   formConfig.set(..);
   ....
   formConfig.setGroup(groupForms);
   formConfig.setForm(form);
   form.getFormConfigs().add(formConfig);
   groupForms.getFormConfigs().add(formConfig);

   /* code responsible for beginTransaction */
   session.saveOrUpdate(formConfig);
   session.saveOrUpdate(form);
   session.saveOrUpdate(groupForms);
   /* code responsible for endTransaction */
}

I call this function two times, for one groups, which means that I want to create two forms and those forms should be in one group. But unfortunately, query using hibernate, returns me two rows in entity GroupForms. I check my tables, and there is only one row in table associated with entity GroupForms. Can anyone help with that? I do not have idea why hibernate returns more GroupForms than exist in database.

Regards

3
  • What is the query you are using ? Commented Oct 28, 2013 at 18:53
  • Something like: Query q = session.createQuery("FROM " + "GroupForms" + " o ORDER BY " + sort + " " + this.setOrder(order)); where order and sort are defined Commented Oct 28, 2013 at 18:58
  • Please paste the SQL that is generated, you can find this in the log or in the console when it is executed. I am confused by the this.setOrder(order) part in the query. Commented Oct 28, 2013 at 20:10

1 Answer 1

2

This most likely caused by the following:

@OneToMany(mappedBy="group", fetch=FetchType.EAGER)

See here for further discussion:

Hibernate Criteria returns children multiple times with FetchType.EAGER

and my answer to a similar question here:

Hibernate and criteria that return the same items multiple times

Sign up to request clarification or add additional context in comments.

2 Comments

Thank You. I've add this: @Fetch(FetchMode.SUBSELECT) and query returns me properly number of group! But there is appear another bug: now I can't delete FormConfig element: form.getFormConfigs().clear(); session.saveOrUpdate(form); In database there is still FormConfig element.
Have a look at the orphanRemoval attribute of @OneToMany: en.wikibooks.org/wiki/Java_Persistence/… and stackoverflow.com/questions/4329577/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.