mirror of https://github.com/apache/openjpa.git
OPENJPA-851: enhanced bi-directional map support in
update operation git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@734617 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
388c56f05f
commit
88f288b47a
|
@ -201,6 +201,9 @@ public class HandlerRelationMapTableFieldStrategy
|
|||
|
||||
public void update(OpenJPAStateManager sm, JDBCStore store, RowManager rm)
|
||||
throws SQLException {
|
||||
if (field.getMappedBy() != null)
|
||||
return;
|
||||
|
||||
Map map = (Map) sm.fetchObject(field.getIndex());
|
||||
ChangeTracker ct = null;
|
||||
if (map instanceof Proxy) {
|
||||
|
|
|
@ -230,29 +230,28 @@ public class RelationFieldStrategy
|
|||
if (rel == null)
|
||||
return;
|
||||
ClassMetaData meta = rel.getMetaData();
|
||||
FieldMapping[] fields = ((ClassMapping)meta).getFieldMappings();
|
||||
for (int i = 0; i < fields.length; i++) {
|
||||
FieldMetaData mappedBy = fields[i].getMappedByMetaData();
|
||||
if (mappedBy == field) {
|
||||
if (fields[i].getDeclaredTypeCode() == JavaTypes.MAP) {
|
||||
Map mapObj = (Map)rel.fetchObjectField(
|
||||
fields[i].getIndex());
|
||||
Object keyObj = getMapKeyObj(mapObj,
|
||||
sm.getPersistenceCapable());
|
||||
ValueMapping key = fields[i].getKeyMapping();
|
||||
FieldMapping mapField = getMapField(meta);
|
||||
|
||||
// there is no bi-directional map field
|
||||
if (mapField == null)
|
||||
return;
|
||||
|
||||
Map mapObj = (Map)rel.fetchObjectField(mapField.getIndex());
|
||||
Object keyObj = getMapKeyObj(mapObj, sm.getPersistenceCapable());
|
||||
ValueMapping key = mapField.getKeyMapping();
|
||||
if (!key.isEmbedded()) {
|
||||
if (keyObj instanceof PersistenceCapable) {
|
||||
OpenJPAStateManager keySm = RelationStrategies.
|
||||
getStateManager(keyObj, store.getContext());
|
||||
// key is an entity
|
||||
ForeignKey fk = fields[i].getKeyMapping().
|
||||
ForeignKey fk = mapField.getKeyMapping().
|
||||
getForeignKey();
|
||||
ColumnIO io = new ColumnIO();
|
||||
row.setForeignKey(fk, io, keySm);
|
||||
}
|
||||
} else {
|
||||
// key is an embeddable or basic type
|
||||
FieldStrategy strategy = fields[i].getStrategy();
|
||||
FieldStrategy strategy = mapField.getStrategy();
|
||||
if (strategy instanceof
|
||||
HandlerRelationMapTableFieldStrategy) {
|
||||
HandlerRelationMapTableFieldStrategy strat =
|
||||
|
@ -263,10 +262,17 @@ public class RelationFieldStrategy
|
|||
kio, true);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private FieldMapping getMapField(ClassMetaData meta) {
|
||||
FieldMapping[] fields = ((ClassMapping)meta).getFieldMappings();
|
||||
for (int i = 0; i < fields.length; i++) {
|
||||
FieldMetaData mappedBy = fields[i].getMappedByMetaData();
|
||||
if (fields[i].getDeclaredTypeCode() == JavaTypes.MAP &&
|
||||
mappedBy == field)
|
||||
return fields[i];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private Object getMapKeyObj(Map mapObj, Object value) {
|
||||
|
@ -294,8 +300,12 @@ public class RelationFieldStrategy
|
|||
&& field.isBidirectionalJoinTableMappingNonOwner()) ?
|
||||
Row.ACTION_DELETE : Row.ACTION_UPDATE;
|
||||
Row row = field.getRow(sm, store, rm, action);
|
||||
if (row != null)
|
||||
if (row != null) {
|
||||
field.setForeignKey(row, rel);
|
||||
// this is for bi-directional maps, the key and value of the
|
||||
// map are stored in the table of the mapped-by entity
|
||||
setMapKey(sm, rel, store, row);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -325,6 +335,9 @@ public class RelationFieldStrategy
|
|||
fk.getDeleteAction() == ForeignKey.ACTION_CASCADE) {
|
||||
Row row = field.getRow(sm, store, rm, Row.ACTION_DELETE);
|
||||
row.setForeignKey(fk, null, rel);
|
||||
// this is for bi-directional maps, the key and value of the
|
||||
// map are stored in the table of the mapped-by entity
|
||||
setMapKey(sm, rel, store, row);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -243,6 +243,9 @@ public class RelationRelationMapTableFieldStrategy
|
|||
|
||||
public void update(OpenJPAStateManager sm, JDBCStore store, RowManager rm)
|
||||
throws SQLException {
|
||||
if (field.getMappedBy() != null)
|
||||
return;
|
||||
|
||||
Map map = (Map) sm.fetchObject(field.getIndex());
|
||||
ChangeTracker ct = null;
|
||||
if (map instanceof Proxy) {
|
||||
|
|
Loading…
Reference in New Issue