Cascading save on OneToMany causes not null constraint violation on child table
-------------------------------------------------------------------------------
Key: HHH-2935
URL:
http://opensource.atlassian.com/projects/hibernate/browse/HHH-2935
Project: Hibernate3
Issue Type: Bug
Affects Versions: 3.2.1
Environment: hibernate-3.2.1, hibernate-annotations-3.2.1
Reporter: Aaron Luchko
Attachments: saveNull.zip
When defining a OneToMany relationship, controlled by the parent, and not nullable by the
child, there's an issue when trying to insert the initially created columns.
With a schema attached as thus
public class Parent {
...
@OneToMany(targetEntity = Child.class, cascade = { CascadeType.ALL })
@Cascade( { org.hibernate.annotations.CascadeType.SAVE_UPDATE,
org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
@JoinColumn(name = "parent_child")
public List<Child> getChildren() {
return children;
}
}
public class Child{
...
@ManyToOne
@JoinColumn(name = "parent_child", insertable = false, updatable = false,
nullable=false)
public Parent getParent() {
return parent;
}
}
And running a section of code such as this
Session session = HibernateUtil.getSession();
session.beginTransaction();
Parent parent = new Parent();
int times = 2;
List<Child> children = new ArrayList<Child>(times);
for (int i = 0; i < times; i++) {
Child child = new Child();
children.add(child);
child.setParent(parent);
}
parent.setChildren(children);
session.save(parent);
session.getTransaction().commit();
Hibernate attempts to run the following series of queries
select child_.id from Child child_ where child_.id=?
select child_.id from Child child_ where child_.id=?
insert into Parent (id) values (?)
insert into Child (id) values (?)
insert into Child (id) values (?)
update Child set parent_child=? where id=?
update Child set parent_child=? where id=?
The problem is that in initial batch inserting the two children fails violating the not
null constraint before the update can assign a value to parent_child.
I've reproduced this with postgresql and mysql (after setting the dialect to
org.hibernate.dialect.MySQLInnoDBDialect, MyISAM doesn't catch the constraint
violation).
As a workaround one can simply leave off the nullable=false.
I've attached a full testcase.
thanks,
Aaron
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
http://opensource.atlassian.com/projects/hibernate/secure/Administrators....
-
For more information on JIRA, see:
http://www.atlassian.com/software/jira