[
http://opensource.atlassian.com/projects/hibernate/browse/HHH-6708?page=c...
]
Florian Rampp commented on HHH-6708:
------------------------------------
After some more testing, I realized that the problem is more complex.
1. When calling {{session.save()}}, cascading using the annotation
{{j.p.CascadeType.PERSIST}} has no effect. It only does, if {{session.persist()}} is used.
On the other hand, if you use {{j.p.CascadeType.ALL}}, cascading also works with the
{{session.save()}} method, since {{ALL}} implicitly adds
{{o.h.a.CascadeType.SAVE_UPDATE}}, whereas {{j.p.CascadeType.PERSIST}} does not. The
JavaDoc of {{ALL}} says:
{quote}The value cascade=ALL is equivalent to cascade=\{PERSIST, MERGE, REMOVE, REFRESH,
DETACH\}{quote}
I cannot assess, if this is an error. At least, it is behavior that is non-intuitive.
({{j.p.}} == {{java.persistence.}}, {{o.h.a.}} == {{org.hibernate.annotations}})
2. If I change the mapping of the {{Child}} to its {{Parent}} from
{code:java}
@Id
@OneToOne(optional = false)
private Parent parent;
{code}
to
{code:java}
@Id
private Long parentId;
@MapsId
@OneToOne(optional = false)
private StrongEntity parent;
{code}
the StackOverflowError does not occur any more. While the second one is the JPA way,
http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/mapping.htm... says,
that Hibernate is supposed to support my initial mapping as well.
So the total solution for this is the use {{session.persist()}} instead of
{{session.save()}} and the use {{@MapsId}} instead of annotating a {{@OneToOne}}
relationship directly with {{@Id}}.
Loading parent/child entities with foreign key in child table being
its primary key results in StackOverflowError
-----------------------------------------------------------------------------------------------------------------
Key: HHH-6708
URL:
http://opensource.atlassian.com/projects/hibernate/browse/HHH-6708
Project: Hibernate Core
Issue Type: Bug
Components: core
Affects Versions: 3.6.6
Environment: HSQL 1.8.0.10 (also reproducible with Oracle 11g, with Oracle JDBC
11.2.0.1.0
Reporter: Florian Rampp
Labels: hibernate, jpa2
Attachments: bugreports-hibernate-cascadeAll-1.0-SNAPSHOT-src.zip
Consider the following parent/child relationship mapping:
{code:java}
@Entity
public class Parent {
@Id
Long id;
// @OneToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE,
CascadeType.REFRESH,
// CascadeType.DETACH }, orphanRemoval = true, mappedBy = "parent")
@OneToOne(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy =
"parent")
Child child;
void setChild(Child child) {
this.child = child;
child.setParent(this);
}
}
@Entity
public class Child implements Serializable {
@Id
@OneToOne(optional = false)
private Parent parent;
public void setParent(Parent parent) {
this.parent = parent;
}
}
{code}
When storing a parent with a child and loading it again, the load results in a
StackOverflowError. When I replace the cascade type {{ALL}} with a list of all cascade
types {{\{CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE, CascadeType.REFRESH,
CascadeType.DETACH\}}}, the error does not occur any more.
I attached a test case.
--
This message is automatically generated by JIRA.
For more information on JIRA, see:
http://www.atlassian.com/software/jira