当我们将包含子实体的父实体对象保存为一对多时,仅将父插入父数据库。
下面是postInsert(DescriptorEvent事件)
public void postInsert(DescriptorEvent event) {
AbstractEntity ae = (AbstractEntity) event.getObject();
// inserting all the fields as seperate entry to audit table
InsertObjectQuery query = (InsertObjectQuery) event.getQuery();
AuditSession as = new AuditSession();
as.setOperationType("I");
as.setReason("test reason");
long count=1;
for (int i = 0; i < query.getModifyRow().getFields().size(); i++)
{
AuditTrail at = new AuditTrail();
DatabaseField dbField =(DatabaseField)query.getModifyRow().getFields().elementAt(i);
if (dbField == null)
continue;
String fieldName = dbField.getName();
if(query.getModifyRow().getValues(fieldName) != null){
at.setNewValue(query.getModifyRow().getValues(fieldName));
}else{
at.setNewValue(null);
}
at.setFieldId(fieldName);
at.setOldValue(null);
if (ae.getClass().getAnnotation(Table.class) != null) {
at.setTableId(ae.getClass().getAnnotation(Table.class)
.name());
} else {
at.setTableId(ae.getClass().getSimpleName());
}
at.setOperationType("I");
at.setSeqNo(count);
at.setAppRecordId("1");
as.addAuditTrail(at);
count++;
}
event.getSession().insertObject(as);
}
在上面的方法中,AuditSession是具有OneToMany映射到AuditTrail实体的父实体
答案 0 :(得分:0)
所有插入,更新和删除查询都默认为级联私有部分,因此除非您的1:M映射是私有的,否则不会插入引用的子对象。您可以根据需要使用cascadeAllParts()方法在查询上更改此值: http://www.eclipse.org/eclipselink/api/2.0/org/eclipse/persistence/queries/DatabaseQuery.html#cascadeAllParts()