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

user2539823
  • 103
  • 12
  • What is the query you are using ? – Shailendra Oct 28 '13 at 18:53
  • Something like: Query q = session.createQuery("FROM " + "GroupForms" + " o ORDER BY " + sort + " " + this.setOrder(order)); where order and sort are defined – user2539823 Oct 28 '13 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. – alterfox Oct 28 '13 at 20:10

1 Answers1

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

Community
  • 1
  • 1
Alan Hay
  • 22,665
  • 4
  • 56
  • 110
  • 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. – user2539823 Oct 28 '13 at 21:56
  • 1
    Have a look at the orphanRemoval attribute of @OneToMany: http://en.wikibooks.org/wiki/Java_Persistence/Relationships#Orphan_Removal_.28JPA_2.0.29 and http://stackoverflow.com/questions/4329577/jpa-2-0-orphanremoval-true-vs-on-delete-cascade – Alan Hay Oct 28 '13 at 22:17