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)
|
public void update(OpenJPAStateManager sm, JDBCStore store, RowManager rm)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
|
if (field.getMappedBy() != null)
|
||||||
|
return;
|
||||||
|
|
||||||
Map map = (Map) sm.fetchObject(field.getIndex());
|
Map map = (Map) sm.fetchObject(field.getIndex());
|
||||||
ChangeTracker ct = null;
|
ChangeTracker ct = null;
|
||||||
if (map instanceof Proxy) {
|
if (map instanceof Proxy) {
|
||||||
|
|
|
@ -230,29 +230,28 @@ public class RelationFieldStrategy
|
||||||
if (rel == null)
|
if (rel == null)
|
||||||
return;
|
return;
|
||||||
ClassMetaData meta = rel.getMetaData();
|
ClassMetaData meta = rel.getMetaData();
|
||||||
FieldMapping[] fields = ((ClassMapping)meta).getFieldMappings();
|
FieldMapping mapField = getMapField(meta);
|
||||||
for (int i = 0; i < fields.length; i++) {
|
|
||||||
FieldMetaData mappedBy = fields[i].getMappedByMetaData();
|
// there is no bi-directional map field
|
||||||
if (mappedBy == field) {
|
if (mapField == null)
|
||||||
if (fields[i].getDeclaredTypeCode() == JavaTypes.MAP) {
|
return;
|
||||||
Map mapObj = (Map)rel.fetchObjectField(
|
|
||||||
fields[i].getIndex());
|
Map mapObj = (Map)rel.fetchObjectField(mapField.getIndex());
|
||||||
Object keyObj = getMapKeyObj(mapObj,
|
Object keyObj = getMapKeyObj(mapObj, sm.getPersistenceCapable());
|
||||||
sm.getPersistenceCapable());
|
ValueMapping key = mapField.getKeyMapping();
|
||||||
ValueMapping key = fields[i].getKeyMapping();
|
|
||||||
if (!key.isEmbedded()) {
|
if (!key.isEmbedded()) {
|
||||||
if (keyObj instanceof PersistenceCapable) {
|
if (keyObj instanceof PersistenceCapable) {
|
||||||
OpenJPAStateManager keySm = RelationStrategies.
|
OpenJPAStateManager keySm = RelationStrategies.
|
||||||
getStateManager(keyObj, store.getContext());
|
getStateManager(keyObj, store.getContext());
|
||||||
// key is an entity
|
// key is an entity
|
||||||
ForeignKey fk = fields[i].getKeyMapping().
|
ForeignKey fk = mapField.getKeyMapping().
|
||||||
getForeignKey();
|
getForeignKey();
|
||||||
ColumnIO io = new ColumnIO();
|
ColumnIO io = new ColumnIO();
|
||||||
row.setForeignKey(fk, io, keySm);
|
row.setForeignKey(fk, io, keySm);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// key is an embeddable or basic type
|
// key is an embeddable or basic type
|
||||||
FieldStrategy strategy = fields[i].getStrategy();
|
FieldStrategy strategy = mapField.getStrategy();
|
||||||
if (strategy instanceof
|
if (strategy instanceof
|
||||||
HandlerRelationMapTableFieldStrategy) {
|
HandlerRelationMapTableFieldStrategy) {
|
||||||
HandlerRelationMapTableFieldStrategy strat =
|
HandlerRelationMapTableFieldStrategy strat =
|
||||||
|
@ -263,10 +262,17 @@ public class RelationFieldStrategy
|
||||||
kio, true);
|
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) {
|
private Object getMapKeyObj(Map mapObj, Object value) {
|
||||||
|
@ -294,8 +300,12 @@ public class RelationFieldStrategy
|
||||||
&& field.isBidirectionalJoinTableMappingNonOwner()) ?
|
&& field.isBidirectionalJoinTableMappingNonOwner()) ?
|
||||||
Row.ACTION_DELETE : Row.ACTION_UPDATE;
|
Row.ACTION_DELETE : Row.ACTION_UPDATE;
|
||||||
Row row = field.getRow(sm, store, rm, action);
|
Row row = field.getRow(sm, store, rm, action);
|
||||||
if (row != null)
|
if (row != null) {
|
||||||
field.setForeignKey(row, rel);
|
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) {
|
fk.getDeleteAction() == ForeignKey.ACTION_CASCADE) {
|
||||||
Row row = field.getRow(sm, store, rm, Row.ACTION_DELETE);
|
Row row = field.getRow(sm, store, rm, Row.ACTION_DELETE);
|
||||||
row.setForeignKey(fk, null, rel);
|
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)
|
public void update(OpenJPAStateManager sm, JDBCStore store, RowManager rm)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
|
if (field.getMappedBy() != null)
|
||||||
|
return;
|
||||||
|
|
||||||
Map map = (Map) sm.fetchObject(field.getIndex());
|
Map map = (Map) sm.fetchObject(field.getIndex());
|
||||||
ChangeTracker ct = null;
|
ChangeTracker ct = null;
|
||||||
if (map instanceof Proxy) {
|
if (map instanceof Proxy) {
|
||||||
|
|
Loading…
Reference in New Issue