Please have a look at this, i think this is a major bug...
Parent has
two oneToMany relations and the "hibernate.max_fetch_depth"is the default, so upon doing "entitymanager.refresh(parent)" everything is loaded by a big "left outer join xxx left outer join yyy":
Code:
select parent0_.id as id157_2_, children1_.parent_id as parent2_4_,
children1_.id as id4_, children1_.id as id158_0_, children1_.parent_id as parent2_158_0_,
kinder2_.parent_id as parent2_5_, kinder2_.id as id5_, kinder2_.id as id159_1_, kinder2_.parent_id as parent2_159_1_
from FOOBAR_PARENT parent0_
left outer join FOOBAR_CHILD children1_ on parent0_.id=children1_.parent_id
left outer join FOOBAR_KIND kinder2_ on parent0_.id=kinder2_.parent_id
where parent0_.id=?
which obiously produces a lot more then it should in case one of the two relations has more then one entry.
As a workaround i now set "hibernate.max_fetch_depth=0" and the problem disapperas.
to reproduce:
Code:
Parent parent = new Parent();
List<Child> children = new ArrayList<Child>();
Child child = new Child();
child.setParent(parent);
children.add(child);
child = new Child();
child.setParent(parent);
children.add(child);
parent.setChildren(children);
List<Kind> kinder = new ArrayList<Kind>();
Kind kind = new Kind();
kind.setParent(parent);
kinder.add(kind);
kind = new Kind();
kind.setParent(parent);
kinder.add(kind);
parent.setKinder(kinder);
manager.persist(parent);
manager.flush();
int numChildrenBeforeRefresh = parent.getChildren().size();
manager.refresh(parent);
int numChildrenAfterRefresh = parent.getChildren().size();
if (numChildrenBeforeRefresh!=numChildrenAfterRefresh) {
System.out.println("Error. Number of children magically changed...");
}
Parent:
Code:
@Entity
@Table(name = "FOOBAR_PARENT")
public class Parent implements java.io.Serializable {
protected long id;
protected List<Child> children;
protected List<Kind> kinder;
@Id
@GeneratedValue
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "parent")
public List<Child> getChildren() {
return children;
}
public void setChildren(List<Child> children) {
this.children = children;
}
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "parent")
public List<Kind> getKinder() {
return kinder;
}
public void setKinder(List<Kind> kinder) {
this.kinder = kinder;
}
}
Child
Code:
@Entity
@Table(name = "FOOBAR_CHILD")
public class Child implements java.io.Serializable {
protected long id;
protected Parent parent;
@Id
@GeneratedValue
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "parent_id")
public Parent getParent() {
return parent;
}
public void setParent(Parent parent) {
this.parent = parent;
}
}
Kind
Code:
@Entity
@Table(name = "FOOBAR_KIND")
public class Kind implements java.io.Serializable {
protected long id;
protected Parent parent;
@Id
@GeneratedValue
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "parent_id")
public Parent getParent() {
return parent;
}
public void setParent(Parent parent) {
this.parent = parent;
}
}
My Versions:
* JBoss 4.0.4RC1
* Hibernate Annotations 3.1beta8
* Hibernate EntityManager 3.1beta6
* Firebird 1.5[/code]