Parent/Child Save Issue - cf-orm-dev cross post

Apologies for the cross post - wasn't really sure which list this fell
under.

I have two entities, defined as thus:
Parent:

component persistent="true" entityname="ParentEntity"
table="PARENT_TABLE" output="false" {
  property name="id" column="PARENT_ID" fieldtype="id"
generator="increment" sqltype="integer" precision="0" ;
  property name="name";
  property name="children" singularname="child" type="array"
fieldtype="one-to-many" cfc="ChildEntity" inverse="false"
fkcolumn="CHILD_ID";

Child:

component persistent="true" entityname="ChildEntity"
table="CHILD_TABLE" output="false" {
  property name="id" column="CHILD_ID" fieldtype="id"
generator="increment" sqltype="integer" ;
  property name="parent" fieldtype="many-to-one" fkcolumn="PARENT_ID"
cfc="ParentEntity" sqltype="integer" ;

I have a form in a view which has parent information at the top of the
page and a way to add children below that. I would like to create and
save the children in the same handler that I create and save the
parent.

the snippet of my handler code looks like:
rc.parent = courseparentService.new();

beanFactory.populateBean(rc.parent);
formUtil.buildFormCollections(rc);

parentService.save(rc.parent);

parentService.refresh(rc.parent);

for(var i = 1; i <= rc.numberOfChildren; i++)
{
  var child = childService.new();
  child.setParent(rc.parent);
  beanFactory.populateFromStruct(child,evaluate("rc.child"&i));
  rc.parent.addChild(child);
  childService.save(child);
}

The error thrown is:
Error Messages: org.hibernate.exception.ConstraintViolationException:
Could not execute JDBC batch update

The beginning of the stack trace is:
coldfusion.runtime.CustomException:
org.hibernate.exception.ConstraintViolationException: Could not
execute JDBC batch update
  at coldfusion.tagext.lang.ThrowTag.doStartTag(ThrowTag.java:142)
  at coldfusion.runtime.CfJspPage._emptyTcfTag(CfJspPage.java:2722)
  at cfBaseORMService2ecfc842935831$funcSAVE.runFunction(F:\workspace
\tracker_bb\wwwroot\coldbox\system\orm\hibernate\BaseORMService.cfc:
669)

I wouldn't have thought this was uncommon..

can anyone point me in the right direction?

Cheers
Steve

The error message suggests a constraint violation, which means you may have
to switch on the HQL Debugging to see what it is stopping on.

I would also suggest placing all of this inside of a transaction block as
well.

Regards,
Andrew Scott
http://www.andyscott.id.au/

Thanks Andrew.

I assumed the problem was a constraint issue and the parentID is not available when the children are being saved. Although when it fails it has already inserted the parent record… hmmm

You’re idea of a transaction block sounds good - but I’m not sure how to enforce the transaction block within coldbox though?

Cheers
Steve

You don’t need to worry about ColdBox here, just wrap it in the transaction block, put a try/catch there as well to trap and report that an error has occurred and rollback, and report this to your calling code.

Regards,

Andrew Scott

http://www.andyscott.id.au/